I am working on a batch file to use as a wrapper for some data processing modules that are already written, with the goal of being able to run some in parallel and others serially as needed/processing power allows. The underlying modules require one input (a path contained in double quotes), and optionally another path contained in double quotes and/or a set of flags, and I am trying to figure out how to test these inputs. I currently have
: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
This seems to parse everything correctly if things are input in the correct order. However, I also wanted to check if the flags (which will be preceeded by ‘-‘) are popping up as the first or second inputs, and if they are, to display a usage message/set the variables correctly as needed. I figured the easiest way to do this would be to see if the first character of these strings is a ‘-‘, but I can’t find any way to do this. I found a snippet of code that checks if a string contains a certain substring by replacing the substring with empty strings and then seeing if the resulting string is the same as the original, but this wouldn’t work as people might legitimately have hyphens somewhere else in their path(s). Is there a way to check whether the first character of a string matches a given character in a batch file, or a better way of doing what I want?
You can transfer the argument to an environment variable, and then use a SET substring operation to look at the first character. (Type
HELP SETfrom the command line to get info about substring operations)