使用批处理命令检查文件夹是否为空

时间:2013-07-19 11:27:45

标签: windows batch-file command directory

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

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

4 个答案:

答案 0 :(得分: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
)

答案 1 :(得分:1)

找不到文件

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

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

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

2>NUL

因此,例如,在批处理文件中,具有适当的转义字符:

:: echo list of files
@for /f "tokens=*" %%a in ('dir /b /a-d "%srcPath%" 2^>NUL') do @echo(%srcPath%\%%a

答案 2 :(得分: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
 )
)

答案 3 :(得分:0)

@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!