Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 470 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I increment a folder name using Windows batch?

#1
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.
Reply

#2
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!
Reply

#3
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%"
Reply

#4
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%

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through