Page 1 of 1

remaning files from download in batch

Posted: 11 Apr 2012 08:52
by mbessey
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?

Re: remaning files from download in batch

Posted: 11 Apr 2012 10:31
by Ed Dyreen
'
It can be done in batch.

Re: remaning files from download in batch

Posted: 11 Apr 2012 10:38
by Ed Dyreen
'
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

Posted: 11 Apr 2012 10:54
by !k

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

Posted: 11 Apr 2012 13:13
by foxidrive
A good method - I'd use this variation both for readibility and to stop the possibility of running into the call recursion limit

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

Posted: 11 Apr 2012 13:46
by Ed Dyreen
'
I agree foxi, loops are made with goto, not with call,
besides the recursion limit, call wastes memory and performance.