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?
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:
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 :
I wanted to add one small comment to most of the above solutions. They are not actually testing if a particular file exists or not. They are checking to see if the file exists and you have access to it. It's entirely possible for a file to exist in a directory you do not have permission to in which case you won't be able to view the file even though it exists.
Bash:
Please note, however, that using the default file systems under both Win32 and *nix there is no way to guarantee the atomicity of the operation, i.e. if you check for the existence of files A, B, and C, some other process or thread might have deleted file A after you passed it and while you were looking for B and C.
File systems such as Transactional NTFS can overcome this limitation.
In Windows:
(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: