Answer» Hello,
Ok, I need some serious ASSISTANCE with batch files. I know I'm new here but plan to now visit regularly. I am trying to perform the following:
1. count all files in a directory 2. rename a file numerically based on total file count + 1 3. append that new filename with leading zeros until a total character count of 8 total characters is in the file name
What I've got so far will count all files, rename file to numerical name but won't let me append leading zeros. Can anyone help with this?
dir "c:\Talasa\frames" /a-d | find /c ".jpg" > NUMfiles.### set /p count=Echo %count% set /a counted=%count%+1
ren c:\Talasa\ftp\*.jpg %counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames You should try to do this with another language, say vbs. In batch, things like this could get long and confusingunfortunatly for my purposes it must be done in a batch file. one of those it's too late to turn back now things. :SRESOLVED, not overly elegant but the solution is:
Code: [Select]REM COUNT TOTAL IMAGES IN FRAMES TO DETERMINE NEXT IMAGE NUMBER IN SEQUENCE dir "c:\Talasa\frames" /a-d | find /c ".jpg" > NUMfiles.### set /p count=<NUMfiles.### REM DISPLAY COUNTED NUMBER Echo !count! REM SET VARIABLE TO VALUE OF COUNTED IMAGES +1 set /a counted=%count%+1
SETLOCAL ENABLEDELAYEDEXPANSION Set SearchForChar=. Set CharPos=
set currentpath="c:\Talasa\frames" set filepath="c:\Talasa\ftp\
ren c:\Talasa\ftp\*.jpg %counted%.jpg
CD %filepath%
for %%f in ("*.jpg") do set filename=%%f
Set CheckString=%1 IF NOT Defined CheckString Set CheckString=%filename% IF NOT Defined CheckString GOTO :EOF
FOR /l %%a in (0,1,10) DO IF "!CheckString:~%%a,1!" == "%SearchForChar%" SET /a CharPos=%%a + 1 & IF DEFINED CharPos GOTO CharFound ECHO "%SearchForChar%" was not found in "%CheckString%" GOTO :EOF
:CharFound set /a trueplace=%CharPos%-1 ECHO String Number Ends in "%CheckString%" at position %trueplace%
CD %currentpath%
if [%trueplace%]==[1] GOTO 1 if [%trueplace%]==[2] GOTO 2 if [%trueplace%]==[3] GOTO 3 if [%trueplace%]==[4] GOTO 4 if [%trueplace%]==[5] GOTO 5 if [%trueplace%]==[6] GOTO 6 if [%trueplace%]==[7] GOTO 7 if [%trueplace%]==[8] GOTO 8
:1
ren c:\Talasa\ftp\*.jpg 0000000%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:2 ren c:\Talasa\ftp\*.jpg 000000%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:3 ren c:\Talasa\ftp\*.jpg 00000%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:4 ren c:\Talasa\ftp\*.jpg 0000%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:5 ren c:\Talasa\ftp\*.jpg 000%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:6 ren c:\Talasa\ftp\*.jpg 00%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:7 ren c:\Talasa\ftp\*.jpg 0%counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:8 ren c:\Talasa\ftp\*.jpg %counted%.jpg move C:\Talasa\ftp\*.jpg C:\Talasa\frames GOTO :EOF
:end This is simpler...
1. Edit folder name
2. This code, and also your code above uses no sort-order switch after the dir command. This means that the order of jpg NAMES found by dir is the current Windows default (if you have set dircmd environment variable, if that contains a sort order switch e.g. /on, then the output of a bare dir command will be sorted that way.) If dircmd is not set, then the default sort is by name, ascending) If you wish to FORCE a particular sort order then insert the switch after "dir" e.g. dir /on or dir /o-d or whatever. See dir /? for help.
3. Remove the echo before REN when you are HAPPY it works the way you want.
Code: [Select]@echo off setlocal enabledelayedexpansion set JPGfolder=C:\Users\Mike\Pictures\Camera Photos\Jazmina Pictures\subfolder cd /d "%JPGfolder%" set number=1 for /f "delims=" %%F in ( 'dir /b *.jpg' ) do ( set oldname=%%~nxF set newname=!number!.jpg if !number! LEQ 9999999 set newname=0!newname! if !number! LEQ 999999 set newname=0!newname! if !number! LEQ 99999 set newname=0!newname! if !number! LEQ 9999 set newname=0!newname! if !number! LEQ 999 set newname=0!newname! if !number! LEQ 99 set newname=0!newname! if !number! LEQ 9 set newname=0!newname! echo REN "!oldname!" "!newname!" set /a number+=1 ) Code: [Select]REN "dsc00366.jpg" "00000001.jpg" REN "dsc00386.jpg" "00000002.jpg" REN "dsc00399.jpg" "00000003.jpg" REN "dsc00410.jpg" "00000004.jpg" REN "dsc00411.jpg" "00000005.jpg" REN "dsc00412.jpg" "00000006.jpg" REN "dsc00414.jpg" "00000007.jpg" REN "dsc00415.jpg" "00000008.jpg" REN "dsc00416.jpg" "00000009.jpg" REN "dsc00417.jpg" "00000010.jpg" REN "dsc00418.jpg" "00000011.jpg" REN "dsc00419.jpg" "00000012.jpg" REN "dsc00420.jpg" "00000013.jpg" REN "dsc00421.jpg" "00000014.jpg" REN "dsc00423.jpg" "00000015.jpg" REN "dsc00430.jpg" "00000016.jpg" REN "dsc00431.jpg" "00000017.jpg" REN "dsc00432.jpg" "00000018.jpg" less code; same result
Code: [Select]@echo off setlocal enabledelayedexpansion set JPGfolder=C:\Users\Mike\Pictures\Camera Photos\Jazmina Pictures\subfolder cd /d "%JPGfolder%" set number=1 for /f "delims=" %%F in ( 'dir /b *.jpg' ) do ( set newname=!number!.jpg for %%N in (9999999 999999 99999 9999 999 99 9) do if !number! LEQ %%N set newname=0!newname! echo REN "%%~nxF" "!newname!" set /a number+=1 ) select number of leading zeroes
Code: [Select]@echo off setlocal enabledelayedexpansion set JPGfolder=C:\Users\Mike\Pictures\Camera Photos\Jazmina Pictures\subfolder cd /d "%JPGfolder%"
REM 1=no leading zeroes, 2 to 8, pad to that length set namelength=8
if %namelength% GEQ 2 set formatstring=9 if %namelength% GEQ 3 set formatstring=99 %formatstring% if %namelength% GEQ 4 set formatstring=999 %formatstring% if %namelength% GEQ 5 set formatstring=9999 %formatstring% if %namelength% GEQ 6 set formatstring=99999 %formatstring% if %namelength% GEQ 7 set formatstring=999999 %formatstring% if %namelength% GEQ 8 set formatstring=9999999 %formatstring%
set number=1 for /f "delims=" %%F in ( 'dir /b *.jpg' ) do ( set newname=!number!.jpg for %%N in (%formatstring%) do if !number! LEQ %%N set newname=0!newname! echo REN "%%~nxF" "!newname!" set /a number+=1 )
|