I'm having this problem with the ** IF NOT processed == %true% **.
The problem is that it's staying in the loop when it shouldnt.
I tried the otherway around IF processed == %false% it goes in the loop BUT doesnt go back in the 2nd time. sometimes, it goes in up to 3 times.
my "echo !processed! process" is always giving me the right number but the IF statement is just not processing the second time around
%TRUE% AND %FALSE% are global variable 1 and 0
setlocal
:loopapp
**if not '!processed!'=='%TRUE%'** (
set /a count+=1
"%ProgramFiles%\abc\abc.exe" !file! !post!
call :ERRORCODES !file! !post! !ERRORLEVEL! !count!
goto :loopapp
)
endlocal
:ERRORCODES
setlocal
if %errornum% LEQ 99 (
set no_license=%FALSE%
if '!post!'== 'A' set no_license=%TRUE%
if '!no_license!'=='%TRUE%' (
echo Searching ... '!count!' ... Please Wait ...
if !count! EQU 5 (
set execute=%TRUE%
set succes=%FALSE%
echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt
goto :END
)
set execute=%FALSE%
set succes=%FALSE%
goto :out_errorcodes
)
set execute=%TRUE%
set succes=%FALSE%
echo %~n1 - Fatal - process %TIME% >> %tempfolder%\errorlog.txt
goto :out_errorcodes
)
... other errors
:out_errorcodes
endlocal & set processed=%execute% & set fait=%succes%
goto :EOF
1) I don't see where you define what TRUE and FALSE are - they are certainly not standard batch values.
If you are trying to set up variables that function as boolean flags, then I recommend the following.
To set the flag to false, use SET "FLAG=". This "undefines" the flag.
To set the flag to true, use SET "FLAG=1. Note that the value 1 has no significance. You could use any value as long as it is not empty.
To test if the flag is true use IF DEFINED FLAG ECHO FLAG IS TRUE
To test if the flag is false use IF NOT DEFINED FLAG ECHO FLAG IS FALSE
The reason I like this technique is that DEFINED is evaluated at execution time, not parse time. So it is safe to use this test within any block of code such as within a FOR loop, and you don't have to worry about delayed expansion.
2) I haven't bothered to try to trace your logic. It would be good if you can come up with a minimal amount of code that demonstrates the problem.
3) If you have used ECHO OFF elsewhere, try setting ECHO ON just prior to the IF so that you can see what the batch script is attempting to do.
(I thought perhaps you were missing SETLOCAL EnableDelayedExpansion, but now I see your report that echo !processed! process works.)
The reason your subprogram does not seem to work is because you made a little error in it.
endlocal & set processed=%execute% & set fait=%succes%
You have a space between %execute% and &.
That space will also be in the %processed% variable's value.
So in your if statement this will be true
'!processed!'=='%TRUE% '
To avoid having that space in the %processed% variable use this
endlocal & set processed=%execute%& set fait=%succes%
This is also documented on SS64
Related
I have two questions regarding a batch script I'm working on. I realize that batch script questions are common but haven't found an answer to my exact question so I thought I'd try asking. The problematic areas are the user input sections on the menus.
There are two problems: 1) Input entered that is not one of the specified choices will cause the script to jump to random areas. And 2) some sections that use external programs are not taking the user %input% even when I know the syntax and flag use would normally be correct (as in, I can run them manually... so for some reason the input isn't capturing on them).
First issue example:
:MenuOne
echo Select one of the following options:
echo 1) x
echo 2) y
echo Q) Quit
set INPUT=
set /P INPUT=[1,2,Q]: %=%
If "%INPUT%"=="1" goto xoption
If "%INPUT%"=="2" goto yoption
If /I "%INPUT%"=="Q" goto Quit
:xoption
#REM Here goes a lot more submenus and/or options that actually run tools via cmd.
:yoption
#REM Again, menus and/or tools being invoked, in a listed menu, designed like above.
:Quit
echo Quitting...
exit
If a user types "b" at the selection prompt, I would love for the script to give an error and repeat the menu. Instead it jerks around other menus. I'm guessing that I need some ELSE statements? Does anyone have an example that I can use to accomplish this?
Second issue of some commands not using the %input% properly and returning an error as though it never received the %input%.
set /P INPUT=[Testone Input]: %testone%
set /P INPUT=[Testtwo Input]: %testtwo%
commandtorun.exe -f %testone% -h %testtwo%
Thanks!
Better to use choice (http://ss64.com/nt/choice.html) because it will persist until you set the correct input
CHOICE /C XYQ /M "Select of the following options [X,Y,Q]"
if errorlevel 1 goto :x
if errorlevel 2 goto :y
uf errorlevel 3 goto :q
Yet it's still possible to be done with IFs
set INPUT=
set /P INPUT=[1,2,Q]: %=%
If "%INPUT%"=="1" goto xoption
If "%INPUT%"=="2" goto yoption
If /I "%INPUT%"=="Q" goto Quit
rem -- will be executed only if the all the above are not true
goto :eof
For the second problem..You are not using SET /P correctly (the name of the variable should be in the front) , or you are trying something that I don't understand (where input variable is used):
set /P testone=[Testone Input]:
set /P testtwo=[Testtwo Input]:
commandtorun.exe -f %testone% -h %testtwo%
In your program as written, all your choices will fall through to the next one. If no relevant choice is entered, it will run :xoption and :yoption. Each of those should probably return to the menu after executing:
:MenuOne
echo Select one of the following options:
echo 1) x
echo 2) y
echo Q) Quit
set INPUT=
set /P INPUT=[1,2,Q]: %=%
If "%INPUT%"=="1" goto xoption
If "%INPUT%"=="2" goto yoption
If /I "%INPUT%"=="Q" goto Quit
echo Invalid selection.
echo.
goto MenuOne
:xoption
#REM Here goes a lot more submenus and/or options that actually run tools via cmd.
goto MenuOne
:yoption
#REM Again, menus and/or tools being invoked, in a listed menu, designed like above.
goto MenuOne
A real simple way to ensure a valid selection is made is to use the choice command instead of set /P. That will force the user to enter a value:
choice -c 12Q
echo %errorlevel%
The choice command will return the index of the selected character (1, 2 or 3 in the above example). A bonus is that it is case-insensitive, so you don't have to worry about checking both Q and q.
I would like to ask your help in a possibly simple situation (where the solution is yet unknown for me).
I am trying to provide a variable for the if command to make a bit more "dynamic" the code, but this fails for me with:
% was unexpected at this time.
Here is a simple example for that:
> for %i in (NEQ) do (if 1 %i 2 echo jo)
%i was unexpected at this time.
While the following works like charm:
>set oper=NEQ
>for %i in (NEQ) do (if 1 %oper% 2 echo works)
works
As I should stay in the for loop (and I get the actual operator from the for loop in the real code), I am really stuck how to solve it...
Tried to play with EnableDelayedExpansion as well, but !variable! instead of the operator is refused as well. Is there a way to submit the variable in a FOR loop for IF, without major modifications in the script?
Is there a way to submit the variable in a FOR loop for IF?
No!
Because, the IF statement has it's own parser and it expects some tokens already expanded at this parse phase.
So it's neither possible to expand the options nor the operator, nor NOT.
But it's allowed to expand the values with delayed or FOR-variables.
setlocal EnableDelayedExpansion
set myOperator=EQU
IF 1 %myOperator% 1 echo Works
IF 1 !myOperator! 1 echo FAILS
for %%O in (EQU) do IF 1 %%O 1 echo Works
The percent expansion works, as it is expanded before the IF parser gets the line.
If you really need to use variable operators, then you need to use a function.
for %%O in (EQU) do call :myFunc %%O
exit /b
:myFunc
IF ONE %1 ONE echo Works
exit /b
EDIT:
And the special parser is also the cause, that it's not possible to use here a CALL expansion for the IF statement.
Like this idea
set myOperator=EQU
CALL IF 1 %%myOperator%% 1 echo Works
This should expand to IF 1 EQU 1 echo Works, but the combination of CALL and IF fails always.
One of my tests is intermittently failing. I now want to execute the test frequently.
I wrote a little batch file
:loop
mstest /testcontainer:"path/test.dll" /test:"mytest"
set /a COUNT=%COUNT%+1
if %COUNT GTR %MAX% goto end
goto loop
While this works fine, though the output isn't meaningful or telling. Let's assume I run this test 100 times I get 100 times a text saying
Loading ...
Starting execution ...
Results
Passed/Failed 1
--------
Total 1
And 100 .trx files will be created...
What I actually want is having an output like this
Passed 97
Failed 3
--------
Total 100
and just one .trx file.
I somehow doubt that it's possible to run a test several times in a single test run at all since I didn't find any parameter on mstest /help output.
Is there a way to create an output as close as possible to the given request?
setlocal enabledelayedexpansion
set passed=0
set failed=0
for /l %%x in (1,1,%max%) do (
mstest /testcontainer:"path/test.dll" /test:"mytest"
if !errorlevel!==0 (
set /a passed=passed+1
) else (
set /a failed=failed+1
)
)
echo Passed %passed% > %~n1.trx
echo Failed %failed% >> %~n1.trx
echo --------- >> %~n1.trx
echo Total %Max% >> %~n1.trx
endlocal
This does not relate directly to my development project but I am curious none the less. Recently, after a lot of head-banging, I traced a build problem to an invalid entry in the System PATH variable. On my machine, it contains about 20 entries. I am guessing there has to be an easier way to verify the validity of each entry. Can anyone suggest a way to check this? Thanks for your time.
This code in a batch file (based on this answer) works for me:
#echo off
setlocal DisableDelayedExpansion
set "var=%PATH%"
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
rem ** This is the key line, the missing quote is intention
set var=%var:""="%
set "var=%var:"=""%"
set "var=%var:;;="";""%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
set "var=%var:"=""%"
set "var=%var:"";""=";"%"
set "var=%var:"""="%"
setlocal EnableDelayedExpansion
for %%a in ("!var!") do (
endlocal
call :testdir "%%~a"
setlocal EnableDelayedExpansion
)
goto :eof
:testdir
if exist %1 echo OK: %1
if not exist %1 echo NOK: %1
Put the code into a text file, e.g. validatepath.bat.
When run, it should output something like:
OK: C:\Users\abcde
NOK: C:\this\is\no\dir
In a DOS batch file we can only have 1 line if statement body? I think I found somewhere that I could use () for an if block just like the {} used in C-like programming languages, but it is not executing the statements when I try this. No error message either. This my code:
if %GPMANAGER_FOUND%==true(echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
Strangely neither "GP Manager is up" nor "GP Manager is down" gets printed when I run the batch file.
You can indeed place create a block of statements to execute after a conditional. But you have the syntax wrong. The parentheses must be used exactly as shown:
if <statement> (
do something
) else (
do something else
)
However, I do not believe that there is any built-in syntax for else-if statements. You will unfortunately need to create nested blocks of if statements to handle that.
Secondly, that %GPMANAGER_FOUND% == true test looks mighty suspicious to me. I don't know what the environment variable is set to or how you're setting it, but I very much doubt that the code you've shown will produce the result you're looking for.
The following sample code works fine for me:
#echo off
if ERRORLEVEL == 0 (
echo GP Manager is up
goto Continue7
)
echo GP Manager is down
:Continue7
Please note a few specific details about my sample code:
The space added between the end of the conditional statement, and the opening parenthesis.
I am setting #echo off to keep from seeing all of the statements printed to the console as they execute, and instead just see the output of those that specifically begin with echo.
I'm using the built-in ERRORLEVEL variable just as a test. Read more here
Logically, Cody's answer should work. However I don't think the command prompt handles a code block logically. For the life of me I can't get that to work properly with any more than a single command within the block. In my case, extensive testing revealed that all of the commands within the block are being cached, and executed simultaneously at the end of the block. This of course doesn't yield the expected results. Here is an oversimplified example:
if %ERRORLEVEL%==0 (
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
)
This should provide var3 with the following value:
blue_cheese
but instead yields:
_
because all 3 commands are cached and executed simultaneously upon exiting the code block.
I was able to overcome this problem by re-writing the if block to only execute one command - goto - and adding a few labels. Its clunky, and I don't much like it, but at least it works.
if %ERRORLEVEL%==0 goto :error0
goto :endif
:error0
set var1=blue
set var2=cheese
set var3=%var1%_%var2%
:endif
Instead of this goto mess, try using the ampersand & or double ampersand && (conditional to errorlevel 0) as command separators.
I fixed a script snippet with this trick, to summarize, I have three batch files, one which calls the other two after having found which letters the external backup drives have been assigned. I leave the first file on the primary external drive so the calls to its backup routine worked fine, but the calls to the second one required an active drive change. The code below shows how I fixed it:
for %%b in (d e f g h i j k l m n o p q r s t u v w x y z) DO (
if exist "%%b:\Backup.cmd" %%b: & CALL "%%b:\Backup.cmd"
)
I ran across this article in the results returned by a search related to the IF command in a batch file, and I couldn't resist the opportunity to correct the misconception that IF blocks are limited to single commands. Following is a portion of a production Windows NT command script that runs daily on the machine on which I am composing this reply.
if "%COPYTOOL%" equ "R" (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using RoboCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
%TOOLPATH% %SRCEPATH% %DESTPATH% /copyall %RCLOGSTR% /m /np /r:0 /tee
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Robocopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
) else (
WWLOGGER.exe "%APPDATA%\WizardWrx\%~n0.LOG" "Using XCopy to make a backup of %USERPROFILE%\My Documents\Outlook Files\*"
call %TOOLPATH% "%USERPROFILE%\My Documents\Outlook Files\*" "%USERPROFILE%\My Documents\Outlook Files\_backups" /f /m /v /y
C:\BIN\ExitCodeMapper.exe C:\BIN\ExitCodeMapper.INI[Xcopy] %TEMP%\%~n0.TMP %ERRORLEVEL%
)
Perhaps blocks of two or more lines applies exclusively to Windows NT command scripts (.CMD files), because a search of the production scripts directory of an application that is restricted to old school batch (.BAT) files, revealed only one-command blocks. Since the application has gone into extended maintenance (meaning that I am not actively involved in supporting it), I can't say whether that is because I didn't need more than one line, or that I couldn't make them work.
Regardless, if the latter is true, there is a simple workaround; move the multiple lines into either a separate batch file or a batch file subroutine. I know that the latter works in both kinds of scripts.
Maybe a bit late, but hope it hellps:
#echo off
if %ERRORLEVEL% == 0 (
msg * 1st line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue1
)
:Continue1
If exist "C:\Python31" (
msg * 2nd line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue2
)
:Continue2
If exist "C:\Python31\Lib\site-packages\PyQt4" (
msg * 3th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue3
)
:Continue3
msg * 4th line WORKS FINE rem You can relpace msg * with any othe operation...
goto Continue4
)
:Continue4
msg * "Tutto a posto" rem You can relpace msg * with any othe operation...
pause