English 中文(简体)
需要帮助使用Windows batch书写器,根据档案名称的一部分确定瓦尔的阀值。
原标题: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.

This should help: 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.

还应当帮助:

请让我知道,你是否有问题。

问题回答

假设<代码>Custom_SomethingBuild和Client_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




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签