需要有关Windows批处理脚本的帮助,该脚本应根据部分文件名设置var的val

需要有关Windows批处理脚本的帮助,该脚本应根据部分文件名设置var的val,windows,string,language-features,batch-file,Windows,String,Language Features,Batch File,这不是一个家庭作业-谁会有关于批处理脚本的家庭作业? 我需要自动化一些东西。目前,有一个硬编码的批处理脚本打算每天运行以获取系统,它需要以动态方式工作。它只需要输入一个版本号,这个版本号可以从位于。。。说C:\DumpLocation\。我不擅长批处理脚本,并寻找一个批处理忍者。如果由我决定,我会自己用Python编写代码,但我不能指望其他人仅仅为此安装它。PowerShell也不是在每台Windows计算机上都可用,因此批处理脚本是最低的公分母 这将有助于: 以下是我希望脚本执行的操作: d

这不是一个家庭作业-谁会有关于批处理脚本的家庭作业? 我需要自动化一些东西。目前,有一个硬编码的批处理脚本打算每天运行以获取系统,它需要以动态方式工作。它只需要输入一个版本号,这个版本号可以从位于。。。说
C:\DumpLocation\
。我不擅长批处理脚本,并寻找一个批处理忍者。如果由我决定,我会自己用Python编写代码,但我不能指望其他人仅仅为此安装它。PowerShell也不是在每台Windows计算机上都可用,因此批处理脚本是最低的公分母

这将有助于:

以下是我希望脚本执行的操作:

dirToLookAt = 'C:\DumpLocation\'
# In that location there should be a single file named
# Custom_SomethingBuild34567Client_12345.zip
# I want to extract the build number into a variable to this effect:
buildNumber = '34567'
# strings which surround the build number are fixed.
# If there is more than one zip file with a build number in it,
# I need to print a warning and pick the largest one.
# I can do the rest.
以下内容也应该有所帮助:


如果您有任何疑问,请告诉我。

假设
自定义\u something build
客户端\u 12345.zip是常量,这应该可以实现以下功能:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM Get a count of the files in the directory

set /a FileCount=0
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d C:\DumpLocation') do (
set /a FileCount+=1
)

REM If the file count is greater than or equal to 2, warn the user
IF %FileCount% GTR 1 ECHO The total number of files in the directory is:  %FileCount%

REM If the file count is less than or equal to 0, pause and exit
IF %FileCount% LEQ 0 PAUSE & EXIT

:: For each build number, use that number if it is the largest number
:: In the loop, we'll strip out the assumed constants, leaving us with a build number.

SET Build=
SET /a BuildNum=0
FOR /F %%A IN ('DIR /P /B C:\DumpLocation') DO (
   SET Build=%%A
   SET Build=!Build:Custom_SomethingBuild=!
   SET Build=!Build:Client_12345.zip=!
   IF !Build! GTR !BuildNum! SET /a BuildNum=!Build!
)

ECHO The greatest build number in the directory is:  %BuildNum%

PAUSE

那么
Custom\u something build
Client\u 12345.zip总是一样的吗?我们可以假设,如果我们将
自定义\u SomethingBuild
客户端\u 12345.zip替换为一个空格,您将始终只剩下内部版本号?您是否签出了
以获取/?
?@drop studens?实际上答案是肯定的+1.现在去纠正错误,愿望脚本呢?我记得这可以追溯到win2k,您可以使用javascript或vbscript编程。该批处理功能更强大。您可以使用
来代替
来代替/f”令牌=*delims=“%%a in('dir/s/b/a-d C:\DumpLocation')
来代替
来代替/f%%a in('dir/P/b C:\DumpLocation')
来代替
来代替/f%%a in('dir/P/b C:\DumpLocation')
,您可以使用
来代替%%a in(C:\DumpLocation)
谢谢,但是部分
SET Build=!Build:Custom_SomethingBuild=获取一个空字符串。我正在Windows Server 2008 R2上尝试此功能。@Hamish-Hrm。。。我手边没有2008R2,但我会看看能不能找到什么。谢谢。下划线对于字符串替换是否有特殊意义?假设它是
\u Client\u 12345\u
\u Custom\u something build\u
。。。这会改变什么吗?抱歉,我不想透露我正在使用的确切字符串。而且,我看到%被用来代替!在线 的