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

发布于 2024-10-09 04:02:03 字数 1270 浏览 0 评论 0原文

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

这应该有帮助: http ://www.techsupportforum.com/microsoft-support/windows-xp-support/54848-set-variable-based-output-seach-string-batch.html

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

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.

以下内容也应该有所帮助:

http://www.computing。 net/answers/programming/batch-string-substitution/12097.html

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

This is not a homework - who would have homework on batch scripting?
I need to automate something. Currently there is a hard-coded batch script meant to be run daily to get systems, and it needs to work in a dynamic fashion. All it needs as an input is a build number, which can be deduced from the name of the file located at ... say C:\DumpLocation\. I am not good at batch scripting, and looking for a batch Ninja. If it was up to me, I would code this up in Python myself, but I cannot expect others to install it just for this. PowerShell is also not available on every Windows computer, so batch script is the lowest common denominator.

This should help:
http://www.techsupportforum.com/microsoft-support/windows-xp-support/54848-set-variable-based-output-seach-string-batch.html

Here is what I want the script to do:

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.

The following should help as well:

http://www.computing.net/answers/programming/batch-string-substitution/12097.html

Please let me know if you have questions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离不开的别离 2024-10-16 04:02:03

假设 Custom_SomethingBuildClient_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

Assuming that Custom_SomethingBuild and Client_12345.zip are constants, this should do the trick:

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文