用批处理提取指定路径下所有.txt文件的前N行,合并到指定文件
答案:2 悬赏:40 手机版
解决时间 2021-03-04 13:33
- 提问者网友:你挡着我发光了
- 2021-03-04 03:54
例如,提取 D:\aam\ 下所有.txt文件的前10行,合并输出到 D:\axs\add.txt
最佳答案
- 五星知识达人网友:污到你湿
- 2021-03-04 04:13
@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
全部回答
- 1楼网友:老鼠爱大米
- 2021-03-04 04:36
@echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir/b d:\aam\*.txt‘)do (
set /a n=0
for /f %%j in ("%%i") do (
if !n! lss 10 echo %%j >>d:\axs\add.txt
set /a n+=1
)
)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯