Need a neater way to make the next directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tph

Need a neater way to make the next directory

#1 Post by tph » 01 Oct 2006 20:26

I have a batch file that sends files to a history folder in a very long drawn out fashion using if exist do that. if that exist do this. line after line.
I am looking for a way to determine the last folder created *.001 or *.* then make the next folder *.002 out to 50 folders.
I have a name convention for the folders that works well I just need to find a better way for getting from folder *.01 to folder *.050

here is a sample of what I do now:
:mkhistory
::
:: Stores last 10 batch uploads in history folder to provide a way to
:: recover in the event some data is lost and data from guns has already
:: been removed.
::
cls
if exist %SystemDrive%\WindowsPC\erbt\BAR* goto mkhistory1
if not exist %SystemDrive%\WindowsPC\erbt\BAR* goto end
:mkhistory1
cls
if exist %SystemDrive%\WindowsPC\erbt\history\*.**1 goto mkhistory2
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**1 goto mk1
:mk1
cls
MKDIR %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.001
move /y %SystemDrive%\WindowsPC\BAR*.* %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.001\
goto end

:mkhistory2
cls
if exist %SystemDrive%\WindowsPC\erbt\history\*.**2 goto mkhistory3
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**2 goto mk2
:mk2
cls
MKDIR %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.002
move /y %SystemDrive%\WindowsPC\BAR*.* %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.002\
goto end

:mkhistory3
cls
if exist %SystemDrive%\WindowsPC\erbt\history\*.**3 goto mkhistory4
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**3 goto mk3
:mk3
cls
MKDIR %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.003
move /y %SystemDrive%\WindowsPC\BAR*.* %SystemDrive%\WindowsPC\erbt\history\%mm%%dd%%hh%%min%%ss%.003\
goto end

:mkhistory4
cls
if exist %SystemDrive%\WindowsPC\erbt\history\*.**4 goto mkhistory5
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**4 goto mk4
:mk4

on and on and on

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 02 Oct 2006 21:20

tph,

:idea: You can determine the next number to be used like this:

Code: Select all

set folder=%SystemDrive%\WindowsPC\erbt\history

set last=000
for /f "tokens=*" %%a in ('dir /ad /oe /b "%folder%"') do set last=%%~xa
set /a next=1 + 1%last:~-3%
set next=%next:~-3%

echo.The next number to be used is: %next%


:arrow: Detailed description

set last=000
Variable for last number used. Initialized with 000 in case there is no historized directory yet.

dir /ad /oe /b
List all directories sorted by extension (alphabetic).

for /f ... do set last=%%a
Loop through output of dir command and remember the last folder name in 'last' variable.

%last:~-3%
Use only the last 3 digits of the folder name. I.e.:
...\WindowsPC\erbt\history\1002111735.007 -> 007

1%last:~-3%
Put a non 0 digit in front so that 'set /a' command doesn't think it's an octal number. I.e. 007 is handled as octal number, 1007 decimal.

set /a next=1 + 1%last:~-3%
Calculate the next number to be used, i.e. 1008 = 1 + 1007

set next=%next:~-3%
Use last 3 numbers only, this will keep the leading zeros but cut of the extra digit, i.e. 1008 -> 008

DOS IT HELP?

Note: To combat nasty spam you will now need to register in order to post to this forum. Sorry for the inconvenience 8)
Registering is free and easy: http://www.dostips.com/forum2/profile.php?mode=register

tph
Posts: 1
Joined: 03 Oct 2006 17:53

What a neater way to make the next directory!

#3 Post by tph » 03 Oct 2006 18:07

This works great! and is PERFECT... This trimmed 150 lines down to 8 lines
YOU ARE A GOD!

was getting
The next number to be used is: 102
had to change....
set /a next=1 + 1%last:~-3%
to
set /a next=1 + 1%last:~-4%
The next number to be used is: 002

Thank you soooooo much

Post Reply