如何测试文件列表是否存在? [英] How can I test if a list of files exist?

查看:100
本文介绍了如何测试文件列表是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件列出文件名,每个都在自己的行,我想测试是否每个都存在于特定的目录。例如,该文件的一些示例行可能是

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

我感兴趣的目录是 X:\Windows \System32 \ ,因此我想查看是否存在以下文件:

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

我如何使用Windows命令提示符?

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?

推荐答案

在cmd.exe中,

编辑:我尝试cmd.exe脚本,请求:

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

注意,上面的脚本必须是脚本;由于某些奇怪的原因,.cmd或.bat文件中的FOR循环在变量之前必须有双百分号。

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.

现在, bash | ash | dash | sh | ksh:

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

这篇关于如何测试文件列表是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆