Link to home
Start Free TrialLog in
Avatar of FriarTuk
FriarTuk

asked on

Batch file renaming with increments

How can I rename a group of files in a folder with an incremented number?  The folder can be any folder located under My Pictures, & can be inputted into the batch file.  I can rename files adding the date to the end (see below - not unique), but I'd rather have a unique # that is 3 digits including leading zeroes, such as beachpics001-999.jpg
-------------------
TEST.BAT

@echo off
IF (%1) == () GOTO syntax
set zpath="c:\temp2"

set zdate=%date:~4,2%%date:~7,2%%date:~-2%
REM zdate would be MMDDYY
FOR %%F IN (%zpath%\%1\*.jpg) DO ren %%F %%~nF%zdate%%%~xF
REM result would be filnamdate.ext

goto end
:syntax
echo.
echo Usage:  TEST.bat "FolderName"
echo Any FolderName located under "c:\my documents\my pictures\"
echo This routine will rename all files in user inputted folder with the original
echo file name plus an incremented number from 001-999 plus the file extension
echo.
:end
-------------------
So the entire middle section would be replaced with something similar to this:
FOR %%F IN (%zpath%\%1\*.txt) DO ren %%F %%~nF%INCNUM%%%~xF
What I need is the how-to of getting the variable INCNUM including the leading 0's.

I have tested the above as is & it works, but I couldn't get it to work when trying to set zpath to "c:\my documents\my pictures" - nor if I tried including my homepath location like:
(%homepath%\%zpath%\%1\*.jpg).  Is there a special variable that represents the current user's my documents path?


Any help from those DOS guru's lurking around would be of great help, thx.

FriarTuk
TheDeepFatFryer says, "Can you read me now?"  :)

ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FriarTuk
FriarTuk

ASKER

Thanks for the prompt response SteveGTR.

I understand this line - assuming that the "call:renfile" is similar to a goto, correct?
for %%f in ("%zpath%\%1\*.jpg") DO call :RENFILE "%%f"

I also understand the nextinc phase comparing #'s then adding it to a string.  Thank U very much!

But I'm not sure about what this line does in the beg & end, but I know it does the rename part in the middle.  Could you explain it a little plz?
if not exist "%~dpf1%strCnt%%~x1" ren %1 "%~n1%strCnt%%~x1"&goto :EOF


Also do you know of a preset variable that points to "c:\my documents & settings\currently logged in unknown username\my documents" so I could set zpath to whatever data type subfolder like "my pictures" ("my music", "my received files") & then feed it another subfolder under it that one (like holidaypics).

For tonight I'll be testing the new incremented number code you provided using zpath = c:\temp2 & feeding the batch file a subfolder named hold - I'll let you know the results.

Thx again.
SteveGTR

this line
      if not exist "%~dpf1%strCnt%%~x1" echo ren %1 "%~n1%strCnt%%~x1"&goto :EOF

Produces these results <the "if not exist" file is incorrect at "blank1.txt001.txt">
     C:\Temp>if not exist "c:\Temp2\hold\blank1.txt001.txt" ren "c:\temp2\hold\blank1.txt" "blank1001.txt"  & goto :EOF

My test did work using 5 differently named files, blank1.txt thru blank5.txt -
the results were blank1001.txt thru blank5001.txt.  Can you modify your procedure a little so I can ren all files to baby001.txt-baby005.txt?  Maybe I can feed in two variables into the batch file like "test.bat hold baby" -> hold = subfolder & baby = new file name to attach an incremented # to.


In my original line using a date instead of 3 digit #
      DO ren %%F %%~nF%zdate%%%~xF
I used ren %%F vs you used %1
I used %%~nF which equals just the filename vs you used %~n1
I used %%~xF which equals just the fileextention vs you used %~x1
Any difference which is used?  How come the %1 you use doesn't relate to the one I used in the beginning of the batch file (inputted subfolder)? Does putting everything in quotes mean you don't need to used two %% infront of the variable, such as %%F vs %1?
Whew! I played with the code until I could get it to ren any filenam.txt in subfolder hold in zpath c:\temp2 to baby001.txt - baby005.txt.  Here's the changes I made & why.

IF (%1) == () GOTO syntax
IF (%2) == () GOTO syntax
set zpath="c:\temp2"

set /a cnt=1
     * moved the above stmt from below the RENFIL area because the goto EOF on
     * the "if not exist" line would go back to the FOR line thus cnt would = 1 again &
     * again when doing the number compare thus always being 001

FOR %%f IN ("%zpath%\%1\*.txt") DO CALL :RENFIL %2 %%f
     * %2 = user input newfilnam, ie baby
     * %%f =  current filnam in folder, ie c:\temp2\hold\blank1.txt
goto :EOF
     * this goto EOF only works after the last file is done being renamed

:RENFIL
if /i %cnt% LSS 10 (
  set strCnt=00%cnt%
) else if /i %cnt% LSS 100 (
  set strCnt=0%cnt%
) else (
  set strCnt=%cnt%
)

     * removed the nextinc loop & its corresponding goto stmt since during testing
     * it never did anything since the "goto EOF" at the end of the "if not exist" line
     * went back to the FOR line before incrementing the count & bypassing the nextinc loop

if not exist "%~dpf1%strCnt%%~x2" ren %2 "%~n1%strCnt%%~x2"
     * produces:
           if not exist "C:\Temp\baby001.txt" ren c:\temp2\hold\blank1.txt "baby001.txt"
     * %1 = baby %2 = c:\temp2\hold\blank1.txt
     * A) "%~dpf1%strCnt%%~x1" chg'd ~x1 to ~x2 to grab .ext from existing file
     *      since no .ext exists on user inputted file name of baby
     * B) ren %1 chg'd to ren %2 grabbing the path\filenam.ext
     * C) "%~n1%strCnt%%~x1" chg'd ~x1 to ~x2 to grab .ext from existing file

set /a cnt=cnt+1
goto :EOF
     * moved the "goto EOF" from the "if not exist" line to after the increment count

--------------------------------------------------
Questions unanswered:

from my 1st response:
1) plz explain systax for "if not exist" line
2) plz answer about preset variables in xp, especially to my documents for current logged in user

from my 2nd response:
1) the "if not exist" produces incorrect results - points to wrong (current dos path) folder, needs fixing
2) solved issue about inputting a 2nd variable indicating new file name on my own
3) plz explain bottom questions about variables & syntax

And SteveGTR - upping pts from 125 to 200 - thanks again!
Q. plz explain systax for "if not exist" line

if not exist "%~dpf1%strCnt%%~x1" echo ren %1 "%~n1%strCnt%%~x1"&goto :EOF

A. This code basically makes sure that the file that we are going to rename to doesn't exist prior to us trying to rename it. It makes use of the enhanced variable substitution options. In this case I'm using the 1st parameter (%1). I believe I got a little carried away with the 1st usage of %~dpf1. This works on my home machine, but it looks to be overkill as 'd' pulls the drive letter only, 'p' pulls the path, and 'f' is the fully qualified name. I should have just used %~f1. For more information on these check out the help for the for command by typing for /? at the command prompt.

Q. plz answer about preset variables in xp, especially to my documents for current logged in user

A. You could try the HOMEPATH. Type set homepath to see what it equals. Or maybe USERPROFILE.

Q. the "if not exist" produces incorrect results - points to wrong (current dos path) folder, needs fixing

A. As I stated above, I believe %~f1 will work better.

Q. solved issue about inputting a 2nd variable indicating new file name on my own

A. No sure if this is still an issue. Looks like you took care of this. I don't know...

Q. plz explain bottom questions about variables & syntax

A. You were correct when you mentioned that the CALL <label> syntax acted like a subroutine. Anything passed to the subroutine is referencable by using the same syntax that you use for the parameters of the original batch file, i.e. %1 %2. As for the quotes around file names. This has nothing to do with how you reference %%f. It has to do with protection against file names with spaces. If you don't do this you'll get into trouble when evaulating some statements for example:

if exist c:\documents and settings echo This generates a syntax error

Should be coded:

if exist "c:\documents and settings" echo This works




I'm going to go ahead & accept your 1st comment as answer, even though it needed a little tweaking, you gave me what I needed & I appreciated it.  I'll keep trying the preset variables to see if I can get them to work & will post back final results.

I used to be great working with DOS 622 & Win98se DOS, but this XP DOS has all these unfamilar bells & whistles.  I guess if I keep working with it long enough I'll eventually figure it out, but in the meantime if I get stumped I'll be back here asking for help again.

cya l8r & thanks again,
                      FriarTuk
                      TheDeepFatFryer says, "Can you read me now?"  :)