I am trying to call a command from a batch file which is reading in lines from a file.
This is working correctly, except for when the line contains the redirection character >.
Is there a way to tell call to escape this character, or to replace it within the content of the for loop?
I’ve looked at setlocal enabledelayedexpansion and (when the call is updated to use ! it still doesn’t work).
set /p status=<%tmp_file%
for /f "delims=*" %%a in (%tmp_file%) do (
echo "%%a"
call check.bat "%%a"
echo.
if not "%errorlevel%" == "0" do exit /b 1
)
This produces the following output (when check.bat echo’s %1)
"a"
"a"
"b -> b"
"b -"
I’ve tried to replace > within %%a but I’m not entirely sure how this can be achieved, each time i try, it yields an empty string, i.e.
set line=%a:^>=¬%
EDIT 1
Some more clarification (it appears to only be the case if %1 is set to a variable, and then that variable is used)?:
check.bat:
set rawinput=%1
set input=%~1
echo %1
echo "%rawinput%"
echo "%input%"
echo.
This yields the following output, although im not quite sure why setting %1 to a variable causes it to mangle the value?
"a"
"a"
""a""
"a"
"b -> b"
"b -> b"
"b -"
Interestingly b -> b is only output 2 times, echo "%rawinput%" is not showing at all.
Both echo "%input%" and echo "%rawinput%" are writing to a file named b.
This means that the check.bat for b -> b must be interpreted as:
echo "b -> b"
echo ""b -> b""
echo "b -> b" REM - this is what 'should' be happening, however does appear to be the case, as it writes '' to a file named b
echo.
If anyone can shed light on why echo "b -> b" in a batch file does not appear to be behaving it would be greatly appreciated.
Do not try to use
call by value, that’s nearly impossible with batch.Simply set a new variable with the value and then use the variable name.
Edit: Your problem
You try to call a function with the content
"b -> b"usingcall check.bat "%%a"this will expand tocall check.bat ""b -> b""and that’s the problem!As the
>redirect character is now outside of the quotes, it will redirect the output of check.bat to the fileb.You can escape a special character (outside of quotes) when using a call, but this is a bit advanced and has many problems.
So, I would recommend again, not using directly the content in a call.
You could use a reference, like
check.bat