Check folder is empty using batch command?

泄露秘密 提交于 2019-12-05 09:12:21

try this:

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
)

File Not Found

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

The error that you see when you run this command, comes for the standard error output. But that is only a warning printed to your console. When this case happens, the body of the iteration won't be evaluated, and the algorithm based on this "for/dir" instruction, is in fact correct.

Now, if you want to get rid of this ugly error message, you need to add the following to your script, in order to redirect the standard error to null device:

2>NUL

so for instance, in a batch file, with the appropriate escape character:

:: echo list of files
@for /f "tokens=*" %%a in ('dir /b /a-d "%srcPath%" 2^>NUL') do @echo(%srcPath%\%%a
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!