批处理脚本:搜索文件夹是否包含任何文件

发布于 2024-09-27 22:00:35 字数 331 浏览 2 评论 0 原文

我正在尝试获取批处理脚本来检查文件夹中是否包含任何文件。到目前为止,这就是我已经取得的进展:

IF EXIST %FILEPATH%\%%i\FromGlobus\%FILE% (

%WINZIP% %FILEPATH%\GlobusEOD\ExtraFiles\%ZIPFILE% -m %FILE%
IF errorlevel 1 goto subBADEND

)

其中 %FILE%*.* 但发生的情况是,即使文件不存在,它也会尝试压缩文件,因此失败!

有什么建议或想法吗?

谢谢

I am trying to get a batch script to check whether a folder contains any files in it. So far this is how far I have got:

IF EXIST %FILEPATH%\%%i\FromGlobus\%FILE% (

%WINZIP% %FILEPATH%\GlobusEOD\ExtraFiles\%ZIPFILE% -m %FILE%
IF errorlevel 1 goto subBADEND

)

where %FILE% is *.* but what happens is it tries to zip up files even when none exist and therefore fails!

Any tips or ideas?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

七度光 2024-10-04 22:00:35

对我自己的脚本进行一些修改后

的变体@Belisarius' 的答案是我已经使用了一段时间的答案......经过一番修改后,@NimeCloud's 版本似乎不适用于我的 XP 机器...

我想出了两者的混合满足我的需求:

%FOLDER%=C:\Temp
FOR /F %%i in ('dir /b "%FOLDER%\*.*"') DO ( goto :Process )
goto :Exit

:Process
...
...
...

:Exit
exit

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:

%FOLDER%=C:\Temp
FOR /F %%i in ('dir /b "%FOLDER%\*.*"') DO ( goto :Process )
goto :Exit

:Process
...
...
...

:Exit
exit
千寻… 2024-10-04 22:00:35

您可以使用类似的方法,

set VAR=init
for /f %%a in ('dir /b c:\kk\*.*') do set VAR=exists
if %VAR%==exists ...

在目录较大的情况下效率不高,但它可以工作。

哈!

You may use something like

set VAR=init
for /f %%a in ('dir /b c:\kk\*.*') do set VAR=exists
if %VAR%==exists ...

Not very efficient in case of large directories, but it works.

HTH!

千秋岁 2024-10-04 22:00:35

dir . 返回 . ..也许这些虚拟目录造成了混乱。您可以尝试像下面这样查找管道:

SET filter=*.*
SET notfound="File not found"

DIR %filter% | FIND %notfound% 
@If ErrorLevel 1 Goto :end

dir . returns . .. maybe these virtual dirs cause the mess. You could try FIND pipe like below:

SET filter=*.*
SET notfound="File not found"

DIR %filter% | FIND %notfound% 
@If ErrorLevel 1 Goto :end
朦胧时间 2024-10-04 22:00:35

如果您使用/A:-D作为dir命令,那么如果目录有文件,它将切换返回码。仅适用于文件(/A:D 不适用于子目录)

dir /A:-D /B "mydir" >nul 2>nul && ; echo.mydir 有文件

If you use /A:-D for dir 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

亚希 2024-10-04 22:00:35

您可以将 finddir 一起使用,以确保返回代码与未找到文件时的预期相符:

DIR /A /B | >NUL FIND /V "" && ECHO Folder contains files || ECHO Folder empty

You could use find with dir to ensure the return code matches what you'd expect when no files are found:

DIR /A /B | >NUL FIND /V "" && ECHO Folder contains files || ECHO Folder empty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文