Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Developer: les ferch Date: Sunday, November 23, 2008 08:33 AMThis is a UDS I put together to make command line parameter handling in WinBatch more convenient. With this approach, you can use vbscript style command line parameters and (hopefully) never have to write another custom command line parameter handler again.
Note that, with this approach, parameters can be on the command line in any order.
;----------------------------------------------------------------------------------------------------------- ; Check for existence of a command line parameter (case insensitive) ; Parameters muse be prefixed with "/" or "-" ; Parameter data (if any) must be separated from parameter by a ":" ; Sample valid parameter formats: /a -b:Hello /c:"Hello world" -D ; Usage: ParamExist(ParamName) ; Returns: @TRUE if parameter exists, @FALSE if parameter does not exist ; Parameter data returned in global variable named "ParamData" #DefineSubRoutine ParamExist(ParamChk) If Param0==0 Then Return @FALSE For iz = 1 To Param0 Param = Param%iz% ParamName = StrUpper(ItemExtract(1,Param,":")) ParamData = ItemExtract(2,Param,":") If StrSub(ParamData,1,1)=='"' Then ParamData = StrSub(ParamData,2,StrCharCount(ParamData) - 2) If ParamName==StrUpper(ParamChk) Then Return @TRUE FirstChar = StrSub(Param,1,1) If FirstChar=="/" || FirstChar=="-" ParamName = StrSub(ParamName,2,-1) If ParamName==StrUpper(ParamChk) Then Return @TRUE EndIf Next Return @FALSE #EndSubRoutine ;Let's test it... Param0 = 4 Param1 = '/a' Param2 = '-b:Hello' Param3 = '/c:"Hello world"' Param4 = '-D' Pause(ParamExist("A"),ParamData) Pause(ParamExist("b"),ParamData) Pause(ParamExist("C"),ParamData) Pause(ParamExist("d"),ParamData) Pause(ParamExist("E"),ParamData)If you prefer a UDF, here it is:
;----------------------------------------------------------------------------------------------------------- ; Check for existence of a command line parameter (case insensitive) ; Parameters muse be prefixed with "/" or "-" ; Parameter data (if any) must be separated from parameter by a ":" ; Sample valid parameter formats: /a -b:Hello /c:"Hello world" -D ; Usage (existence): ParamExist(ParamName) ; Returns: @TRUE if parameter exists, @FALSE if parameter does not exist ; Usage (param data): ParamData(ParamName) ; Returns: Parameter data (null if no data or parameter does not exist) #DefineFunction ParamCheck(ParamChk) CmdLine = IntControl(1006,0,0,0,0) MyPath = IntControl(1004,0,0,0,0) CmdLine = StrReplace(CmdLine,'"':MyPath:'"',"") ParseData(CmdLine) Data = "" If Param0==0 Then Return @FALSE:@TAB:Data For iz = 1 To Param0 Param = Param%iz% ParamName = StrUpper(ItemExtract(1,Param,":")) Data = ItemExtract(2,Param,":") If StrSub(Data,1,1)=='"' Then Data = StrSub(Data,2,StrCharCount(Data) - 2) If ParamName==StrUpper(ParamChk) Then Return @TRUE:@TAB:Data FirstChar = StrSub(Param,1,1) If FirstChar=="/" || FirstChar=="-" ParamName = StrSub(ParamName,2,-1) If ParamName==StrUpper(ParamChk) Then Return @TRUE:@TAB:Data EndIf Next Return @FALSE:@TAB:Data #EndFunction #DefineFunction ParamExist(Param) Return ItemExtract(1,ParamCheck(Param),@TAB) #EndFunction #DefineFunction ParamData(Param) Return ItemExtract(2,ParamCheck(Param),@TAB) #EndFunction ; To test, run this script from a command line. Example: ; ScriptName.wbt /a -b:Hello /c:"Hello world" -D Pause(ParamExist("A"),ParamData("a")) Pause(ParamExist("b"),ParamData("B")) Pause(ParamExist("C"),ParamData("c")) Pause(ParamExist("d"),ParamData("D")) Pause(ParamExist("E"),ParamData("e"))
Article ID: W18381
Filename: Command Line Parameter Handling.txt
File Created: 2008:11:25:12:20:42
Last Updated: 2008:11:25:12:20:42