Bat to rename 1 file to next avail

February 15, 2009 at 14:55:11
Specs: Windows 2000
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.qic

Yes, 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


See More: Bat to rename 1 file to next avail

Report •


#1
February 15, 2009 at 22:43:22
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.


Report •

#2
February 16, 2009 at 01:35:10
Hi Holla,

Looks pretty cool.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#3
February 16, 2009 at 02:19:20
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.


Report •

Related Solutions

#4
February 16, 2009 at 04:24:56
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


Report •

#5
February 16, 2009 at 08:34:49
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


Report •

#6
February 16, 2009 at 09:35:53
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.


Report •

#7
February 16, 2009 at 11:41:26
The bat file exists in D:\data\command
&
the backup files exists in F:\backups

When 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


Report •

#8
February 16, 2009 at 18:07:37
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


Report •


Ask Question