2013-02-20 33 views
2

批處理文件如下遞歸回聲文件和文件夾,同時加入一些簡單的格式,如縮進顯示遞歸深度,添加「/」前的文件夾名稱,「*」,並跳過名爲「存檔」的文件夾。除了文件和文件夾是隨機排序的,而不是按字母順序排列的,它效果很好。這怎麼可能被改變來按字母順序排序文件和文件夾呢?批處理文件:之前某些文件排序文件夾和文件按字母順序

@echo off 
setlocal disableDelayedExpansion 
pushd %1 
set "tab= " 
set "indent=" 
call :run 
exit /b 

:run 

REM echo the root folder name 
for %%F in (.) do echo %%~fF 
echo ------------------------------------------------------------------ 

set "folderBullet=\" 
set "fileBullet=*" 

:listFolder 
setlocal 

REM echo the files in the folder 
for %%F in (*.txt *.pdf *.doc* *.xls*) do echo %indent%%fileBullet% %%F - %%~tF 

REM loop through the folders 
for /d %%F in (*) do (

    REM skip "Archive" folder 
    if /i not "%%F"=="Archive" (

    REM if in "Issued" folder change the file bullet 
    if /i "%%F"=="Issued" set "fileBullet= " 

    echo %indent%%folderBullet% %%F 
    pushd "%%F" 
    set "indent=%indent%%tab%" 
    call :listFolder 

    REM if leaving "Issued folder change fileBullet 
    if /i "%%F"=="Issued" set "fileBullet=*" 

    popd 
)) 
exit /b 

回答

4

只需要很少的更改。將FOR循環轉換爲FOR/F運行排序的DIR命令。 /A-D選項僅列出文件,而/AD僅列出目錄。

這個版本的名字

@echo off 
setlocal disableDelayedExpansion 
pushd %1 
set "tab= " 
set "indent=" 
call :run 
exit /b 

:run 

REM echo the root folder name 
for %%F in (.) do echo %%~fF 
echo ------------------------------------------------------------------ 

set "folderBullet=\" 
set "fileBullet=*" 

:listFolder 
setlocal 

REM echo the files in the folder 
for /f "eol=: delims=" %%F in (
    'dir /b /a-d /one *.txt *.pdf *.doc* *.xls* 2^>nul' 
) do echo %indent%%fileBullet% %%F - %%~tF 

REM loop through the folders 
for /f "eol=: delims=" %%F in ('dir /b /ad /one 2^>nul') do (

    REM skip "Archive" folder 
    if /i not "%%F"=="Archive" (

    REM if in "Issued" folder change the file bullet 
    if /i "%%F"=="Issued" set "fileBullet= " 

    echo %indent%%folderBullet% %%F 
    pushd "%%F" 
    set "indent=%indent%%tab%" 
    call :listFolder 

    REM if leaving "Issued folder change fileBullet 
    if /i "%%F"=="Issued" set "fileBullet=*" 

    popd 
)) 
exit /b 

排序文件要排序的擴展,然後再通過名稱,只需更改/ONE/OEN

+0

謝謝D.完美的作品。 – buttonsrtoys 2013-02-21 11:58:14

1

嘗試從

for /d %%F in (*) do 

改變你for /d環路

for /f "delims=" %%F in ('dir /b /o:n *.') do 

,看看是否有差別。實際上,按名稱排序是dir默認行爲,所以你很可能與

for /f "delims=" %%F in ('dir /b *.') do 

脫身如果你的一些目錄的名稱在他們的點,你需要改變它一點點。

for /f "delims=" %%F in ('dir /b') do (
    rem Is this a directory? 
    if exist "%%F\" (
     rem do your worst.... 
    ) 
) 
相關問題