2017-08-17 63 views
0

以下是我使用的目录。如何通过批处理文件获得特殊的目录列表?

C:\test>dir /s /b /a:d 
C:\test\A 
C:\test\A\a 
C:\test\A\b 
C:\test\A\a\a 
C:\test\A\a\b 
C:\test\A\a\a\20160101 
C:\test\A\a\a\20160816 
C:\test\A\a\b\20160101 
C:\test\A\a\b\20160816 
C:\test\A\b\a 
C:\test\A\b\b 
C:\test\A\b\a\20160101 
C:\test\A\b\a\20160816 
C:\test\A\b\b\20160101 
C:\test\A\b\b\20160816 

使用dir /s /b /a:d获取所有文件夹目录。

如何通过批处理文件获取文件列表下的3层测试文件夹?

我想获得以下列表:

C:\test\A 
C:\test\A\a 
C:\test\A\b 
C:\test\A\a\a 
C:\test\A\a\b 
C:\test\A\b\a 
C:\test\A\b\b 
+1

你究竟想做什么?进入嵌套目录的批处理文件? –

+0

@PradeepDeshmukh我编辑我的问题,谢谢你的提问。 – Peggy

+0

因此,您只希望列表显示在cmd选项卡右侧? –

回答

1
@echo off 
cls 
for /f "delims=" %%I in ('dir /s /b /ad') do (
    call :countAppr "%%~I" 
) 
exit /b 

:countAppr 
set "string=%~1" 
set count=0 
:again 
set "oldstring=%string%" 
set "string=%string:*\=%" 
set /a count+=1 
if not "%string%" == "%oldstring%" goto :again 
if %count% leq 4 echo(%~1 
exit /b 

说明:

  • 循环彻底文件
  • 计数\出现在文件夹名
  • 量如果小于或等于4,则返回文件夹名称

要显示文件夹在文件夹级别,改变leqequ。深层次也可以改变。

注意:某些脚本是从Stephan的回答here复制并编辑的。

+0

这个命令只能显示3级吗?例如,只显示'C:\ test \ A \ a \ a','C:\ test \ A \ a \ b','C:\ test \ A \ b \ a','C:\ test \ A \ b \ b' – Peggy

+0

是的,通过将'leq'改为'equ'。 – SteveFest

+0

谢谢,看起来不错! – Peggy

1

一个简单的解决方案是使用robocopy命令。虽然打算用于文件复制操作,但它包括一个/L开关,请求不要复制,但要列出。调整开关删除非必要的信息,您可以使用

robocopy . . /e /nfl /njh /njs /ns /lev:4 /l 

这将递归(/e)名单(/l)当前文件夹下的所有选定的元素,不显示文件信息(/nfl),无作业报头(/njh)无摘要(/njs),没有文件/大小计数器(/ns)为深搜索四个层次的(当前文件夹加上以下三个要求的水平)

robocopy命令的输出包括一些选项卡/空格在开始的线。如果您需要将其删除,你可以使用像

for /f "tokens=*" %a in ('robocopy . . /e /nfl /njh /njs /ns /lev:4 /l') do echo %a 

或者从一个批处理文件

for /f "tokens=*" %%a in ('robocopy . . /e /nfl /njh /njs /ns /lev:4 /l') do echo %%a 

编辑如果robocopy的使用是一个问题(不可用/允许你的系统) ,或者你需要(从评论)的输出限制为只有最后一个级别,你可以使用像

@echo off 
    setlocal enableextensions disabledelayedexpansion 

    rem Retrieve folder from command line, default current folder 
    for /f "delims=" %%a in ("%~f1\.") do set "target=%%~fa" 

    echo ---------------------------------------------------------------------- 
    rem Call subroutine searching for ALL folders up to 3 levels 
    call :treeDump target 3 


    echo ---------------------------------------------------------------------- 
    rem Call subroutine searching for folders ONLY 3 levels deep 
    call :treeDump target 3 true 

    goto :eof 

rem Recursive folder search  
:treeDump targetVar maxLevel forceLevel 
    rem targetVar = name of variable containing the folder to iterate 
    rem maxLevel = how many levels to search under target 
    rem forceLevel = only show the last requested level 

    setlocal disabledelayedexpansion 
    rem Check we are not searching too deep 
    2>nul set /a "nextLevel=%~2-1", "1/(%~2+1)" || goto :eof 

    rem Retrieve folder to iterate 
    setlocal enabledelayedexpansion & for %%a in ("!%~1!") do endlocal & (
     rem Determine if current level must be shown 
     if "%~3"=="" (
      echo %%~fa 
     ) else (
      if %nextLevel% lss 0 echo %%~fa 
     ) 
     rem If not at the last level, keep searching 
     if %nextLevel% geq 0 for /d %%b in ("%%~fa\*") do (
      set "target=%%~fb" 
      call :treeDump target %nextLevel% "%~3" 
     ) 
    ) 
    goto :eof 

它采用递归functi在那将遍历目录树。对于找到的每个文件夹,如果我们不在所需的级别,子文件夹将被枚举,并且每个文件夹都会再次调用该函数。

+0

兼容性警报? OP没有声明他/她的操作系统,但XP用户需要安装一个'robocopy'资源包。 – SteveFest

+0

这个命令只能显示3级吗?例如,只显示'C:\ test \ A \ a \ a','C:\ test \ A \ a \ b','C:\ test \ A \ b \ a','C:\ test \ A \ b \ b' – Peggy

+0

@Peggy是的,MC ND的解决方案有可能工作,但需要使用相同的代码。 – SteveFest

相关问题