Search code examples
loopsbatch-fileif-statementfor-loopdos

DOS For if statement to look for file in multiple directories


I'm trying to use the following DOS statement to look for 1st level folders that do not have important.txt.

for /d %X in (M:\*) do if not exist important.txt echo %X

This statement runs, but the if portion does not run properly, always returning that the file is missing even when it exists. What am I doing wrong?


Solution

  • You are testing the existance of the file in the current directory only because you didn't prefix the folder name. Try for /d %X in (M:*) do if not exist %X\important.txt echo %X
    and see what changes.
    Another note: you scan directories from the current directory on M: on. This might be M:\ or some other directory.