I have a batch file that gets a .jpg from a ftp site. I want to be able to rename the file after downloading if it already exist by andding incrementing numbers to the file name.
image1
image2
image3
can this be done in the batch file or am i barking up the wrong tree?
remaning files from download in batch
Moderator: DosItHelp
Re: remaning files from download in batch
'
It can be done in batch.
It can be done in batch.
Re: remaning files from download in batch
'
You didn't ask for it, but I'll show you how anyways
You didn't ask for it, but I'll show you how anyways
Code: Select all
@echo off &setlocal enableDelayedExpansion
set "$fileName=dummy"
set "$fileExt=TXT"
set "uBound=50"
::
for /l %%? in ( 1, 1, %uBound% ) do if exist "!$fileName!.!$fileExt!" (
::
echo. ren "!$fileName!.!$fileExt!" "!$fileName!%%~?.!$fileExt!"
)
pause
exit /b 0
Re: remaning files from download in batch
Code: Select all
@echo off &setlocal enableextensions
set "file=image.jpg"
set /a cnt=0
call :next "%file%"
goto :eof
:next
set /a cnt+=1
ren %1 "%~n1%cnt%%~x1" 2>nul ||call :next %1
goto :eof
Re: remaning files from download in batch
A good method - I'd use this variation both for readibility and to stop the possibility of running into the call recursion limit
and this version has padded the numbers out to 5 digits for easy sorting.
Code: Select all
@echo off &setlocal enableextensions
set "file=image.jpg"
set /a cnt=0
call :next "%file%"
goto :eof
:next
set /a cnt+=1
if exist "%~n1%cnt%%~x1" goto :next
ren %1 "%~n1%cnt%%~x1"
goto :eof
and this version has padded the numbers out to 5 digits for easy sorting.
Code: Select all
@echo off &setlocal enableextensions
set "file=image.jpg"
set /a cnt=0
call :next "%file%"
goto :eof
:next
set /a cnt+=1
set num=0000%cnt%
set num=%num:~-5%
if exist "%~n1%num%%~x1" goto :next
ren %1 "%~n1%num%%~x1"
goto :eof
Re: remaning files from download in batch
'
I agree foxi, loops are made with goto, not with call,
besides the recursion limit, call wastes memory and performance.
I agree foxi, loops are made with goto, not with call,
besides the recursion limit, call wastes memory and performance.