使用批处理文件增加文件版本号 [英] Incrementing file version numbers with batch file
问题描述
I am trying to do something nearly identical to this: How do I increment a folder name using Windows batch?
基本上,我想创建一个批处理文件以扫描目录,确定最高版本号,然后依次创建下一个.因此,如果目录包含:
Essentially, I want to create a batch file to scan through a directory, determine the highest version number, and then create the next one in sequence. So if the directory contains:
New folder V1.0
New folder V1.1
New folder V1.3
我希望批处理文件添加一个新文件夹V1.4.应该是可行的.问题是我找到了脚本:
I want the batch file to add a New folder V1.4. Should be doable. The problem is that the script I found:
@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%"
不适用于名称为IE的带有空格的文件夹,它不能用于"New_Folder",而不能用于"New Folder".我尝试用^和的各种排列来转义空格,例如,
doesn't work with folders with spaces in the name IE it works with 'New_Folder' but not 'New Folder'. I've tried escaping the spaces with various permutations of ^ and ", for example,
set "baseName=New^ Folder"
set "baseName=New" "Folder"
set "baseName=New""^ ""Folder"
等等,但是,我还没有开始工作.
and so forth, however, I haven't gotten it to work.
我知道我可以通过更改文件名以使用下划线来解决问题,但是在这一点上,我想知道为什么不起作用以及如何解决./p>
I am aware that I could solve the problem by changing my file names to use underscores, but at this point, I want to know why it doesn't work and how to fix it.
推荐答案
在这里有效.
@echo off
setlocal enableDelayedExpansion
set "baseName=New Folder V1."
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*"^|findstr /xri /c:"%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%"
这篇关于使用批处理文件增加文件版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!