Computing.Net > Forums > Programming > Bat to rename 1 file to next avail

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Bat to rename 1 file to next avail

Reply to Message Icon

Name: mikeburg
Date: February 15, 2009 at 14:55:11 Pacific
OS: Windows 2000
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Holla
Date: February 15, 2009 at 22:43:22 Pacific
Reply:

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.


0

Response Number 2
Name: Mechanix2Go
Date: February 16, 2009 at 01:35:10 Pacific
Reply:

Hi Holla,

Looks pretty cool.


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

M2


0

Response Number 3
Name: Holla
Date: February 16, 2009 at 02:19:20 Pacific
Reply:

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.


0

Response Number 4
Name: Mechanix2Go
Date: February 16, 2009 at 04:24:56 Pacific
Reply:

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


0

Response Number 5
Name: mikeburg
Date: February 16, 2009 at 08:34:49 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Holla
Date: February 16, 2009 at 09:35:53 Pacific
Reply:

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.


0

Response Number 7
Name: mikeburg
Date: February 16, 2009 at 11:41:26 Pacific
Reply:

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


0

Response Number 8
Name: mikeburg
Date: February 16, 2009 at 18:07:37 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Bat to rename 1 file to next avail

BAT File to Increment File Name +1 www.computing.net/answers/programming/bat-file-to-increment-file-name-1/17272.html

a bat file to search & move more than 1 file www.computing.net/answers/programming/a-bat-file-to-search-move-more-than-1-file/19098.html

File rename in CMD or Bat script www.computing.net/answers/programming/file-rename-in-cmd-or-bat-script/14672.html