0

I have a root folder "Basic" which has child folders "1", "2" and "3". "1" has sub-folders "11", "12" and "text.txt". Similarly "2" has sub-folders "22", "21" and "text.txt". "3" has "31" and "32".

I need a batch file program to find whether "text.txt" is present in each folder. If it isn't present in a particular root folder "Basic", I want to write the child folder's name in missingfile.txt.

Here is my account - It doesn't work.

set value = ""
set exact = ""
cd "C:\Users\bthirumurthy\Desktop\Basic"
dir  "C:\Users\bthirumurthy\Desktop\Basic" /b >> text.txt
for %%a in (text.txt) do (
  if (%%a|="text.txt") (
    dir  C:\Users\bthirumurthy\Desktop\Basic\%%a /b >> C:\Users\bthirumurthy\Desktop\Basic\%%a\result.txt
    for %%b in (result.txt) do (
      if(%%b == "text.txt") (
        set exact = %%b
        set status = 1
      )
      else (
        set missingfile =%%b
        set status = 0
      )
    )
    if (%status% == 1) (
      echo %exact% pass >> pass.txt
    )
    else (
      echo %exact% fail >> Missingfile.txt
    )
    set status = ""
  )
)>>output.txt

Can you please help me out?

1 Answer 1

2

I haven't taken the time to figure out where your script is going wrong. But a simple one-liner from the command line is all that is needed. No need for batch:

>missingFile.txt (for /r "C:\Users\bthirumurthy\Desktop\Basic" %F in (.) do @if not exist "%F\test.txt" echo %~fF)

Double up all percents if run from within a batch script. (% becomes %%)

Basically, this one-liner traverses the directory tree starting from the specified root directory (the for /d /r loop). For every subdirectory, it checks if the specified file exists in it (or, rather, if it doesn't exist there: if not exist ...). If the file doesn't exist, the corresponding subdirectory's full path is logged into missingfile.txt. Actually, the path is simply echoed (echo %~fF), but the entire loop's output has been redirected (the >missingFile.txt at the beginning of the line), so, as a result, echo writes to the file.

EDIT - a slightly simpler variation

>missingFile.txt (for /r "C:\Users\bthirumurthy\Desktop\Basic" %F in (test.txt) do @if not exist "%F" echo %~dpF)

The FOR /R loop doesn't check if the file exists unless there is a wildcard in the IN() clause. Without a wildcard it simply walks the directory tree and builds a path with the file name in each directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.