如何用批处理遍历一个文件夹下所有子目录和子目录下的文件

2024-11-22 17:24:40
推荐回答(3个)
回答1:

以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI
@echo off
rem 获取指定目录下的所有文件路径并进行截取
set #=Any question&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$% %z%
cd /d "%~dp0"
set "folder=D:\xxx\A"
set "outfile=xxx.txt"
type nul>"%outfile%"
if not exist "%folder%" (echo;"%folder%" not found&goto end)
for /f "delims=" %%a in ("%folder%") do set "fd=%%~nxa"
for /f "delims=" %%a in ('dir /a-d/b/s "%folder%\"') do (
set "fpath=%%a"
setlocal enabledelayedexpansion
>>"%outfile%" echo;%fd%!fpath:%folder%=!
endlocal
)
:end
echo;%#% +%$%%$% %z%
pause

回答2:

@echo off
rem 获取指定目录下的所有文件路径并进行截取
set #=Any question&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$% %z%
cd /d "%~
dp0
"
set "源目录=D:\xxx\A"
set "输出=xxx.txt"
type nul>"%输出%"
for /f "delims=" %%a in ("%源目录%") do set "fd=%%~nxa"
for /f "delims=" %%a in ('dir /a-d/b/s "%源目录%"') do (
set "fpath=%%a"
setlocal enabledelayedexpansion
>>"%输出%" echo;%fd%!fpath:%源目录%=!
endlocal
)
echo;%#% +%$%%$% %z%
pause

回答3:

很简单:
dir /s /b>1.txt
然后在1.txt里,把你不需要的 d:/C 替换为空即可。