Reputation: 476
1 and 2 work without problems, but I cannot get 3 to work.
My code:
SET EXE="path\to\program.exe"
for /f "delims=" %%i in ('%EXE% argument') do (
rem echo %%i
set mystatus=%%i
)
echo %mystatus%
^ works up to here ^ - echo
returns the correct value. Now I want to return a simpler value that tells me if the string "not" was present in %mystatus%
var
set "isNot="
ECHO.%mystatus%| FIND /I "not">Nul && (
Echo.Found "not"
do something
rem ^this works^
set isNot= "true"
) || (
Echo.Did not find "not"
do something
rem ^this works^
set isNot= "false"
)
now, out of FIND, this returns nothing: WHY?
echo %isNot%
Upvotes: 0
Views: 64
Reputation: 80033
I find your answer - let's say, naïve.
First, I can't reproduce your result. It worked perfectly for me, returning isnot
set to Space"false" or Space"true", not nothing
.
Had isnot
been undefined, then the echo
response should have been Echo is off
, so isnot
was defined in your test. I would use echo +%isNot%+
for this test so that the string is shown with an obvious delimiter. If it contained just spaces - well, they tend to be remarkably invisible.
As to the solution, I's suggest
set "isnot="
if "%mystatus%" neq "%mystatus:not=%" set "isnot=Y"
which avoids find
and piping and conditions, setting isnot
to be either defined
or not defined
as appropriate, ready for testing using
if defined isnot (echo not was not found) else (echo not was found)
The reason for this is that if defined
works on the run-time status of the variable, hence it can be used within a code block
where the variable may be switched from one status to the other.
Use set "var=value"
for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \
, Space or "
- build pathnames from the elements - counterintuitively, it is likely to make the process easier.
Upvotes: 2
Reputation: 38622
This may be a guess as to your intended task, but my assumption is that you are trying to determine if the executable command returns the string, 'path\to\program.exe' is not recognized as an internal or external command, operable program or batch file.
.
If that is the case, then perhaps this would be a better overall solution for you:
@Set "exe=path\to\program.exe"
@For %%G In ("%exe%")Do @If "%%~aG" Lss "-" (Echo ERROR: No such object '%exe%')Else If "%%~aG" GEq "d" (Echo ERROR: '%exe%' is a directory object.)Else %%G argument
All you would need to change is the path on line 1, and the string argument
at the end of line 2.
Upvotes: 0
Reputation: 476
Idiotic mistake, it seems.
There should not be spaces after =
when setting a variable.
Removed the spaces, and it work as it should.
Upvotes: 1