Computing.Net > Forums > Programming > BAT File to Increment File Name +1

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 File to Increment File Name +1

Reply to Message Icon

Name: Igor911
Date: November 19, 2008 at 07:41:18 Pacific
OS: Windows
CPU/Ram: P4
Product: Dell
Comment:

Hello,

I am in need of a DOS BAT file to read in the current .txt file name of a file name that starts with "CD0001" and rename it to CD0002 and tag along the date stamp of MMDDYY.

I have the date stamp portion working:
%date:~-10,2%%date:~-7,2%%date:~-2,2%

However, I need this BAT file to somehow loop through and read in the current TXT file name that begins with "CD" and a 4 digit # and increment it by 1.

The example file name is "CD0001.txt" which would need to be "CD0002.txt" after the BAT ran...and so forth.

Thanks for your help!




Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: November 19, 2008 at 08:22:20 Pacific
Reply:

"%date:~-10,2%%date:~-7,2%%date:~-2,2%"

There's no built-in DATE in DOS; nor is there any substring handler, so I suppose you're using NT.

For your CD#### file, do you need the bat to "figure out the last number used"?


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

M2


0

Response Number 2
Name: Igor911
Date: November 19, 2008 at 08:49:23 Pacific
Reply:

Yes, I would need the BAT file to figure out the current file #.

Correct, I'm running this bat file on Windows XP.

Thank you so much!


0

Response Number 3
Name: Mechanix2Go
Date: November 19, 2008 at 10:31:25 Pacific
Reply:

Note that this does not check to see if the last 4 chars of filename are numbers. So if they are anything else, it'll be out in the ozone.

=======================================


@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/on CD????.txt') do (
set str=%%~Na
)

set lastnum=!str:~-4!
echo last num used is !lastnum!
call :sub1
set /a N=lastnum+=1
call :sub2
echo next 4 digit num is !S!

goto :eof

:sub1 unpad
if !lastnum:~0^,1! equ 0 set lastnum=!lastnum:~1^,3!& goto :sub1
goto :eof

:sub2 pad to 4 places
set S=!N!
:pad
if !S:~3^,1!'==' set S=0!S!& goto :pad
goto :eof

============================================================


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

M2


0

Response Number 4
Name: Igor911
Date: November 19, 2008 at 11:09:19 Pacific
Reply:

Thank you so much, but I need that also renaming the file to the next interval #.

The file name will be CD0001 111808.txt (with the last portion as todays date)

Now if this is ran, I need it to rename the current file if it matches todays date (111808) to CD0002 111808.txt

Please help as I have no idea how to do this!

Thanks!


0

Response Number 5
Name: Igor911
Date: November 19, 2008 at 11:27:10 Pacific
Reply:

I think I got it working

@echo off & setLocal EnableDelayedExpansion

set currDate=%date:~-10,2%%date:~-7,2%%date:~-2,2%

for /f "tokens=* delims= " %%a in ('dir/b/on CD????!currDate!.txt') do (
set str=%%~Na
)

echo !currDate!
set lastnum=!str:~-10,4!
echo last num used is !lastnum!
call :sub1
set /a N=lastnum+=1
call :sub2
echo next 4 digit num is !S!

echo CD!S!.txt

REN !str!.txt CD!s!!currDate!.txt


goto :eof

:sub1 unpad
if !lastnum:~0^,1! equ 0 set lastnum=!lastnum:~1^,3!& goto :sub1
goto :eof

:sub2 pad to 4 places
set S=!N!
:pad
if !S:~3^,1!'==' set S=0!S!& goto :pad
goto :eof


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: November 19, 2008 at 11:34:00 Pacific
Reply:

You lost me on that one.

You said earlier you had the date.

What I wrote gets you an incremented number.


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

M2


0

Response Number 7
Name: Igor911
Date: November 19, 2008 at 11:59:57 Pacific
Reply:

Thank you so much for your help, I have it working but I need more features to this little experiment.

What I have it doing now is finding the current file with todays date (i.e. "STERTD0001111908") and incrementing the 4 digit # in the middle by 1 to (i.e. "STERTD0002111908")

I also have it writing out the NEW INCREMENTED 4 digit # and the current date to a file called nextVal.txt

Here is what needs to happen, in the morning when this runs, the BAT file needs to look for a file name called "newFile.txt". It then needs to take that file, rename it to STERTD0001 + todays date MMddyy.

Now here's the funky part...IF the BAT has been ran with todays date, it then needs to increment the file (i.e. STERTD0002 + todays date MMddyy)

This BAT file will be ran more than once per day with the same input file ("newFile.txt"). What the BAT file needs to do is check the nextVal.txt file and see if it was ran today, and if so, get the most current incrementation then rename the newFile.txt to STERTD + new incrementation + todays date.txt

I figured an easy way to do this would be to write out the incremented # and date out to the file nextVal.txt then somehow check it.

Please help, it is greatly appreciated!

@echo off & setLocal EnableDelayedExpansion

set currDate=%date:~-10,2%%date:~-7,2%%date:~-2,2%

for /f "tokens=* delims= " %%a in ('dir/b/on STERTD????!currDate!.txt') do (
set str=%%~Na
)

echo !currDate!
set lastnum=!str:~-10,4!
echo last num used is !lastnum!
call :sub1
set /a N=lastnum+=1
call :sub2
echo next 4 digit num is !S!

echo !s!!currDate! > nextVal.txt

REN !str!.txt STERTD!s!!currDate!.txt

goto :eof

:sub1 unpad
if !lastnum:~0^,1! equ 0 set lastnum=!lastnum:~1^,3!& goto :sub1
goto :eof

:sub2 pad to 4 places
set S=!N!
:pad
if !S:~3^,1!'==' set S=0!S!& goto :pad
goto :eof


0

Response Number 8
Name: Mechanix2Go
Date: November 19, 2008 at 18:48:06 Pacific
Reply:

Looks like you just about got it. I dont immediately see what's lacking.

Comment: using substrings of %DATE% is fraught will peril. It will work while it works but may go horribly wrong; usualy at the most inconvenient moment.

Note also that a date layout of mmddyy is quaint, but finally nesrly useess.

A far better choice is yyyymmdd.

The script below will get an appropriate YYYYMMDD from BIOS,
regardless of date layout, language, version or any other gotcha factors.
You ca incorporate it as a subroutine.

=============================


@echo off & setLocal EnableDelayedExpansion

call :YMD8
set currdate=!year!!month!!dat!
echo currdate is: !currdate!

goto :eof

:YMD8

@echo off > d.d

>> d.d echo E 0100 B4 2A CD 21 B4 4C CD 21
>> d.d echo N DAY.COM
>> d.d echo RCX
>> d.d echo 8
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 F0 B4 4C CD 21
>> d.d echo N MONTH.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 89 C8 B4 4C CD 21
>> d.d echo N YEAR.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo E 0100 B4 2A CD 21 88 D0 B4 4C CD 21
>> d.d echo N DAT.COM
>> d.d echo RCX
>> d.d echo A
>> d.d echo W
>> d.d echo Q

debug < d.d > nul
del d.d

dat
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set dat=0%%a
for %%a in ( 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31) do if errorlevel %%a set dat=%%a

day
if errorlevel 0 set day=Sunday
if errorlevel 1 set day=Monday
if errorlevel 2 set day=Tuesday
if errorlevel 3 set day=Wednesday
if errorlevel 4 set day=Thursday
if errorlevel 5 set day=Friday
if errorlevel 6 set day=Saturday

month
for %%a in (1 2 3 4 5 6 7 8 9 ) do if errorlevel %%a set month=0%%a
for %%a in ( 10 11 12) do if errorlevel %%a set month=%%a

year
if errorlevel 215 set Year=2007
if errorlevel 216 set Year=2008
if errorlevel 217 set Year=2009
if errorlevel 218 set Year=2010

del day.com
del month.com
del year.com
del dat.com
::== DONE

goto :eof


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

M2


0

Response Number 9
Name: lee123abc
Date: November 20, 2008 at 05:17:11 Pacific
Reply:

Mechanix, you are a legend


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 File to Increment File Name +1

Bat + reg = 1 bat file help!!! www.computing.net/answers/programming/bat-reg-1-bat-file-help/9468.html

Bat file to check for file with current date www.computing.net/answers/programming/bat-file-to-check-for-file-with-current-date/18823.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