English 中文(简体)
批量文件: 分析命令行输入
原标题:Batch file: parsing command line inputs

我正准备一个批量文件,作为一些已经写入的数据处理模块的包装纸,目标是能够按照需要/处理电源的要求平行运行一些模块,并按顺序运行其他模块。 基本模块需要一个输入(一个包含在双引号中的路径),另一个路径也可以选择包含在双重引用和(或)一套旗帜中的路径,我正试图找出如何测试这些输入。 我目前已经有了一个。

:TEST_PARAMS
REM Make sure parameters are correct; if not, display a usage message
IF NOT "%1"=="" (SET SUBBASENAME=%1
) ELSE (
    GOTO :USAGE
)
IF NOT "%2"=="" (SET ATLASBASENAME=%2
) ELSE (
    SET ATLASBASENAME=%DEFAULTATLAS%
)
IF NOT "%3"=="" (SET FLAGS=%3
) ELSE (
    SET FLAGS=""
)
GOTO :START_SVREG

这似乎可以正确分析所有事物, 如果事物是输入正确的顺序。 但是, 我还想检查国旗( 将先由 - ) 是否弹出为第一个或第二个输入, 如果是的话, 是否显示使用信息/ 按需要正确设置变量 。 我想最简单的方法就是查看这些字符串的第一个字符是否是一个 -, 但我找不到任何方法这样做 。 我发现一个代码片段, 如果字符串中包含一个子字符串, 则通过用空字符串替换子字符串来检查, 然后看看由此产生的字符串是否与原始字符串相同, 但是这样可以正常地在其他人的路径中显示连字符 。 是否有办法检查字符串的第一个字符是否匹配批文件中给定的字符, 或者检查一个更好的方法来做什么?

最佳回答

您可以将参数传输到环境变量, 然后使用 SET 子字符串操作查看第一个字符 。 (从命令行输入 < code> HELP SET 以获得关于子字符串操作的信息)

@echo off
set "var=%~1"
if defined var if "%var:~0,1%" equ "-" echo arg 1 is a flag
问题回答

您真的不需要在任何批次文件参数中检查“ - ”, 因为作为批次文件创建者, 您知道哪些命令可以被处理 。 意思是如果使用 “ - b”, 那么您就会知道如何处理它, 或者如果用户通过了“ t”, 您就会知道如何处理。 在批次文件中, 您只需要处理所有参数, 无论这些参数是按哪个顺序排列的, 而在您的批次文件中, 您将会有部分可以处理它 。 这里有一个例子批次文件可以处理与“ -” 或“ /” 一起使用的任何参数 :

@echo off

if [%1]==[] goto usage
:CHECKFORSWITCHES
@echo Execute the Command
IF  %1 == /param1  GOTO PARAM1
IF  %1 == -param2  GOTO PARAM2
IF  %1 == /param3  GOTO PARAM3
IF  %1 == -param4  GOTO PARAM4
IF  %1 == /param5  GOTO PARAM5
goto :eof

:PARAM1
@echo This is Param1
set var=%1
set var2=%var:~0,1%
echo %var2%
SHIFT
goto :CHECKFORSWITCHES
:PARAM2
@echo This is Param2
SHIFT
goto :CHECKFORSWITCHES
:PARAM3
@echo This is Param3
SHIFT
goto :CHECKFORSWITCHES
:PARAM4
@echo This is Param4
set var=%1
set var2=%var:~0,1%
echo %var2%
SHIFT
goto :CHECKFORSWITCHES
:PARAM5
@echo This is Param5
SHIFT
goto :CHECKFORSWITCHES
:usage
@echo Usage: %0 ^<Write your command here^>
exit /B 1

上面的脚本还检查任何参数的第一个字符, 所以如果您需要单独使用该代码, 您肯定可以使用它, 但我不认为这是一个很好的方法 。

如果您需要在上面写一些基于“ -” 或“/” 的逻辑, 请使用 IF%var2 /“ GOTO XXX, 您可以做任何你想做的事 。





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签