用批处理提取指定路径下所有.txt文件的前N行,合并到指定文件
例如,提取D:\aam\下所有.txt文件的前10行,合并输出到D:\axs\add.txt...
例如,提取 D:\aam\ 下所有.txt文件的前10行,合并输出到 D:\axs\add.txt
展开
2个回答
展开全部
@echo off
setlocal enabledelayedexpansion
cd /d D:\aam\
for /f "tokens=*" %%i in ('dir/b D:\aam\*.txt') do (
set n=0
for /f "tokens=*" %%j in ('type "%%i"') do (
if !n! lss 10 echo %%j>>D:\axs\add.txt
set /a n+=1
)
)
endlocal
=====================================================================
灵活高效:
@echo off
setlocal enabledelayedexpansion
set 源目录=D:\aam\
set 目标文件=D:\axs\add.txt
set 截止行=10
if "%源目录:~-1,1%"=="\" set "源目录=%源目录:~,-1%"
cd /d "%源目录%"
cd.>"%目标文件%"
for /f "tokens=*" %%i in ('dir/b *.txt') do call :xwj "%源目录%\%%i"
endlocal
goto :eof
:xwj
set n=0
for /f "tokens=*" %%j in ('type "%~1"') do (
set /a n+=1
echo %%j>>"%目标文件%"
if !n!==%截止行% goto :eof
)
goto :eof
setlocal enabledelayedexpansion
cd /d D:\aam\
for /f "tokens=*" %%i in ('dir/b D:\aam\*.txt') do (
set n=0
for /f "tokens=*" %%j in ('type "%%i"') do (
if !n! lss 10 echo %%j>>D:\axs\add.txt
set /a n+=1
)
)
endlocal
=====================================================================
灵活高效:
@echo off
setlocal enabledelayedexpansion
set 源目录=D:\aam\
set 目标文件=D:\axs\add.txt
set 截止行=10
if "%源目录:~-1,1%"=="\" set "源目录=%源目录:~,-1%"
cd /d "%源目录%"
cd.>"%目标文件%"
for /f "tokens=*" %%i in ('dir/b *.txt') do call :xwj "%源目录%\%%i"
endlocal
goto :eof
:xwj
set n=0
for /f "tokens=*" %%j in ('type "%~1"') do (
set /a n+=1
echo %%j>>"%目标文件%"
if !n!==%截止行% goto :eof
)
goto :eof
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询