gpt4 book ai didi

batch-file - 批处理文件: sorting folders and files alphabetically

转载 作者:行者123 更新时间:2023-12-04 00:36:52 25 4
gpt4 key购买 nike

下面的批处理文件递归地回显文件和文件夹,同时添加一些简单的格式,例如缩进以显示递归深度,在文件夹名称前添加“/”,在某些文件前添加“*”,以及跳过名为“存档”的文件夹。它工作得很好,除了文件和文件夹是随机排序的,而不是按字母顺序排序。如何更改为按字母顺序对文件和文件夹进行排序?

@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

最佳答案

只需要很少的改动。将 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

关于batch-file - 批处理文件: sorting folders and files alphabetically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14985614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com