获取某路径下的所有文件夹名.bat

来自「namejm写的批处理」· Batch 代码 · 共 67 行

BAT
67
字号
@echo off
:: 判断输入的路径是不是文件夹,如果是,则获取其下的所有文件夹(包括子目录中的)
:: 带绝对路径或相对路径
::       code by jm CMD@XP  2006-7-20

if exist long_list.txt del long_list.txt /q
if exist short_list.txt del short_list.txt /q
:input
cls
set input=:
set /p input=           请输入要进行判断的路径:
set "input=%input:"=%"
:: 上面这句为判断%input%中是否存在引号,有则剔除。
if "%input%"==":" goto input
if not exist "%input%" goto input
for %%i in ("%input%") do if /i "%%~di"==%%i goto input
pushd %cd%
cd /d "%input%">nul 2>nul || exit
set cur_dir=%cd%
popd
for /f "delims=" %%i in ('dir /b /a-d /s "%input%"') do echo %%i>>long_list.txt
if not exist long_list.txt goto no_dir
setlocal enabledelayedexpansion
for /f "delims=" %%i in (long_list.txt) do (
    set shortname=%%i
    set "shortname=!shortname:%cur_dir%\=%!"
    echo !shortname!>>short_list.txt
)
start long_list.txt
start short_list.txt
exit

:no_dir
cls
echo       %cur_dir% 文件夹下没有文件夹
pause
exit

@echo off
:: 判断输入的路径是不是文件夹,如果是,则获取其下的所有文件夹(包括子目录中的)
:: 只有文件夹名,不带路径
::       code by jm CMD@XP  2006-7-20

if exist list.txt del list.txt /q
:input
cls
set input=:
set /p input=           请输入要进行判断的路径:
set "input=%input:"=%"
:: 上面这句为判断%input%中是否存在引号,有则剔除。
if "%input%"==":" goto input
if not exist "%input%" goto input
for %%i in ("%input%") do if /i "%%~di"==%%i goto input
pushd %cd%
cd /d "%input%">nul 2>nul || exit
set cur_dir=%cd%
popd
for /r "%input%" %%i in (.) do dir /ad /b "%%i">>list.txt
for %%i in (list.txt) do if "%%~zi"=="0" goto no_file
start list.txt
exit

:no_file
cls
echo       %cur_dir% 文件夹下没有单独的文件
del list.txt /q
pause

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?