Computing.Net > Forums > Disk Operating System > Renaming files with a batch script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Renaming files with a batch script

Reply to Message Icon

Name: Russell04
Date: September 26, 2003 at 11:48:41 Pacific
OS: Windows 2000 Pro
CPU/Ram: 1.6Ghz/512 MB
Comment:

I noticed on an earlier forum that someone helped a person to rename some files to another particular format.

I am having problems doing the same thing and I am very unfamiliar with COMMAND.exe under Windows 2000 Pro.

This is the script that was given:

@echo off
if "%1"=="GoTo" goto %2
if not "%OS%"=="Windows_NT" goto eof
%comspec% /v:on /c %0 GoTo start
goto eof

:start
echo.
echo This program will rename all files from the current directory,
echo and possibly from its subdirectories, following a certain rule.
echo.
echo Press any key to VIEW the renaming process . . .
pause > nul
echo Processing . . .

type nul> %temp%.\process.bat
:: To include the files under subdirectories from current
:: directory on the renaming process, add the string ' /R'
:: after the string 'for' on the next line, so it becomes
:: 'for /R' (don't forget the space between the strings)
for %%F in (*.*) do (
set ORIG=%%~nF
set ORIG=!ORIG:-=!
set FILE=!ORIG:~3,-4!
set EXT=!ORIG:~-4,-1!
if not "!FILE!"=="" if not "!EXT!"=="" (
echo REN "%%F" "!FILE!.!EXT!">> %temp%.\process.bat
)
)

echo.
type %temp%.\process.bat |MORE
echo The above process is NOT reversible.
echo.
set CH=
:loop
set /P CH=To you wish to execute the above process [Y/N]?
if /i "%CH%"=="Y" (
call %temp%.\process.bat
echo.
echo Operation done.
del %temp%.\process.bat
goto eof
)
if /i "%CH%"=="N" (
echo.
echo Operation aborted
del %temp%.\process.bat
goto eof
)
goto loop

:eof

What I have is a folder that contains filenames under inconsistent formats.

The folder contains .jpg files under the following 2 formats:

filenumber.x.jpg (“x” can be any number)

filenumber.jpg

I want all the filenames to conform to: filename.jpg. So I want to strip off the sequence (“.x”) from any filename that contains the sequence.

Could you please help me out and make some modifications to the above script to accommodate the changes?

I would greatly appreciate your help. Also is there a site you could recommend that would provide some good documentation for COMMAND.exe file management methods.

Thank you very much for your time,

Russell



Sponsored Link
Ads by Google

Response Number 1
Name: Russell04
Date: September 26, 2003 at 11:52:48 Pacific
Reply:

The above description is inaccurate.

I would like to strip off ".x." sequence and replace it with "." in any file that contains the sequence.

Sorry for the confusion


0

Response Number 2
Name: IVO
Date: September 27, 2003 at 14:27:48 Pacific
Reply:

If your folder contains two kind of .jpg files, i.e.

FileName.x.jpg
FileName.jpg

where x is any sequence, the following solves your problem:

@Echo Off

If %1.==. ( Echo Folder missing & GoTo :EOF )
If not exist %~f1.\Nul ( Echo Folder %~f1 not found & GoTo :EOF )

For /F "tokens=1,2 delims=." %%I in ('Dir %~f1.\*.jpg /B /A:-D') Do (
If /I not "%%J"=="jpg" Echo Ren "%%I.%%J.jpg" "%%I.jpg" )

Save it naming as you like (e.g. XRen.bat) then type at prompt XRen FolderName.

The script as it is, only displays the files to be renamed. To activate, erase the "Echo" in front of the Ren command when you are sure it works fine for your purpose.

The script works under Win 2K/XP/NT NOT under Win 9X/ME.

If your need is not satisfied by this answer, post again.



0

Response Number 3
Name: Russell04
Date: September 29, 2003 at 11:34:54 Pacific
Reply:

Hey I used your script but I got the error :
' and' not recogized as an internal or external command, operable program or batch file.

I figured it out though : I replaced '&' with 'and'

Now it works perfectly....thanks!

Do you know of a good site that would have some documentation of renaming files?


0

Response Number 4
Name: IVO
Date: September 30, 2003 at 03:24:51 Pacific
Reply:

About the "and" issue, I suppose it is solved now; About the need of storing the script into the folder containing the files to be renamed, substitute the original Ren command with the following:

Ren "%~f1.\%%I.%%J.jpg" "%%I.jpg"

and you may call XRen from whatever folder.

Regarding a good site about renaming files, I do not understand what you mean, as renaming under some condition is a process highly influenced by the specific need. So what you need is a strong knowledge of batch commands and scripting.

Have a nice day.


0

Response Number 5
Name: Jens
Date: September 30, 2003 at 07:27:23 Pacific
Reply:

Hello IVO,

i've a similar renaming problem, perhaps you can give me an easy hint!?

I've to rename all the files in a folder from

filename.ext to
filenameext.plt

(The extension goes into the filename, and each file gets the extension ".plt")

Thanks in advance,

Jens


0

Related Posts

See More



Response Number 6
Name: IVO
Date: September 30, 2003 at 08:33:53 Pacific
Reply:

Check your e-mail in-box, I'm sending you what you requested.

By the way, another time open a new post, not queue after a thread in progress unless your query is strictly related to that.

You may risk to get no help at all.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Disk Operating System Forum Home


Sponsored links

Ads by Google


Results for: Renaming files with a batch script

HELP Renaming files with a .bat file www.computing.net/answers/dos/help-renaming-files-with-a-bat-file-/3679.html

Dos Batch Script with Type www.computing.net/answers/dos/dos-batch-script-with-type/14148.html

Renaming files www.computing.net/answers/dos/renaming-files/13066.html