Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test if a list of files exist?

I have a file that lists filenames, each on it's own line, and I want to test if each exists in a particular directory. For example, some sample lines of the file might be

mshta.dll
foobar.dll
somethingelse.dll

The directory I'm interested in is X:\Windows\System32\, so I want to see if the following files exist:

X:\Windows\System32\mshta.dll
X:\Windows\System32\foobar.dll
X:\Windows\System32\somethingelse.dll

How can I do this using the Windows command prompt? Also (out of curiosity) how would I do this using bash or another Unix shell?

like image 992
Justin Voss Avatar asked Sep 29 '08 21:09

Justin Voss


People also ask

How do you check file is exist or not?

The exists() function is a part of the File class in Java. This function determines whether the is a file or directory denoted by the abstract filename exists or not. The function returns true if the abstract file path exists or else returns false.

How do you check if there are files in a directory Python?

In Python, you can check whether certain files or directories exist using the isfile() and isdir() methods, respectively. However, if you use isfile() to check if a certain directory exists, the method will return False. Likewise, if you use if isdir() to check whether a certain file exists, the method returns False.

How do you check if files exist in Linux?

When checking if a file exists, the most commonly used FILE operators are -e and -f . The first one will check whether a file exists regardless of the type, while the second one will return true only if the FILE is a regular file (not a directory or a device).


4 Answers

In cmd.exe, the FOR /F %variable IN ( filename ) DO command should give you what you want. This reads the contents of filename (and they could be more than one filenames) one line at a time, placing the line in %variable (more or less; do a HELP FOR in a command prompt). If no one else supplies a command script, I will attempt.

EDIT: my attempt for a cmd.exe script that does the requested:

@echo off
rem first arg is the file containing filenames
rem second arg is the target directory

FOR /F %%f IN (%1) DO IF EXIST %2\%%f ECHO %%f exists in %2

Note, the script above must be a script; a FOR loop in a .cmd or .bat file, for some strange reason, must have double percent-signs before its variable.

Now, for a script that works with bash|ash|dash|sh|ksh :

filename="${1:-please specify filename containing filenames}"
directory="${2:-please specify directory to check}
for fn in `cat "$filename"`
do
    [ -f "$directory"/"$fn" ] && echo "$fn" exists in "$directory"
done
like image 81
tzot Avatar answered Sep 30 '22 08:09

tzot


Bash:

while read f; do 
    [ -f "$f" ] && echo "$f" exists
done < file.txt
like image 43
JesperE Avatar answered Sep 30 '22 08:09

JesperE


for /f %i in (files.txt) do @if exist "%i" (@echo Present: %i) else (@echo Missing: %i)
like image 31
Zorantula Avatar answered Sep 30 '22 08:09

Zorantula


In Windows:


type file.txt >NUL 2>NUL
if ERRORLEVEL 1 then echo "file doesn't exist"

(This may not be the best way to do it; it is a way I know of; see also http://blogs.msdn.com/oldnewthing/archive/2008/09/26/8965755.aspx)

In Bash:


if ( test -e file.txt ); then echo "file exists"; fi
like image 21
Adam Rosenfield Avatar answered Sep 30 '22 08:09

Adam Rosenfield