Page 1 of 1

Need a neater way to make the next directory

Posted: 01 Oct 2006 20:26
by tph
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:
:: 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.
if exist %SystemDrive%\WindowsPC\erbt\BAR* goto mkhistory1
if not exist %SystemDrive%\WindowsPC\erbt\BAR* goto end
if exist %SystemDrive%\WindowsPC\erbt\history\*.**1 goto mkhistory2
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**1 goto mk1
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

if exist %SystemDrive%\WindowsPC\erbt\history\*.**2 goto mkhistory3
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**2 goto mk2
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

if exist %SystemDrive%\WindowsPC\erbt\history\*.**3 goto mkhistory4
if /I not exist %SystemDrive%\WindowsPC\erbt\history\*.**3 goto mk3
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

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

on and on and on

Posted: 02 Oct 2006 21:20
by DosItHelp

: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.

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

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


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:

What a neater way to make the next directory!

Posted: 03 Oct 2006 18:07
by tph
This works great! and is PERFECT... This trimmed 150 lines down to 8 lines

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

Thank you soooooo much