下面的批处理文件以递归方式回显echos文件和文件夹,同时添加一些简单的格式,例如缩进以显示递归深度,在文件夹名称前添加“/”,在某些文件之前添加“*”,以及跳过名为“Archive”的文件夹。它工作得很好,除了文件和文件夹是随机排序,而不是按字母顺序排序。如何更改以按字母顺序对文件和文件夹进行排序?
@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
答案 0 :(得分:4)
需要的变化非常小。将FOR循环转换为运行已排序DIR命令的FOR / F. /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
。
答案 1 :(得分:2)
尝试从
更改for /d
循环
for /d %%F in (*) do
到
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....
)
)