使用批处理删除文本文件的行

Delete line of a text file with batch

我看到,要从文本文件中批量删除特定行,您需要使用 findstr 来完成,这样您就可以找到您的行,然后将其删除。但是当你不知道这条线时有没有办法做到这一点?我得到了另一个填充文件的程序,批处理必须删除第一行。有谁知道怎么做吗?

我尝试了一些东西,它们从索引中读取一行,然后使用我通过 findstr 得到的东西,但它不起作用:

@echo off
setlocal EnableDelayedExpansion
set count=1
for /f "tokens=*" %%a in (test.txt) do (
if !count! equ 1 (set "TIMER=%%a")
if !count! equ 1 (type test.txt | findstr /v %TIMER%)
set /a count+=1
)
echo %TIMER%
timeout %TIMER%
for /f "tokens=*" %%a in (test.txt) do (
echo %%a
)
pause

它告诉我:FINDSTR:错误的命令行 (在文件的行上循环并找到特定行的代码是在网上找到的)

那么问题出在哪里呢?或者也许有人知道像 delete(x) 这样的东西并且它删除了行?只是需要索引的东西... ^^'

(最后一个for循环用于检查该行是否被删除)

提前感谢您的帮助!

你的代码不能运行主要是因为缺少.

但是有更简单的方法可以实现您的目标。如果 Compo 的建议 (more.com" +1 "test.txt" > "modifiedtest.txt") 对您不起作用(需要保留 TAB 或需要删除第一行以外的另一行),以下内容可能对您有用:

@echo off
setlocal
set file=test.txt
set lineToDelete=1
(for /f "tokens=1* delims=:" %%a in ('findstr /N "^" "%file%" ^|findstr /bv "%lineToDelete%:"') do echo/%%b) > "%file%.tmp
move /y "%file.tmp%" "%file%"

要从文件中删除前 n 行而不将制表符转换为空格:

@echo off
>RemoveFirstLine.txt <test.txt (
    FOR /L %%N in (1 1 1) do set/p"=" SKIP N lines
    %__APPDIR__%findstr.exe /R "^"
)

这与 FINDSTR moves the pointer from STDIN 一样,而 FINDMORE 则不然。