Incrementing file version numbers with batch file

杀马特。学长 韩版系。学妹 提交于 2020-01-25 23:01:27

问题


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

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

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.

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.


回答1:


This works here.

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


来源:https://stackoverflow.com/questions/17320090/incrementing-file-version-numbers-with-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!