We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


.BAT File  -is DOS Smart enough to  INCREMENT Filenames?!?

aniston asked
Medium Priority
Last Modified: 2013-12-29
I would like to create a bat file that would look inside a directory and pkzip all *.txt files into a zip file called file1.zip.  
However if a pkzip file with that name already exists i want the pkzip file name to increment by 1; so in this case it would be file2.zip.

Is DOS smart enough to know how to do this?
Watch Question

I wouldn't count on it :-)  Perhaps you could do it in QBasic?


I remember doing QBasic some 15 years ago so i have no idea how to even begin.
If it is possible in Qbasic could you provide the code on how it can seamlessly work with DOS through a bat file (or any file that dos can run -- like a bat file or exe).


The following is crude, but it works.

@echo off
dir *.txt /s/v/b/on > list
if not exist file1.zip goto noone
for %%c in (1 2 3 4 5 6) do if exist file%%c.zip goto %%c
rem if not exist file2.zip goto 2
rem if not exist file3.zip goto 3
rem if not exist file4.zip goto 4
rem if not exist file5.zip goto 5
d:pkzip -p file1.zip @list
goto end
d:pkzip -p file2.zip @list
goto end
d:pkzip -p file3.zip @list
goto end
d:pkzip -p file4.zip @list
goto end
d:pkzip -p file5.zip @list
goto end
d:pkzip -p file6.zip @list
goto end
d:pkzip -p file7.zip @list
goto end
del list

I use this to backup all the txt files on my harddrive.  I generally only keep three of the previous zip files, but I took it to 6 just in case I forgot to delete them for a while.  I had to use this method because MS-DOS cannot do a concatenated math function within an IF statement.
Unlock this solution and get a sample of our free trial.
(No credit card required)
Either of those batch files look good to me, as long as you don't need unlimited levels of files.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.