Windows 使用DIR命令显示子目录';s与文件…抑制子目录';它没有文件

Windows 使用DIR命令显示子目录';s与文件…抑制子目录';它没有文件,windows,command-line,Windows,Command Line,我当前正在具有子目录的特定目录上运行命令dir/b/s*。如何仅获取不显示空目录的目录列表,而仅显示包含文件的目录。假设以下结构(这将是dir/s/b的结果) 对于至少包含一个文件的目录列表: for /f %i in ('dir /b /s /ad') do @for /f %j in ('dir %i ^| find "File(s)"') do @if not %j==0 echo %i 结果: test\1 test\2 test\1\fred.txt test\2\wilma.tx

我当前正在具有子目录的特定目录上运行命令
dir/b/s*
。如何仅获取不显示空目录的目录列表,而仅显示包含文件的目录。

假设以下结构(这将是dir/s/b的结果)

对于至少包含一个文件的目录列表:

for /f %i in ('dir /b /s /ad') do @for /f %j in ('dir %i ^| find "File(s)"') do @if not %j==0 echo %i
结果:

test\1
test\2
test\1\fred.txt
test\2\wilma.txt
对于文件列表,但不包括仅作为目录的行:

for /f %i in ('dir /b /s /ad') do @for /f %j in ('dir %i ^| find "File(s)"') do @if not %j==0 dir /b /s %i
结果:

test\1
test\2
test\1\fred.txt
test\2\wilma.txt

请记住,如果在批处理文件中执行此操作,则必须将%符号加倍,例如%%i和%%j

这将为您提供一个没有文件的文件夹列表:

@echo off
(for /f "delims=" %%a in ('dir /s /b /ad') do dir "%%a" /b /a-d >nul 2>&1 && echo %%a)>"folderlist.txt"
你说“获取目录列表”,但我认为你是在要求文件,对吗?您想只查看文件的目录名,还是只查看带有路径的完全限定文件名?