I have a hard time understanding the following batch script:
@echo off
echo b>b.txt
for /f %%A in ('echo b.txt b.txt') do (
echo %%A
if not exist %%A (
echo does not exist.
) else (
echo %%A exist.
)
)
In the beginning, I make sure that a file “b.txt” exists, and then I would expect the loop to output twice the name of this file, and that it exists. However, the actual output that I get is:
b.txt
does not exists
Can someone explain this behavior?
Just as suggestion you may try to assign file name to variable just to make sure you reference exactly same thing in both statements, like this:
I also removed echo off so you would see expansion output, and put brackets around
%%Ato see it’s boundaryEdit: some addtional troubleshooting:
I would probably start building the script up from command line, changing only only one thing at a time and see where it breaks.
if b.txt exist echo ExistsIf it works then
for /f %A in ('echo whatever') if b.txt exist echo Existsand so on, up to
You could also use different for variant (although I cannot see how it could make a difference):
for %A in (b.txt b.txt) do (if exist %A (echo %A Exists) else (echo not exists))