根据名称相同的现有文件夹制作编号递增的文件夹 [英] Making a folder with incrementing number depending on existing folders with same name

查看:70
本文介绍了根据名称相同的现有文件夹制作编号递增的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个批处理文件,当已经存在一个具有相同名称的文件夹时,该文件夹将以递增的数字创建一个文件夹.

例如,一个名为 folder 的文件夹,如下所示:

 如果存在文件夹md文件夹1如果存在文件夹md文件夹2 

我想要的是在已经有一个或多个同名文件夹(folder1,folder2,...)时,使另一个文件夹的末尾编号递增,并使代码更短.

解决方案

此批处理代码基于您的最初想法:

  @echo关闭设置文件夹= C:\ Temp \测试"如果不存在,则为%Folder%"(md%Folder%"转到EndBatch)为(1 1 65534)中的/L %% N做(如果不存在,则为%Folder %%% N"(md%Folder %%% N"转到EndBatch)):结束批处理设置文件夹= 

但是这种解决方案非常慢,因为已经存在多个文件夹.

以下批处理文件的速度更快:

  @echo关闭设置文件夹= C:\ Temp \测试"如果不存在,则为%Folder%"(md%Folder%"转到EndBatch)设置"FolderCount = 0"为/F"delims =" %% F in('dir/AD/B%Folder%*")设置/A FolderCount + = 1md%Folder %% FolderCount%"设置"FolderCount =:结束批处理设置文件夹= 

但是此批处理文件并没有真正检查文件夹名称.它仅计算以相同字符串开头的文件夹的数量,并期望对于新文件夹,可以使用下一个数字.这可能是错误的假设,例如删除了 C:\ Temp \ Test1 并删除了 C:\ Temp \ Test C:\ Temp \ Test2 仍然存在.在这种情况下,上面的批处理文件仍尝试创建 C:\ Temp \ Test2 .

因此,一个更好的批处理文件应为:

  @echo关闭setlocal EnableDelayedExpansion设置"ParentFolder = C:\ Temp"设置"FolderName = Test"如果不存在,则为%ParentFolder%\%FolderName%"(md%ParentFolder%\%FolderName%") 别的 (设置最高编号= 0"为/F"delims =" %% F in('dir/AD/B%ParentFolder%\%FolderName%*"')执行(设置"NameFolder = %%〜F"设置"FolderNumber =!NameFolder:%FolderName%=!"如果!FolderNumber!GTR!最高人数!设置最高编号=!文件夹编号!")设置/A HighestNumber + = 1md%ParentFolder%\%FolderName%!HighestNumber!")本地 

这也是快速的,因为也不会检查每个文件夹的存在.但是,它实际上会找出以相同字符串开头的所有文件夹中最多的数目,并使用下一个数字创建一个新文件夹.

注释1:此批代码也不是100%故障安全的.例如,对于此批处理文件,文件夹 C:\ Temp \ Test_New_15 可能是个问题,只希望 C:\ Temp \ Test ,C:\ Temp \ Test1 ,C:\ Temp \ Test2 ,...被发现.( Test_New_15 将被忽略,但是类似的问题可能会成为问题.)当然有可能消除此问题,例如,在 dir 来检查在 FolderName 定义的字符串之后是否附加了数字.

注释2:带有前导 0 的文件夹号被解释为八进制数.如果存在在 if 条件之前值为 FolderNumber 的1个或多个前导 0 的文件夹,则有必要删除前导零.>

要了解3个批处理文件中的代码,请打开命令提示符窗口,执行以下命令,并在窗口中读取每个命令的帮助输出.

  1. dir/?
  2. 用于/?
  3. 转到/?
  4. 如果/?
  5. md/?
  6. 设置/?
  7. setlocal/?

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.

解决方案

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 /?

这篇关于根据名称相同的现有文件夹制作编号递增的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆