批处理增加文件夹名称

批处理增加文件夹名称

本文介绍了如何使用 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.

我该怎么做?我在批处理脚本中看到的增加内容的解决方案似乎不适用于我的情况,除了我为自己的代码复制/粘贴的内容之外,我对批处理脚本一无所知.

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 批处理增加文件夹名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:18