remaning files from download in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mbessey
Posts: 1
Joined: 11 Apr 2012 07:36

remaning files from download in batch

#1 Post by mbessey » 11 Apr 2012 08:52

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?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: remaning files from download in batch

#2 Post by Ed Dyreen » 11 Apr 2012 10:31

'
It can be done in batch.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: remaning files from download in batch

#3 Post by Ed Dyreen » 11 Apr 2012 10:38

'
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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: remaning files from download in batch

#4 Post by !k » 11 Apr 2012 10:54

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: remaning files from download in batch

#5 Post by foxidrive » 11 Apr 2012 13:13

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

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: remaning files from download in batch

#6 Post by Ed Dyreen » 11 Apr 2012 13:46

'
I agree foxi, loops are made with goto, not with call,
besides the recursion limit, call wastes memory and performance.

Post Reply