5

I've got a batch script that creates a folder named New_Folder and a few subdirectories and files within. Currently, if I need to create multiple New_Folders I have to rename each New_Folder created by the batch before I can run it again and create a new one. What I'd like to do is have the batch check and see if New_Folder already exists, and if so, to increment New_Folder by a number. So I'd have New_Folder, New_Folder1, New_Folder2, and so on.

How would I go about doing this? The solutions I've seen for incrementing things in batch scripts don't seem to apply to my situation, and I don't know anything about batch scripting beyond what I've copy/pasted for my own code.

ajw-art
  • 173
  • 1
  • 1
  • 12

3 Answers3

8

Here is a solution that will always work, even if there are gaps in the numbers. The folder number will always be 1 greater than the current max number.

@echo off
setlocal enableDelayedExpansion
set "baseName=New_Folder"
set "n=0"
for /f "delims=" %%F in (
  '2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
  set "name=%%F"
  set "name=!name:*%baseName%=!"
  if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
dbenham
  • 127,446
  • 28
  • 251
  • 390
3

with that you will be able to count the number of occurence of "New_Folder*" and create one with the next number.

@echo off
set /a count=0

for /d %%d in (New_Folder*) do (
    set /a count+=1
)

set /a count+=1

mkdir New_Folder%count%

Note that the best way would be to find the largest number at the end of New_Folder, but Windows Batch is very limitative and I'm a Linux guy!

EDIT : After about one hour of googling and testing :

@echo off

setlocal EnableDelayedExpansion

set max_number=0

for /d %%d in (New_Folder*) do (
    set current_directory=%%~nxd

    call:StrLength name_length !current_directory!
    call:Substring directory_number,!current_directory!,10,!name_length!

    if !directory_number! gtr !max_number! (
        set max_number=!directory_number!
    )
)

set /a max_number+=1

mkdir New_Folder%max_number%

:Substring
::Substring(retVal,string,startIndex,length)
:: extracts the substring from string starting at startIndex for the specified length 
 SET string=%2%
 SET startIndex=%3%
 SET length=%4%

 if "%4" == "0" goto :noLength
 CALL SET _substring=%%string:~%startIndex%,%length%%%
 goto :substringResult
 :noLength
 CALL SET _substring=%%string:~%startIndex%%%
 :substringResult
 set "%~1=%_substring%"
GOTO :EOF

:StrLength
::StrLength(retVal,string)
::returns the length of the string specified in %2 and stores it in %1
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
::echo the string is %length% characters long!
set "%~1=%length%"
GOTO :EOF

Note, the command line return me an error "The syntax of the command is incorrect." but everything works so I'll let you find why... New folder is created regardless of the order of directories or if they start at 1 or not :) Hope you'll enjoy!

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • I agree with your last sentence. Your existing solution can fail if there are gaps in the numbering. – dbenham Nov 11 '12 at 05:43
  • Thanks so much! I ended going with dbenham's solution because the code was more concise, but I appreciate all of the comments you put into yours: like I said, I've never really used batch scripting before, so it's nice to see some of the logic behind it! – ajw-art Nov 11 '12 at 06:39
2

This solution find the largest numbered name, and create the next one to it:

@echo off
for /d %%d in (New_Folder*) do set lastFolder=%%d
set /A nextFolder=%lastFolder:*New_Folder=% + 1
mkdir New_Folder%nextFolder%

EDIT: Previous solution doesn't correctly get the last numbered folder, but the next one is correct:

@echo off
setlocal EnableDelayedExpansion
set lastFolder=0
for /d %%d in (New_Folder*) do (
   set folder=%%d
   set folder=!folder:New_Folder=!
   if not defined folder set folder=0
   if !folder! gtr !lastFolder! set lastFolder=!folder!
)
set /A nextFolder=lastFolder+1
mkdir New_folder%nextFolder%
Aacini
  • 65,180
  • 12
  • 72
  • 108