7
for /F %%i in ('dir /b "C:\Program Files\Apache folder\*.*"') do (
   echo Folder is NON empty
   goto launch_app
)
  • 如何检查文件夹是否为空?

    我尝试了上面的命令,但是没有用。

4

5 回答 5

11

试试这个:

for /F %%i in ('dir /b /a "C:\Program Files\Apache folder\*"') do (
    echo if you see this the folder is NOT empty
    goto launch_app
)
于 2013-07-19T11:45:28.983 回答
2

文件未找到

@for /f "tokens=*" %%a in ('dir /b /a-d "C:\Progra~1\Apache"') do @...

运行此命令时看到的错误来自标准错误输出。但这只是打印到控制台的警告。当这种情况发生时,迭代的主体将不会被评估,并且基于这个“for/dir”指令的算法实际上是正确的。

现在,如果您想摆脱这个丑陋的错误消息,您需要将以下内容添加到您的脚本中,以便将标准错误重定向到空设备:

2>NUL

例如,在批处理文件中,使用适当的转义字符:

@rem Print a list of file paths
@for /f "tokens=*" %%a in ('dir /b /a-d "%srcPath%" 2^>NUL') do @echo(%srcPath%\%%a
于 2016-03-26T23:58:33.333 回答
1
@echo off
cls
echo.
echo.
echo.
echo                  TEST FOR EMPTY DIRECTORY
echo             was tested with "&" without a file
echo.
echo              Can use Full or Subfolder path
echo               (as below) of where test.bat
echo.
    set p=Raw\
echo.
echo                  Just to show p is set
echo.
echo                       "p=%p%"
echo.
echo.
echo.
pause

for /F %%i in ('dir /b /a "%p%*"') do (
    echo Folder %p% was NOT empty
    goto :process
)
    echo Folder %p% was empty 



:process
    echo "BLAH, BLAH, BLAH "
:end
    pause
    exit
rem Copy past in notepad to try. "Create and use or change the "raw" to your own fancy."
rem Hope this helps. Works great for me and I use it.
rem Have Fun.
rem Note use go to end next line after was empty. Worked great in w10. (this program hmm)
rem IF IT WORKS FOR YOU . GIVE A +. THX!
于 2020-05-05T03:52:43.127 回答
0
dir C:\TEST\*.* /a/b/d/od>C:\TEMP\CHECKFOLDER
for /R C:\TEMP\ %%? in (CHECKFOLDER) do (
 if "%%~z?"=="0" (
  ECHO Folder is empty.
 ) ELSE (
  ECHO Folder is NOT empty
 )
)
于 2020-03-28T22:41:12.960 回答
0

我对给出的答案不满意,因为我尽可能避免使用 for 循环、goto 和 temp 文件。

检查文件夹是否为空:

dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul || (
  echo Folder is empty
)

检查文件夹是否为空:

dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul && (
  echo Folder is NOT empty
)
于 2021-11-23T07:41:17.847 回答