DOS Batch If File Exists Rename Sequentially

August 19, 2011 at 08:49:57
Specs: Windows XP/2003
Hello all,
I am creating a DOS Batch file to process large collections of archive files. The batch un-archives each file and re-archives each file individually to zip format. Once I have a zip I want to move them to a destination directory. Before the move occurs I'd like to test if that exact zip file name exists. If it exists, I will need to increment the filename sequentially and then test again. I've found several examples of sequentially incrementing file names in a for loop, but not one that will allow me to test the filename again (2nd, 3rd, 4th, etc times) and increase the filename sequentially to come up with a unique filename.

Any ideas? I'm reluctant to post broken code but I can show you what I've got if you need to see more. Many thanks!


See More: DOS Batch If File Exists Rename Sequentially

Report •

#1
August 19, 2011 at 16:16:38
You can use a batch file to organize files and folders, but I don't think it is possible for a batch file to open and move contents of a zip file. You will most likely have to put the files into a folder to even get started on the code.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#2
August 19, 2011 at 16:52:11
I'm using 7-Zip (open source file archiver) to handle that functionality in my batch file.

The area where I need help is determining whether a newly zipped "filename.zip" file is unique before attempting to move it into a "c:\files" folder. Over time there may be several files in that folder like "filename.zip", "filename1.zip", 'filename2.zip". And at some point in the future when I attempt to move another "filename.zip" file into that directory, I need the script to determine what name to give it in sequential order before moving it there. The reason I'm doing this is to gather a large number of files in one place before determining whether to keep them or delete. I've thought about using a filename_20110819_194905.zip (as it's very unlikely I'd have duplicate names going down to HHMMSS but I'd prefer to do it sequentially if possible. :)

I'd like to keep the task of renaming and moving files within the realm of available batch file commands. I also don't mind calling a second batch file if that's what's needed.

Thanks!


Report •

#3
August 19, 2011 at 17:51:10
You could use something like this:

:LOOP
SET NUM=%RANDOM%
IF /I %NUM% GTR 100 GOTO LOOP
IF /I %NUM% LSS 0 GOTO LOOP
IF %ERRORLEVEL%==1 GOTO RENAME | MOVE FILENAME.ZIP C:\FILES
:RENAME
RENAME "FILENAME.ZIP" "FILENAME%NUM%.ZIP"

The program will automatically come up with a number from 0 to 100, then if "FILENAME.ZIP" already exists when moving, it will rename the file "FILENAME(NUMBER CAME UP WITH).ZIP". It might be a little slow, but try it out.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Related Solutions

#4
August 19, 2011 at 22:50:21
That's not a bad idea but I think whether I try to use your recommendation or run with mine, I have the same issue. Here's a piece of my code:

. . .
:test
if not exist !D_DIR!\!cname!.zip (
move !cname!.zip !D_DIR!
cd ..
rd !vname! /s/q ) else (
set /a n=n+1
SET pname=!cname!
SET cname=!vname!_!n!
ren !pname!.zip !cname!.zip
goto :test
)

This code starts where the new zip file is created. I test to see if it exists in the destination folder - if it doesn't I move the zip file there. I do some cleanup work and then using a FOR statement I grab the next file to process. When/If I hit a file that does exist in the destination folder, the ELSE code above gets triggered. This code does rename FILENAME.ZIP to FILENAME1.ZIP, and so on until it finds a unique sequential filename# as I want. Unfortunately once the ELSE code is triggered, the batch file QUITS after processing this one file. I am not sure how to tell it to return to the FOR command and grab the next file to process.

I think my problem has to do with using goto within a FOR statement, but I'm not sure how else to do this. Can a CALL to a batch file be used instead? What would that syntax look like?

Many thanks!


Report •

#5
August 20, 2011 at 08:33:20
You could restart the program again by using this:

START "BATCHFILENAME.BAT"
EXIT

Try replacing the "goto :test" line with the above 2 lines. Leave the quotes around BATCHFILENAME, or the program won't work right.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#6
August 20, 2011 at 12:46:56
Eureka! After a bit more poking around I came up with a solution. Here's the code if anyone is curious:

if !proc!==y (
pushd !S_DIR!
md !vname!
!W_DIR!\7z.exe x !vname!!vext! -o!vname!
cd !vname!
!W_DIR!\7z.exe a -y -r !vname!.zip !W_DIR!\!ADDFILE! *.* -o!D_DIR!

call :testmove

)

)
goto :EOF

:testmove
if not exist !D_DIR!\!cname!.zip (
REM echo if safe to copy to destination folder...
move !cname!.zip "!D_DIR!"
cd ..
rd !vname! /s/q ) else (
REM echo if NOT safe to copy to destination folder...
set /a n+=1
SET pname=!cname!
SET cname=!vname!_!n!
REM ECHO PREV:!pname! CURR:!cname! VNAME:!vname!
ren !pname!.zip !cname!.zip
GOTO :testmove
)
Echo.
Echo Conversion and moves complete^^! Review Source+Destination Directory contents.
Echo.
goto :end


Report •

#7
August 20, 2011 at 13:17:09
Glad you were able to solve it :).

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Ask Question