The following enumerates drive letters and if they exist, it prints, works as expected:
echo off
echo test 1
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
cmd /c "if exist %%x echo %%x"
)
Now I would like to test for an existing file in %%x
such as C:\test.txt
:
echo off
echo test 2
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
cmd /c "if exist %%x\test.txt echo %%x"
)
But it doesn't work, instead, it opens the file directly.
Question:
How can I test for an existing file inside a for /f
loop using if exist
?
@Squashman
I pasted the following according your suggestion into a new batch file but nothing gets printed out at all.
@echo off
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption ^| findstr "."') do (
if exist %%x\test.txt echo %%x
)