Batch File IF Else info - if-statement

I am attempting to get my script to GOTO Manual if the criteria below is not met .
How ever if the get_info.bat fails and throws a error my script stops and just displays the batch file error (calling python script) . It works when the condtions are met but not on error / not met.
for /f "tokens=1* delims=" %%x in ('get_info.bat ^| find /i "agentVersion: 4"') do #set HPSAAGT=%%x
ECHO %HPSAAGT%
IF "%HPSAAGT%"=="agentVersion: 45.0.31322.0" (set AGTVERSION=45.0.31322.0) ELSE IF "%HPSAAGT%"=="agentVersion: 40.0.0.1.106" (set AGTVERSION=40.0.0.1.106) ELSE (GOTO MANUAL)

The code works fine for me... it may be a problem with your variable. Try echo -%HPSAAGT%- to see if there are extra spaces in your variable.
Additionally, if you just want the version number, you should just use string manipulation, example:
echo %HPSAAGT:~14%
would result in
45.0.31322.0

if a==b (echo ab) else if b==c (echo bc) else (goto manual)
rem some more code
pause
:manual
echo Manual
works fine.
Is there possibly a misspelling in your Label :manual ?

Related

Using batch to read a certain section of a file and create a variable

I've read a lot of things and I can't find away of doing this that I understand. This might be because I'm a wee newbie though.
What I'm trying to do is:
I have a file called start.txt with the contents
..\dll | toapp.exe "..\posdata\filename.xml" "..\OUT" "..\TEMP"
I want to read the contents of that in a batchfile and take only filename.xml as a variable. From device to device inside the network it changes.
I'm then going to use that variable to copy a file from 1 machine to another but I only want that one file and I can't be sure what it's called without looking in the start.txt. I can do all the copy and checks to make sure it's looking in the correct places but just not the findstr section.
Any idea of what to do to understand would be fantastic
#echo off
set "start_file=start.txt"
for /f tokens^=5^ delims^=\^" %%# in ('type "%start_file%"^|find /i "..\posdata"') do set "xml_file=%%#"
echo %xml_file%
Your question is a little bit unclear.Is this the only line in the file? Is this the exact content? How can I recognize the line with the file?
The script above will work in case there's only one line wwith "..\postdata" string and the content is exact as described in question.
Edit Covering the both cases:
#echo off
setlocal
set "start_file=start.txt"
set "xml_file="
for /f tokens^=5^ delims^=\^" %%# in ('type "%start_file%"^|find /i "..\posdata"^|find /i /v "-storedbpath"') do set "xml_file=%%#"
if not defined xml_file (
for /f "usebackq tokens=9" %%# in ("%start_file%") do (
set "xml_file=%%~#"
)
)
echo %xml_file%
Text this:
for /f "usebackq tokens=4" %%a in ("start.txt") do echo %%~nxa

If command in Batch file

I am wondering why my code works this way:
If exist c:\work\first\food.txt (echo win) else (echo fail)
but not like this:
If exist c:\work\first\food.txt (echo food.txt is in C:\work\first) else (echo fail)
I'm using Notepad++ to write this and "in" and c:\work\first are highlighted blue like the other commands if that helps.
Your code works fine. There is no need to escape anything in that particular line.
However double quotes are needed in several cases - where spaces are in the path or & characters etc.
If exist "c:\work\first\food.txt" (echo food.txt is in C:\work\first) else (echo fail)

Batch Script Input Problems

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.

If statement works the first time, but not the second time

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

How to check if directories listed in System PATH variable are valid?

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