I'm currently writing a windows batch file which needs to check if a folder with the current computer name exists. If it doesn't, it writes the folder with the name, but if it is already there, it appends a number at the end so that in the end I'd have a folder containing folder names like this


so far, I tried using this piece of code

set usb=G:
SET logdir="%usb%\logs\%computername%"
FOR /L %%F IN (1,1,25) DO (
IF NOT EXIST "%usb%\logs\%computername%-%%F" (
md "%usb%\logs\%computername%-%%F"
SET logdir=%usb%\logs\%computername%-%%F)
goto :eof
ECHO %logdir%

but all I manage to do is exit the loop, not something I want to do.


set dir=G:\logs\%computername%

set n=0
md "%dir%"||call :a %n%

rem rest of your script    


rem the a label is set after the exit function to prevent it from
rem running in the actual script when it's not required.

set /a n+=1
md "%dir%-%n%"||goto a
exit /b

This script will create numbered folders in the way you described.

In batch, the syntax for set is set "foo=bar". Quotes are necessary when variables include syntax breaking characters, like & or |. Spaces do not require quotes when declaring variables. set foo="bar" sets foo to "bar".

| improve this answer | |

Option 1 - Creating All Folders At Once

Based on the starting code in your question, this will produce a folder with an extension (1 through max) if it does not already exist in one call:

SET usb=G:

SET logdir="%usb%\logs"

SET max=25

FOR /L %%F IN (1,1,%max%) DO (
    IF NOT EXIST "%logdir%\%computername%-%%F" ( 
    md "%logdir%\%computername%-%%F")


Example Batch Folder Creation All At Once

Option 2 - Creating Folders Incrementally

As a second option, the solution below will add an incremental file extension once per call (based on the total number of folders in the directory). Likewise, it disregards existing folders:

SET usb=G:

SET logdir="%usb%\logs"

FOR /f %%A IN ('dir /b /ad %logdir%^|find /c /v "" ') DO SET fcount=%%A

SET /a fcount+=1

IF NOT EXIST "%logdir%\%computername%-%%A" (
    MD "%logdir%\%computername%-%fcount%")


Example Batch Folder Creation Once Per Call


  • The first solution will always produce folders 1 through max.

  • The second solution has a weakness in that it uses the total folder count in a directory. So while it will produce similarly named folders with numerical extensions, true ordered sequences with no gaps (i.e. -1,-2,-3,-4...) require:

    • all folder names in a directory need to be the same.
    • any additional folders (e.g. those added manually) keep this sequence (i.e. if computer-6 exists, the next manually added folder must be computer-7).
| improve this answer | |
  • I just need something that will count up to prevent folder overwriting and indicate which folder came first and then second. – Flaver-D Jul 1 '17 at 14:21
  • Both of these solutions should fulfill your needs then. – Anaksunaman Jul 1 '17 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.