批处理脚本:搜索文件夹是否包含任何文件
我正在尝试获取批处理脚本来检查文件夹中是否包含任何文件。到目前为止,这就是我已经取得的进展:
IF EXIST %FILEPATH%\%%i\FromGlobus\%FILE% (
%WINZIP% %FILEPATH%\GlobusEOD\ExtraFiles\%ZIPFILE% -m %FILE%
IF errorlevel 1 goto subBADEND
)
其中 %FILE%
是 *.*
但发生的情况是,即使文件不存在,它也会尝试压缩文件,因此失败!
有什么建议或想法吗?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对我自己的脚本进行一些修改后
的变体@Belisarius' 的答案是我已经使用了一段时间的答案......经过一番修改后,@NimeCloud's 版本似乎不适用于我的 XP 机器...
我想出了两者的混合满足我的需求:
After some tinkering with my own scripts
A variation of @Belisarius' answer is what I've been using for awhile...After tinkering for a bit, @NimeCloud's version just doesn't seem to work on my XP machine...
I've come up with a blend of the two that seems to work for my needs:
您可以使用类似的方法,
在目录较大的情况下效率不高,但它可以工作。
哈!
You may use something like
Not very efficient in case of large directories, but it works.
HTH!
dir . 返回 . ..也许这些虚拟目录造成了混乱。您可以尝试像下面这样查找管道:
dir . returns . .. maybe these virtual dirs cause the mess. You could try FIND pipe like below:
如果您使用
/A:-D
作为dir
命令,那么如果目录有文件,它将切换返回码。仅适用于文件(/A:D
不适用于子目录)dir /A:-D /B "mydir" >nul 2>nul && ; echo.mydir 有文件
If you use
/A:-D
fordir
command, then it will switch the return code if directory has files. Works only for files (/A:D
won't work for subdirectories in the same way)dir /A:-D /B "mydir" >nul 2>nul && echo.mydir having files
您可以将
find
与dir
一起使用,以确保返回代码与未找到文件时的预期相符:You could use
find
withdir
to ensure the return code matches what you'd expect when no files are found: