运行 迭代文本文件的批处理命令
Run Batch Command on Iterative Text Files
我有一组文本文件(log0.txt、log1.txt 等),我想将其转换为不同的格式。但是,每个文件的最后一行是不完整的,所以我想写一个批处理命令来删除每个文件的最后一行。
我开始工作的一般命令如下所示:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
rem Count the lines in file
set /a count=-1
for /F %%a in (log0.txt) DO set /a count=!count!+1
rem Create empty temp file
copy /y NUL temp.txt >NUL
rem Copy all but last line
for /F %%a in (log0.txt) DO (
IF /I !count! GTR 0 (
echo %%a >>temp.txt
set /a count=!count!-1
)
)
rem overwrite original file, delete temp file
copy /y temp.txt log0.txt >NUL
del temp.txt
rem This for testing
type log0.txt
有没有办法让批处理命令对我的所有文本文件进行操作,而不是为每个文本文件复制和粘贴?
将您的代码重建为一个函数。
@echo off
for %%F in (*.txt) do (
call :removeLastLine "%%~F"
)
exit /b
:removeLastLine
SETLOCAL ENABLEDELAYEDEXPANSION
set "filename=%~1"
echo Processing '!filename!'
rem Count the lines in file
set /a count=-1
for /F %%a in (!filename!) DO set /a count+=1
rem Copy all but last line
(
for /F %%a in (!filename!) DO (
IF /I !count! GTR 0 (
echo(%%a
set /a count=!count!-1
)
)
) > temp.txt
rem overwrite original file, delete temp file
copy /y temp.txt !filename! >NUL
del temp.txt
rem This for testing
type !filename!
endlocal
exit /b
排除最后一行可能有更简单的方法。我修改了你的代码,添加了对所有文本文件的处理。
@echo off
setlocal EnableDelayedExpansion
rem Process all text files
for %%f in (*.txt) do (
echo Processing: %%f
rem Copy all but last line in temp.txt file
set "line="
(for /F %%a in (%%f) do (
if defined line echo !line!
set "line=%%a"
)) > temp.txt
rem Overwrite original file
move /Y temp.txt %%f >NUL
rem This for testing
type %%f
)
使用 powershell 脚本可能比您的方法更快。将以下脚本放入名为 allbutlast.ps1:
的文件中
$content = Get-Content $args[0]
$lines = $content | Measure-Object
$content | select -First ($lines.count-1)
然后从你的批处理文件中调用它:
powershell -file allbutlast.ps1 log0.txt>temp.txt
copy /y temp.txt log0.txt >NUL
del temp.txt
我有一组文本文件(log0.txt、log1.txt 等),我想将其转换为不同的格式。但是,每个文件的最后一行是不完整的,所以我想写一个批处理命令来删除每个文件的最后一行。
我开始工作的一般命令如下所示:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
rem Count the lines in file
set /a count=-1
for /F %%a in (log0.txt) DO set /a count=!count!+1
rem Create empty temp file
copy /y NUL temp.txt >NUL
rem Copy all but last line
for /F %%a in (log0.txt) DO (
IF /I !count! GTR 0 (
echo %%a >>temp.txt
set /a count=!count!-1
)
)
rem overwrite original file, delete temp file
copy /y temp.txt log0.txt >NUL
del temp.txt
rem This for testing
type log0.txt
有没有办法让批处理命令对我的所有文本文件进行操作,而不是为每个文本文件复制和粘贴?
将您的代码重建为一个函数。
@echo off
for %%F in (*.txt) do (
call :removeLastLine "%%~F"
)
exit /b
:removeLastLine
SETLOCAL ENABLEDELAYEDEXPANSION
set "filename=%~1"
echo Processing '!filename!'
rem Count the lines in file
set /a count=-1
for /F %%a in (!filename!) DO set /a count+=1
rem Copy all but last line
(
for /F %%a in (!filename!) DO (
IF /I !count! GTR 0 (
echo(%%a
set /a count=!count!-1
)
)
) > temp.txt
rem overwrite original file, delete temp file
copy /y temp.txt !filename! >NUL
del temp.txt
rem This for testing
type !filename!
endlocal
exit /b
排除最后一行可能有更简单的方法。我修改了你的代码,添加了对所有文本文件的处理。
@echo off
setlocal EnableDelayedExpansion
rem Process all text files
for %%f in (*.txt) do (
echo Processing: %%f
rem Copy all but last line in temp.txt file
set "line="
(for /F %%a in (%%f) do (
if defined line echo !line!
set "line=%%a"
)) > temp.txt
rem Overwrite original file
move /Y temp.txt %%f >NUL
rem This for testing
type %%f
)
使用 powershell 脚本可能比您的方法更快。将以下脚本放入名为 allbutlast.ps1:
的文件中$content = Get-Content $args[0]
$lines = $content | Measure-Object
$content | select -First ($lines.count-1)
然后从你的批处理文件中调用它:
powershell -file allbutlast.ps1 log0.txt>temp.txt
copy /y temp.txt log0.txt >NUL
del temp.txt