Making a folder with incrementing number depending on existing folders with same name

我是研究僧i 提交于 2021-01-28 05:01:29

问题


I want to make a batch file which creates a folder with an incremented number when there is one already existing with the same name.

For example for a folder called folder something like this:

if exist folder md folder1 if exist folder1 md folder2

What I want is to make another folder with an incremented number at the end when there are already 1 or more folders with the same name (folder1, folder2, ...) and make the code shorter.


回答1:


This batch code is based on your initial idea:

@echo off
set "Folder=C:\Temp\Test"
if not exist "%Folder%" (
    md "%Folder%"
    goto EndBatch
)
for /L %%N in (1 1 65534) do (
    if not exist "%Folder%%%N" (
        md "%Folder%%%N"
        goto EndBatch
    )
)
:EndBatch
set "Folder="

But this solution is VERY slow with several folders already existing.

Much faster is following batch file:

@echo off
set "Folder=C:\Temp\Test"
if not exist "%Folder%" (
    md "%Folder%"
    goto EndBatch
)
set "FolderCount=0"
for /F "delims=" %%F in ('dir /AD /B "%Folder%*"') do set /A FolderCount+=1
md "%Folder%%FolderCount%"
set "FolderCount="
:EndBatch
set "Folder="

But this batch file does not really check the folder names. It just counts the number of folders starting with same string and expects that for new folder the next number can be used. This could be a wrong assumption for example with C:\Temp\Test1 deleted and C:\Temp\Test and C:\Temp\Test2 still existing. The batch file above tries to create nevertheless C:\Temp\Test2 in this case.

A better batch file would be therefore this one:

@echo off
setlocal EnableDelayedExpansion
set "ParentFolder=C:\Temp"
set "FolderName=Test"
if not exist "%ParentFolder%\%FolderName%" (
    md "%ParentFolder%\%FolderName%"
) else (
    set "HighestNumber=0"
    for /F "delims=" %%F in ('dir /AD /B "%ParentFolder%\%FolderName%*"') do (
        set "NameFolder=%%~F"
        set "FolderNumber=!NameFolder:%FolderName%=!"
        if !FolderNumber! GTR !HighestNumber! set "HighestNumber=!FolderNumber!"
    )
    set /A HighestNumber+=1
    md "%ParentFolder%\%FolderName%!HighestNumber!"
)
endlocal

It is also fast because also not checking existence of each folder. But it really finds out what is the highest number of all folders starting with same string and creates a new folder with next number.

Note 1: This batch code is also not 100% fail safe. For example a folder C:\Temp\Test_New_15 could be a problem for this batch file expecting only C:\Temp\Test, C:\Temp\Test1, C:\Temp\Test2, ... being found. (Test_New_15 is ignored, but something similar could be a problem.) It would be of course possible to eliminate this problem for example with using additionally findstr on every line returned by dir to check if after string defined by FolderName nothing else than a number is appended.

Note 2: A folder number with a leading 0 is interpreted as octal number. It would be necessary to remove leading zeros if there are folders with 1 or more leading 0 on value of FolderNumber before the if condition.

To understand the codes in the 3 batch files, open a command prompt window, execute the following commands, and read help output in the window for each command.

  1. dir /?
  2. for /?
  3. goto /?
  4. if /?
  5. md /?
  6. set /?
  7. setlocal /?



回答2:


There are better/faster ways of creating increasing folder names (and some of them are at Mofi`s answer!), but as the indicated criteria is short code, here is an alternative (but as Mofi indicates, this can be a slow solution)

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "folderName=test"

    2>nul (md "%folderName%"||cmd /q /c"for /l %%a in (1 1 100000) do md "%folderName%%%a"&&exit %%a")
    if errorlevel 1 set "folderName=%folderName%%errorlevel%"

    echo Created %folderName%


来源:https://stackoverflow.com/questions/28781921/making-a-folder-with-incrementing-number-depending-on-existing-folders-with-same

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!