批处理文件脚本,用于在计数大于 > 时删除文本文件中的行
Batch file script to remove lines in text file when count greater than >
我有充满 URL 的文本文档,当我计算文件中的行数时,一旦行数大于 1000
,我想删除那些后面的行,保留第一 1000
行。
这是我的代码,但我不知道我做错了什么。
使用此脚本位置创建一个 text.txt
文件并将 1000
行垃圾放入其中进行测试。
@ECHO OFF & setLocal EnableDelayedExpansion
color 0A
%*
SET root_path=%~dp0
set text_name=text.txt
set text_name_output=output_text.txt
set max_line_count=1000
set /a counter=0
for /f %%a in (%root_path%%text_name%) do (
set /a counter += 1
echo Number of lines: !counter!
if !counter! LSS %max_line_count% (
echo less than
) else (
echo more than so delete these line numbers
(for /f "tokens=1,* delims=[]" %%a in ('type %root_path%%text_name%^|find /v /n ""') do (
echo/%%a|findstr /x "!counter!" >nul || echo/%%b
))>%root_path%%text_name_output%
)
)
pause
我想通了,如果文本或文档超过 1000 行,则删除后面的所有行。
@ECHO OFF & setLocal EnableDelayedExpansion
color 0A
%*
SET root_path=%~dp0
set text_name=text.txt
set text_name_output=output_text.txt
set max_line_count=1000
set /a counter=0
for /f %%a in (%root_path%%text_name%) do set /a counter+=1
set /a "counter=%counter%-%max_line_count%"
more +%counter% "%root_path%%text_name%" >"%root_path%%text_name_output%"
move /y "%root_path%%text_name_output%" "%root_path%%text_name%" >nul
pause
为此任务使用批处理文件是绝对不合适的,但是,这里有一个基本结构,它应该可以满足您的要求,无论速度有多慢。
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "InFile=text.txt"
Set "LineLimit=1000"
Set "OutFile=output_text.txt"
If Not Exist "%InFile%" Exit /B
(
For /F Delims^= %%G In (
'%SystemRoot%\System32\findstr.exe /N /R "^" "%InFile%" 2^>NUL'
) Do (
Set "}=%%G"
Set /A "{=%%G" 2>NUL
SetLocal EnableDelayedExpansion
If !{! Gtr %LineLimit% (
EndLocal
GoTo Next
)
Echo(!}:*:=!
EndLocal
)
) 1>"%OutFile%"
:Next
听起来您想要一个 head
程序。这在 PowerShell 中很容易完成。
Get-Content -Path '.\text.txt' -First 1000 >'.\output_text.txt'
如果您必须从 cmd.exe
运行,可以使用以下内容。
powershell -NoLogo -NoProfile -Command ^
"Get-Content -Path '.\text.txt' -First 1000 >'.\output_text.txt'"
我有充满 URL 的文本文档,当我计算文件中的行数时,一旦行数大于 1000
,我想删除那些后面的行,保留第一 1000
行。
这是我的代码,但我不知道我做错了什么。
使用此脚本位置创建一个 text.txt
文件并将 1000
行垃圾放入其中进行测试。
@ECHO OFF & setLocal EnableDelayedExpansion
color 0A
%*
SET root_path=%~dp0
set text_name=text.txt
set text_name_output=output_text.txt
set max_line_count=1000
set /a counter=0
for /f %%a in (%root_path%%text_name%) do (
set /a counter += 1
echo Number of lines: !counter!
if !counter! LSS %max_line_count% (
echo less than
) else (
echo more than so delete these line numbers
(for /f "tokens=1,* delims=[]" %%a in ('type %root_path%%text_name%^|find /v /n ""') do (
echo/%%a|findstr /x "!counter!" >nul || echo/%%b
))>%root_path%%text_name_output%
)
)
pause
我想通了,如果文本或文档超过 1000 行,则删除后面的所有行。
@ECHO OFF & setLocal EnableDelayedExpansion
color 0A
%*
SET root_path=%~dp0
set text_name=text.txt
set text_name_output=output_text.txt
set max_line_count=1000
set /a counter=0
for /f %%a in (%root_path%%text_name%) do set /a counter+=1
set /a "counter=%counter%-%max_line_count%"
more +%counter% "%root_path%%text_name%" >"%root_path%%text_name_output%"
move /y "%root_path%%text_name_output%" "%root_path%%text_name%" >nul
pause
为此任务使用批处理文件是绝对不合适的,但是,这里有一个基本结构,它应该可以满足您的要求,无论速度有多慢。
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "InFile=text.txt"
Set "LineLimit=1000"
Set "OutFile=output_text.txt"
If Not Exist "%InFile%" Exit /B
(
For /F Delims^= %%G In (
'%SystemRoot%\System32\findstr.exe /N /R "^" "%InFile%" 2^>NUL'
) Do (
Set "}=%%G"
Set /A "{=%%G" 2>NUL
SetLocal EnableDelayedExpansion
If !{! Gtr %LineLimit% (
EndLocal
GoTo Next
)
Echo(!}:*:=!
EndLocal
)
) 1>"%OutFile%"
:Next
听起来您想要一个 head
程序。这在 PowerShell 中很容易完成。
Get-Content -Path '.\text.txt' -First 1000 >'.\output_text.txt'
如果您必须从 cmd.exe
运行,可以使用以下内容。
powershell -NoLogo -NoProfile -Command ^
"Get-Content -Path '.\text.txt' -First 1000 >'.\output_text.txt'"