2

i have a text file with a list of file names & i want to search 'if not exist' & if command doesn't find any of the files mentioned in listimagescopy.txt document it should create a new document notfound.txt which will list all of the files not found

i started trying to figure it out but im not so good w cmd- something to this affect-pasted into .cmd file

@echo off

FOR /F "tokens=*" %%a IN (listimagescopy.txt) DO (
 IF NOT EXIST "%%a" echo %%a Not found >notfound.txt
)

im not sure how&where to specify in which folder it should be searching in -also need it to look in sub folders maybe i need to use nested FORs w FOR /R ...im not sure

(i found a similar question but none of the answers do what i need it to do- How can I test if a list of files exist? )

Community
  • 1
  • 1

2 Answers2

1

You actually got everything right but the details are missing. Here's a script that works it may have some bugs with paths containing spaces haven't tested it):

@echo off

SETLOCAL

SET fileList=%1
SET notFoundFile=%2

REM clear the not found file

type NUL > %notFoundFile%

FOR /R %%d IN (.) DO (
   FOR /F "tokens=*" %%a IN (%fileList%) DO (

      ECHO processing "%%~fd\%%a"
      IF NOT EXIST "%%~fd\%%a" ECHO %%~fd\%%a Not found >> %notFoundFile%
   )
)

Usage

The command searches for files recursively from the current directory. It accepts two parameters: the first is the full path to the file containing the list of files to search and the second is the full path to the file where the not found files are recorded.

For example, assume:

  1. the batch above is stored in the file findmissing.cmd in the directory c:\batchdir (i.e. the batch file's full path is c:\batchdir\findmissing.cmd)
  2. The list of files to find is listed in the file: c:\batchdir\listimagescopy.txt
  3. The result file containing the list of "not found" files will be: c:\batchdir\notfound.txt

Then in order to search for the files in the directory c:\searchdir and all its subdirectories you'd need to CD to that directory and run:

c:\searchdir> \batchdir\findmissing.cmd \batchdir\listimagescopy.txt \batchdir\notfound.txt

What's going on

Batch files store the first parameter (\batchdir\listimagescopy.txt in the example) in the argument variable %1, the second parameter (\batchdir\notfound.txt in the example) is stored in %2 (further parameters if they existed would be stored %3 %4, etc.)

SET fileList=%1
SET notFoundFile=%2

Stores the command line parameters in local (because of the SETLOCAL above) environment variables. This is just a convenience to have nicer variable names. Environment variables are extended (accessed) in the batch with e.g. %fileList%.

type NUL > %notFoundFile%

Clears the contents the contents of the "not found" file or creates a new zero length file if it doesn't exist. This is important because we'll be appending to this file and don't want the results from different runs to be there.

FOR /R %%d IN (.) DO (

Starts at the current directory and recursively list all the directories by matching them with (.) . The directory names are placed in the for loop variable %%d

   FOR /F "tokens=*" %%a IN (%fileList%) DO (

Does what you described in your question, it takes each line in fileList and stores it in the for variable %%a

Now the interesting part %%d contains the current directory in the loop including the trailing "." so for example:

%%d = "c:\searchdir\."

Accessing the variable with the ~f option removes the trailing '.'

%%~fd = "c:\searchdir"

%%a contains the name of one of the files you are searching say:

%%a = "image001.jpg"

Then:

%%~fd\%%a = "c:\searchdir\image001.jpg"

These lines should now be obvious:

      ECHO processing "%%~fd\%%a"
      IF NOT EXIST "%%~fd\%%a" ECHO %%~fd\%%a Not found >> %notFoundFile%

but I'll explain them anyway; the first just tells you what the batch is doing. It can be removed to speed things up (quite substantially.) The second tests whether the file %%~fd\%%a exists and if not appends (that's the >>, using > overwrites) the string "%%~fd\%%a Not found" to the file %notFoundFile%, after expanding the variables so you get the actual name of the file not found in your file.

You can make the internal loop nicer by placing the content of %%~fd\%%a in a variable instead of repeating the pattern, but you'll need delayed expansion see here and here

Community
  • 1
  • 1
Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
0

This will output all the files in the list not found in any subfolders into the file notfound.txt

setlocal enabledelayedexpansion
pushd "N:\opasdata\d110001\medias\images"
set found=false
for /f "tokens=* delims=" %%a in (listimagescopy.txt) do (
for /r %%x in (%%a) do (
if exist "%%a" set found=true
)
if "!found!"=="false" echo %%a >>"V:\Current Library\notfound.txt"
set found=false
)
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • I have edited my answer with your setup. The only thing you might want to change is the file with the list of names, I have used `listimagescopy.txt` as per your example, but just change it if you want. – Bali C Jan 10 '13 at 08:44