Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如果两个文件夹名称使用bat匹配,则创建一个新名称_Batch File - Fatal编程技术网

Batch file 如果两个文件夹名称使用bat匹配,则创建一个新名称

Batch file 如果两个文件夹名称使用bat匹配,则创建一个新名称,batch-file,Batch File,假设新文件夹位于我的当前目录路径中 bat文件正试图在同一路径中创建另一个新文件夹 如何在创建时自动将第二个文件夹重命名为新文件夹1?如果存在,可以使用 if exist %foldername% set foldername=%foldername%1 这可以通过一个简单的循环变得更加健壮 尽管如此,如果您有多个同名文件夹,它最终将看起来像“新建文件夹”、“新建文件夹1”、“新建文件夹11”。使用一个单独的数字变量非常容易。逐步文件。根据需要进行调整 @echo off setlo

假设
新文件夹
位于我的当前目录路径中

bat文件正试图在同一路径中创建另一个
新文件夹


如何在创建时自动将第二个文件夹重命名为
新文件夹1

如果存在,可以使用

if exist %foldername% set foldername=%foldername%1
这可以通过一个简单的循环变得更加健壮


尽管如此,如果您有多个同名文件夹,它最终将看起来像
“新建文件夹”、“新建文件夹1”、“新建文件夹11”
。使用一个单独的数字变量非常容易。

逐步文件。根据需要进行调整

@echo off

    setlocal enableextensions

    rem How to search and name new folders  
    set "baseName=New Folder"

    rem Search the folder with baseName and greater number.
    rem Dir is generated in descending order, so the first folder is the greatest.
    set "lastFolder="
    for /f "tokens=*" %%d in ('dir /ad /o-n /b "%cd%\%baseName%*." 2^>nul ^| findstr /b /e /r /c:"%baseName%[0-9]*"') do (
        set "lastFolder=%%~nd"
        goto endSearch
    )
:endSearch

    rem Test if we found a folder
    if defined lastFolder (

        rem Folder found, get its number by eliminating the baseName part of the name
        set "lastFolderNumber=!lastFolder:%baseName%=!"

        if not defined lastFolderNumber (
            rem if it has no number, next folder should be 1
            set "lastFolderNumber=1"
        ) else (
            rem number found, add 1
            set /a lastFolderNumber+=1
        )

    ) else (
        rem No folder exist. This is the first. So no numeration
        set "lastFolderNumber="
    )

    rem create the folder
    echo Creating: "%cd%\%baseName%%lastFolderNumber%"
    mkdir "%cd%\%baseName%%lastFolderNumber%"

:endProcess
    rem clean vars
    endlocal