开发者

Need help with a Windows batch script which should set val of var based on part of a file name

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.

Thi开发者_Python百科s 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜