批处理脚本列出所有空子文件夹以归档给定路径(试图修复文件未找到错误和路径问题)

Batch script to list all empty subfolders to file for the path given (trying to fix File Not Found error and path problems)

我正在尝试编写一个批处理脚本,该脚本将列出给定文件路径的所有空子文件夹。

它似乎大部分都在工作,但有一些问题我不知道如何解决。

当我遍历子文件夹时,它以某种方式在路径的末尾添加了点。我相信,这就是 "File Not Found" 结果也给出的原因。

此外,如果我给一个带空格的路径,它说该文件夹不存在。同时,如果不是给定的路径,而是它的子目录有空格,脚本也能很好地处理。

这是脚本:

@Echo off

setlocal disableDelayedExpansion

IF EXIST List.txt del /F List.txt

::No path given
set "root=%~1"
echo Path given: %root%
if not defined root (
    echo No path given!
    GOTO:EOF
)

::The folder does not exist
if exist %root%\* (
echo:
) else (
    echo No such folder!
    GOTO:EOF
)

::Is folder empty?
echo Is folder empty?
FOR /R "%root%" %%G in (.) DO (
    set "folder=%%G"
    call :testFILE
)
GOTO:EOF

:testFILE
for /F %%i in ('dir /b /A-D "%folder%\*"') do exit /b
REM if you're here, directory has no files
echo %folder% has no files
call :testDIR
exit /b

:testDIR
for /F %%i in ('dir /b /A:D "%folder%\*"') do exit /b
REM if you're here, directory has no directories
echo "%folder%" is totally empty >> List.txt
exit /b

如果能帮助修复错误,我们将不胜感激!

我终于想通了!

@echo off

setlocal disableDelayedExpansion

if exist List.txt del /F List.txt

::No path given
set "root=%~1"
if not defined root (
    echo:
    echo No path given!
    GOTO:EOF
)

::The folder does not exist
if exist %root%\* (
GOTO write_path
) else (
    echo:
    echo No such folder!
    GOTO:EOF
)

:write_path
::Write the given path
echo:
echo Path given: %root%

::Is folder empty?
for /D /r "%root%" %%D in (.) do (
    dir /A /B "%%~fD" 2>nul | findstr "^" >nul || echo %%~fD >> List.txt
)
GOTO:EOF

最后一部分有点棘手...

在这里查看有关它的一些解释:

SS64.com - Command Line Redirection

SS64.com - FINDSTR Command