Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to create an batch file that will
rename a file on my F: drive
named MyBackup 00001.qic
to MyBackup nnnnn.qic (nnnnn = next available file number).
For example if files exist:
MyBackup 00002.qic
MyBackup 00003.qic
MyBackup 00004.qic
MyBackup 00005.qic
then rename MyBackup 00001.qic to MyBackup 00006.qicYes, the file names always have a space just before the 5 digit number.
Since I am just learning how to write .bat files for my Windows2000 Computers, any recommendations would be greatly appreciated.Thanks so very much, mikeburg

Hi Mike,
Let me know if this works:
@echo off
setlocal enableDelayedExpansion
set I=2
:NextI
if /I %I% LEQ 9999 set II=0%I%
if /I %I% LEQ 999 set II=00%I%
if /I %I% LEQ 99 set II=000%I%
if /I %I% LEQ 9 set II=0000%I%
if not exist "MyBackup !II!.qic" (
ren "MyBackup 00001.qic" "MyBackup !II!.qic"
goto :eof
)
set /a I+=1
if /i %I% LSS 99999 goto NextI
)--
Holla.

Hi Holla,
Looks pretty cool.
=====================================
If at first you don't succeed, you're about average.M2

Thanks M2.
I am honored. :]
I tried to use "for /l %I (1,1,99999)";
then it actually generates 99999 sets of
instructions and then calls it in sequence..
even though I break out of the loop in 4th
iteration. (or something very close to it)...So resorted to simple goto.
:-)--
Holla.

My approach would be to discover last used number, strip leading zeros, increment, then pad to 5 places.
=====================================
If at first you don't succeed, you're about average.M2

This works great! However I need 2 things:
(1) I discovered I was suppose to copy the file & not rename it. (Leave the original file MyBackup 00001.qic & merely copy it creating the new name of the next available numbered file like you did above).
(2) The bat file exists in a folder on my D drive & the backup files exists in F:\backups\. The above bat file works great as long as it is in the same folder with the backup files & I can't always have it there.
Can you show me the bat file with the above modifications?
Sorry for the changes, but this is helping me start learning a lot. mikeburg

Mike,
1. 'Ren' is changed to 'Copy'.
2. 'cd /d f:\backup' inserted in the beginning. This changes the current directory to f:\backup. If you need to execute the same script in some other directory, we have to change the script then. Here is the new script:@echo off setlocal enableDelayedExpansion cd /d f:\backup set I=2 :NextI if /I %I% LEQ 9999 set II=0%I% if /I %I% LEQ 999 set II=00%I% if /I %I% LEQ 99 set II=000%I% if /I %I% LEQ 9 set II=0000%I% if not exist "MyBackup !II!.qic" ( copy "MyBackup 00001.qic" "MyBackup !II!.qic" goto :eof ) set /a I+=1 if /i %I% LSS 99999 goto NextI )--
Holla.

The bat file exists in D:\data\command
&
the backup files exists in F:\backupsWhen I run the bat file, I get 2 errors,
The system cannot find the path specified.
The system cannot find the file specified.So far this is great! If I can resolve the above items, we will be set. mikeburg

Ok, I figured out my problem. The directory should be F:\backups.
Everything works perfect! You guys are great. I am sure glad I found this forum as I look forward to learning from it. Thank you. mikeburg

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |