我如何增加使用Windows批处理文件夹名称? [英] How do I increment a folder name using Windows batch?

查看:134
本文介绍了我如何增加使用Windows批处理文件夹名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个名为New_Folder并在几个子目录和文件文件夹中的批处理脚本。目前,如果我需要创建多个New_Folders我不得不重新命名批处理创建之前,我可以再次运行它,并创建一个新的每个New_Folder。我想要做的是有批量检查,看看是否New_Folder已经存在,如果是这样,由多个递增New_Folder。所以我有New_Folder,New_Folder1,New_Folder2,等等。

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.

我将如何去这样做呢?我似乎已经看到了在增量批处理脚本的东西解决方案并不适用于我的情况,我不知道有关批处理脚本任何超出我所复制/粘贴为自己的code。

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.

推荐答案

下面是一个解决方案,将一直工作,即使有在数字差距。文件夹数量总是比当前的最大数量大1。

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%"

这篇关于我如何增加使用Windows批处理文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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