I have managed to translate the majority of this script to work in a Windows 7 batch file. I have not tested this against any other version of Windows.
This program scans the command line sent to it and sets various
environment variables that coorespond to the settings.
It sets an OPTION_arg variable for each arg on the command line.
If a switch, the env var is set to 1. If a value is given via the colon sign,
it's set to that value. Note, there can not be any white space around the :
[Modification] This modified script also cannot handle option or parameter values containing spaces, even when encased in parenthesis. [/Modification]
Use If defined OPTION_arg
or if /i "%OPTION_arg%"=="value"
to test for options
It also sets a parameter variable for each paramater entered: PARAM_1
to PARAM_n
and
PARAM_0
is a special value that contains the number of PARAMs. Useful for looping
through all of them. For example for /l (1,1,%PARAM_0%) do ...
In your batch file call getopt as call GetOpt.bat %*
I also recommend setting setlocal
and endlocal
in the host batch file so that
the option and param variables do not stick around after the host batch files exits.
Example usage: HostBatchFile.bat /a /b:22 /longopt Parm1 Parm2
OPTION_a will equal 1.
OPTION_b will equal 22
OPTION_longopt will eqal 1.
PARAM_1 will equal Parm1
PARAM_2 will equal Parm2
PARAM_0 will be set to the number of parms, so 2 in this case
The parts I have not translated are:
- Incorporating any kind of DEBUG flag for screen output
- Being able to handle "strings with spaces" for either options or parameters as the initial separation of arguments fed in are split using
<SPACE>
Here is the translated script.
@echo off
cls
set getopt_ParmCounter=1
set paramc=1
set DEBUG=1
set argc=0
for %%x in (%*) do Set /A argc+=1
echo Number of arguments: %argc%
echo %*&echo.
set _myvar=%*
rem Loop through all command line arguments one at a time
:varloop
set isparam=1
for /f "tokens=1*" %%a in ('echo %_myvar%') DO (
set getopt_Parm=%%a
set _myvar=%%b
call :paramtype
rem shift along arguments and rerun loop
if NOT "%%b"=="" goto varloop
)
goto :eof
:paramtype
rem If first character starts with a - or / it must be an option
if /i "%getopt_Parm:~0,1%"=="-" call :option
if /i "%getopt_Parm:~0,1%"=="/" call :option
if /i "%isparam%"=="1" call :param
goto :eof
:option
set isparam=0
rem Set the Equal Index to the position of the colon. 0 means none was found
for /f %%j in ('findstring %getopt_Parm% :') do set getopt_EqIdx=%%j
rem If the index is GE 0 then we must have a colon in the option.
if /i "%getopt_EqIdx%"=="0" (call :nocolon) else (call :colon)
goto :eof
:colon
rem set the OPTION value to the stuff to the right of the colon
set /a getopt_ParmNameEnd=%getopt_EqIdx%-2
call set getopt_ParmName=%%getopt_Parm:~1,%getopt_ParmNameEnd%%%
call set getopt_ParmValue=%%getopt_Parm:~%getopt_EqIdx%%%
set OPTION_%getopt_ParmName%=%getopt_ParmValue%
goto :eof
:nocolon
rem This is a flag, so simply set the value to 1
set getopt_ParmName=%getopt_Parm:~1%
set getopt_ParmValue=1
set OPTION_%getopt_ParmName%=%getopt_ParmValue%
goto :eof
:param
rem There was no / or - found, therefore this must be a paramater, not an option
set PARAM_%getopt_ParmCounter%=%getopt_Parm%
set PARAM_0=%getopt_ParmCounter%
set /a getopt_ParmCounter+=1
goto :eof
Try this:
set test=BE99012345678901
if "%test:~0,4%"=="BE99" (
echo This number begins with BE99
) else (
echo This number doesn't begin with BE99
)
pause
You will get following output:
This number begins with BE99
Kindly note:
if %test% == "BE99????????????"
will do this:
"BE99012345678901" == "BE99????????????"
and this will show following output:
This number doesn't begin with BE99
Best Answer
One of the main problems in your logic is that you set the "import" variable once if something is true, but never reset it, or undefined it if it's not true anymore. So after the first time it's set, it will remain "defined" for the rest of the loop.
My preference is to set the variable specifically (
true
orfalse
). Set it tofalse
to start, then set it totrue
as wanted, but then also ensure you set it back tofalse
when you need to. Then, at each iteration of the loop check if the variable is specifically set toTrue
orFalse
instead of checking if it's just defined.This code works for me based on your info/goals:
We purposefully set the "Import" variable (flag) to
false
to start.First For-loop goes through each line in the file (
test.txt
in this example, as specified by the "buildlog" variable; note: you need to remove the quotes around the file name in the variable for it to work in the For-loop).The first IF sees if the current line is the "Import:" line, and if so, it flips the "import" flag to
true
.The next IF sees if the current line is the "View:" line, and if so, it flips the "import" flag (back to) to
false
, so that it stops processing each line.The 3rd IF checks if that "import" flag is
true
, and if so, it processes the line.If it's
true
then the nested (4th) IF then checks if the line is the actual "import:" line, and if not, displays the line (keeps it from showing the "Import:" line in the output).The second For-loop goes through the line we want displayed, and pulls only the first Token set which is (just) the path you want, leaving off the
#head
.More/related info:
Edit after comments:
To deal with the "View:" line if it has a version number after it you could modify the code to something like:
This added For-loop will pull the first token off the line to check if it's "View:" or "Import:" instead of checking the whole line. Effectively ignoring anything after the first space it encounters on the line to do the check.