batch file on win xp  | |
September 24th, 2003, 10:47 AM
|
#1 (permalink)
| | Junior Member
Join Date: Sep 2003
Posts: 4
|
Hi there,
Currently, I have a batch file for running a simple backup. The backup directory is set using the name of the directory being backed up plus the current date -
set dd=%date:~0,2%
set mm=%date:~3,2%
set yyyy=%date:~6,4%
set backupdir=c:\tempBAK%yyyy%%mm%%dd%
I need to modify the script so it checks to see if the directory already exists, and if so, it should append "-1" to the backupdir.
It then needs to check again to see if this new backupdir already exists, if so it should change the appended "-1" to "-2", and so on.
I have tried a few things but not making any real progress, and would very much appreciate any help anyone can give.
TIA
nick
Last edited by nnichols : September 24th, 2003 at 10:50 AM.
|
| |
September 24th, 2003, 11:09 AM
|
#2 (permalink)
| | Retired mostly.
Join Date: Oct 2001 Location: Finland
Posts: 5,144
|
I can't test it, but I suppose it should work like this.
backup.bat
set dd=%date:~0,2%
set mm=%date:~3,2%
set yyyy=%date:~6,4%
set backupdir=c:\tempBAK%yyyy%%mm%%dd%
if exist %backupdir% (set backupdir=c:\tempBAK%yyyy%%mm%%dd%-1) else <here whatever it normally does>
now I don't know if that works like the way you want it to, but that should give you a good start 
-M |
| |
September 24th, 2003, 11:16 AM
|
#3 (permalink)
| | Junior Member
Join Date: Sep 2003
Posts: 4
|
Cheers,
I've already got that much, it's the next bit I'm struggling with.
Instead of appending "-1" it needs to be appending "-%i%" where "i" is an incremented number. That way I can check to see if it exists, if so grab the next value for "i" and try again until it does not exist, at which point the script can continue.
Thanks for your thoughts anyway
Nick |
| |
September 24th, 2003, 11:24 AM
|
#4 (permalink)
| | Retired mostly.
Join Date: Oct 2001 Location: Finland
Posts: 5,144
|
Uh, I don't know if that can be done with normal dos commands 
I'm sure with vbs it would be a 2minute job
See 'help if' in command prompt for insight on if.
-M |
| |
September 24th, 2003, 11:36 AM
|
#5 (permalink)
| | Junior Member
Join Date: Sep 2003
Posts: 4
|
OK. I've now got this far, but my increment of "%i%" is not working - Code: @echo off
REM setup backup directory name
set dd=%date:~0,2%
set mm=%date:~3,2%
set yyyy=%date:~6,4%
set backupdir=c:\tempBAK%yyyy%%mm%%dd%
set backupdir1=%backupdir%
set i=0
REM check to see if directory already exists
:checkdir
if not exist %backupdir1% goto continue
set a/ i=%i% + 1
echo i: %i%
set backupdir1=%backupdir%-%i%
goto checkdir
:continue
echo Backup directory: %backupdir1% Any thoughts as to the problem with "i" not being incremented? |
| |
September 24th, 2003, 11:45 AM
|
#6 (permalink)
| | Junior Member
Join Date: Sep 2003
Posts: 4
|
I feel such a pratt.
Simple typo - "a/" should have been "/a".  |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |