批量列出特定格式的所有文件夹和子文件夹

时间:2014-05-28 08:41:13

标签: batch-file

我有一个问题是我上一个问题的后续问题 Batch List files in folder and subfolder according to specific format

现在脚本工作正常,它会生成我需要的list.txt。但是我需要对它进行一些调整,老实说,我不知道从哪里开始或者如何开始。

我在根文件夹中有多个文件夹,每个文件夹都可以包含一个子文件夹,该子文件夹也可以包含一个子文件夹。

现在我正在寻找的是改变代码以便输出是这样的

Ver:24
 C:\\RESCLIENT

2014-05-27  09:37            29293018  Pandora_00.res
2014-05-27  15:41      <DIR>           Char
2014-05-11  15:41            19287692  Char\Pandora_00.res
2014-05-11  15:45      <DIR>           World
2014-05-11  15:45      <DIR>           WdMadrigal
2014-05-11  15:50            19287692  WdMadrigal\Pandora_00.res

如果有人可以帮助我,我会非常赞赏它。

亲切的问候,

Thomas de vries。

EDIT 我忘了在具体输出的例子中提到一件事。

我们在Root目录中有2个文件夹和几个文件 现在它必须采用这种格式

C:\\RESCLIENT

2014-05-27  08:52p      <DIR>          Char
2014-05-27  08:52p      <DIR>          Client
2014-05-27  09:29p                   0 Flyff.a
2014-05-27  08:52p      <DIR>          Icon
2014-05-27  08:52p      <DIR>          Item
2014-05-27  08:53p      <DIR>          Model
2014-05-27  08:52p            29293018 Pandora_00.res
2014-05-27  08:53p      <DIR>          SFX
2014-05-27  08:53p      <DIR>          Weather
2014-05-27  08:51p      <DIR>          World

 C:\\RESCLIENT\\Char
2014-05-27  08:52p            19287692 Pandora_00.res

 C:\\RESCLIENT\\Client
2014-05-27  08:52p              506652 Pandora_00.res

我怎样才能做到这一点。 我知道它发生了很大变化,但我忘记了C ++程序以这种格式读取它而不是另一种格式。

对于给您带来的不便表示歉意

亲切的问候。

2 个答案:

答案 0 :(得分:1)

@echo off
setlocal EnableDelayedExpansion

set "base=%CD%\"

for /F "delims=" %%a in ('dir /B /S') do (
   for /F "tokens=1-5 delims=/-. " %%b in ("%%~Ta") do set "dateTime=%%d-%%c-%%b  %%e%%f"
   set name=%%a
   set attrib=%%~Aa
   if /I "!attrib:~0,1!" neq "d" (
      set "size=                   %%~Za"
      echo !dateTime! !size:~-19! !name:%base%=!
   ) else (
      echo !dateTime!      ^<DIR^>           !name:%base%=!
   )
)

答案 1 :(得分:1)

从先前的答案中检索代码并使用一点格式化......

@echo off

    setlocal enableextensions disabledelayedexpansion

    :: Select where to start listing files

    if "%~1"=="" (set "startingFolder=%cd%") else (set "startingFolder=%~1")

    :: Determine the length of the starting path to remove
    :: it from output and normalize the path inside the 
    :: starting folder variable

    for /d %%a in ("%startingFolder%\"
    ) do for /f "skip=1 tokens=1 delims=:" %%b in (
        '(echo(%%~fa^&echo(^)^|findstr /o "^"'
    ) do ( set /a "cutPoint=%%b-3" & set "startingFolder=%%~fa" )

    :: Show header

    echo(
    echo(%startingFolder%
    echo(

    if not exist "%startingFolder%" (
        endlocal 
        echo Error, folder not found >&2
        exit /b 1
    )

    :: Retrieve the list of files and folders under starting
    :: folder, correctly sorted, to a temporary file. This avoids
    :: problems with for /f handling large sets of data retrieved
    :: from a command execution

    set "tempFile=%temp%\%~nx0.%random%.tmp"
    dir "%startingFolder%*" /s /b 2>nul | sort > "%tempFile%"

    :: Read the temporary file and, for each of the lines=files/folders
    :: select the adecuated format to output

    for /f "usebackq delims=" %%a in ("%tempFile%") do (
        set "_name=%%a"
        setlocal enabledelayedexpansion
        set "_attribs=%%~aa"

        if "!_attribs:~0,1!"=="d" (
            set "_size=      <DIR>         "
        ) else (
            set "_size=                    %%~za"
        )
        echo(%%~ta !_size:~-20! !_name:~%cutPoint%!
        endlocal
    )

    :: Cleanup and exit
    del /q "%tempFile%" >nul 2>nul
    endlocal

    exit /b

已编辑将递归dir操作转换为递归调用子例程

@echo off

    setlocal enableextensions disabledelayedexpansion

    call :myCustomDIR %1

    exit /b

:myCustomDIR startingPoint
    setlocal enableextensions disabledelayedexpansion

    if "%~1"=="" (set "startingFolder=%cd%") else (set "startingFolder=%~1")

    :: normalize the path inside the starting folder variable
    for /d %%a in ("%startingFolder%\") do set "startingFolder=%%~fa"

    echo(
    echo(%startingFolder%
    echo(

    if not exist "%startingFolder%" (
        endlocal 
        echo Error, folder not found >&2
        exit /b 1
    )

    :: List files/folders into current level
    for /f "delims=" %%a in ('dir /on /b "%startingFolder%*"'
    ) do for %%b in ("%startingFolder%%%~a") do (
        set "_name=%%b"
        setlocal enabledelayedexpansion
        set "_attribs=%%~ab"

        if "!_attribs:~0,1!"=="d" (
            set "_size=      <DIR>         "
        ) else (
            set "_size=                    %%~zb"
        )
        echo(%%~tb !_size:~-20! %%~nxb
        endlocal
    )

    :: Cleanup this level and call for any lower folder
    endlocal & for /d %%a in ("%startingFolder%*") do call :myCustomDIR "%%a"

    exit /b