0

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
)
aybe
  • 15,516
  • 9
  • 57
  • 105
  • Get rid of the `CMD /C`. It is not needed. Should just be `if exist "%%x\test.txt" echo %%x` – Squashman Mar 27 '20 at 17:43
  • I've already tried that, it doesn't work, nothing gets printed. – aybe Mar 27 '20 at 17:46
  • 1
    Your code does not reproduce the problem you are describing. If it is opening the file instead of printing the file name to the screen then your code is missing the `echo` for the command to execute. – Squashman Mar 27 '20 at 17:48
  • I've updated my question according your comment but that didn't work. – aybe Mar 27 '20 at 17:59
  • And no, indeed it does reproduce the problem I am describing. – aybe Mar 27 '20 at 18:00
  • I copied and pasted your original code to test for the existence of the file and it produces this error. `'\test.txt' is not recognized as an internal or external command, operable program or batch file.` – Squashman Mar 27 '20 at 18:03
  • That is exactly the problem I am talking about, this part of the code is interpreted as a command to be executed, not as a parameter to the `if exist` command. Here, I've created `c:\test.txt` and it gets opened in Notepad. – aybe Mar 27 '20 at 18:07
  • You said the batch file was opening the file. That is not happening. Your words: **But it doesn't work, instead, it opens the file directly.** – Squashman Mar 27 '20 at 18:10
  • That's what is happening here, I have cmd along notepad++ and when I run the batch file, the file gets opened in notepad++. ¯\_(ツ)_/¯ – aybe Mar 27 '20 at 18:19
  • read [this](https://stackoverflow.com/questions/4905708/batch-files-dont-run-theyre-being-opened-with-notepad/43068749?r=SearchResults&s=1|48.9045#43068749) if you want to change that behavior. – Stephan Mar 27 '20 at 18:24
  • Thank you but I'd rather not do this, this script is meant to run on many computers :) – aybe Mar 27 '20 at 18:25

1 Answers1

1

The issue is coming from the WMIC output. You can't see it on the screen but the output is Unicode and also outputs extra CRLF. So you need to feed the output into another FOR command to essentially clean it up.

@echo off
echo test 2
for /f "skip=1 delims=" %%x in ('wmic logicaldisk get caption') do (
    for /F %%H in ("%%x") do if exist %%H\test.txt echo %%H
)
Squashman
  • 13,649
  • 5
  • 27
  • 36