Batch file 批处理文件,以删除特定行下面的所有行

Batch file 批处理文件,以删除特定行下面的所有行,batch-file,lines,strip,Batch File,Lines,Strip,我正在寻找一个批处理或VBS解决方案,以剥离出扩展名为.trs的程序生成的文本文件中的行 在创建的每个.trs文件中,都有一行包含“人工”一词。我需要删除包含单词labour的行之后的每一行 .trs文件都存储在c:\export中 我已经搜索过了,但是有些命令我已经听不懂了。有谁能帮我剪贴打开整个批处理文件吗?我相信这就是你正在寻找的代码(在批处理文件中)来删除“人工”一词上面的所有行。让我知道是否需要对代码进行修改(例如,如果文件中有多个“人工”实例) 输出: test.trs(更改前)

我正在寻找一个批处理或VBS解决方案,以剥离出扩展名为.trs的程序生成的文本文件中的行

在创建的每个.trs文件中,都有一行包含“人工”一词。我需要删除包含单词labour的行之后的每一行

.trs文件都存储在c:\export中


我已经搜索过了,但是有些命令我已经听不懂了。有谁能帮我剪贴打开整个批处理文件吗?

我相信这就是你正在寻找的代码(在批处理文件中)来删除“人工”一词上面的所有行。让我知道是否需要对代码进行修改(例如,如果文件中有多个“人工”实例)


输出:

test.trs(更改前)

test.trs(更改后)


我相信这是您正在寻找的代码(在批处理文件中),用于删除单词“labour”上方的所有行。让我知道是否需要对代码进行修改(例如,如果文件中有多个“人工”实例)


输出:

test.trs(更改前)

test.trs(更改后)


以下是处理“C:\export”中每个.trs文件的替代方法:

首先,我做了一些错误检查,以避免发生会破坏脚本的事情。接下来,我提取存储在C:\export中的.trs文件列表,并循环遍历每个文件

我使用“findstr/inc:“labour”“C:\export\%%A””获取当前文件中“labour”所在的行号,然后将其导入“findstr/n.”以对结果进行编号,以防找到多个匹配项

然后我使用带有“标记=1,2 delims=:”的for循环来查找第一个结果(如果“%%B”eq“1”),并存储行号(设置行号=%%C

接下来,我使用“findstr/n.*”C:\export\%%A“”读取文件的每一行,“tokens=1*delims=:”再次分离行号,然后将所有数据复制到临时文件,直到达到%LineNumber%为止。这种读取文件的方法(使用findstr并对行进行编号)还可以确保for循环不会跳过任何空行

最后,我用临时文件替换原始文件,然后循环到下一个文件

我试图尽可能精简上述代码。以下是相同的脚本,包含格式、注释、视觉反馈和用户可定义变量:

@echo off

::Set user-defined Variables
set FilePath=C:\export
set FileType=*.trs
set Keyword=labour

::Check for files to process and exit if none are found
if not exist "%FilePath%\%FileType%" echo Error. No files to process.&goto :EOF

::Delete temp file if one already exists
if exist "%FilePath%\queue.tmp" del /q "%FilePath%\queue.tmp"

::List all files in the above specified destination, then process them one at a time
for /f "tokens=*" %%A in ('dir /b "%FilePath%\%FileType%"') do (
  ::Echo the text without a line feed (so that "Done" ends up on the same line)
  set /p NUL=Processing file "C:\export\%%A"... <NUL

  ::Search the current file for the specified keyword, and store the line number in a variable
  for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"%Keyword%" "%FilePath%\%%A" ^| findstr /n .*') do (
    if "%%B" equ "1" set LineNumber=%%C
  )>NUL

  ::Output all data from the current file to a temporary file, until the line number found above has been reached
  for /f "tokens=1* delims=:" %%D in ('findstr /n .* "%FilePath%\%%A"') do (
    if %%D lss %LineNumber% echo.%%E>>"%FilePath%\queue.tmp"
  )>NUL

  ::Replace the current file with the processed data from the temp file
  move /y "%FilePath%\queue.tmp" "%FilePath%\%%A">NUL
  echo Done.
)
@echo关闭
::设置用户定义的变量
设置FilePath=C:\export
设置文件类型=*.trs
set关键字=人工
::检查要处理的文件,如果找不到,则退出
如果不存在“%FilePath%\%FileType%”回显错误。没有要处理的文件。&转到:EOF
::删除临时文件(如果已存在)
如果存在“%FilePath%\queue.tmp”del/q“%FilePath%\queue.tmp”
::列出上述指定目标中的所有文件,然后一次处理一个文件
对于('dir/b“%FilePath%\%FileType%”中的/f“tokens=*”%%A,请执行以下操作(
::在没有换行符的情况下回显文本(以便“完成”结束于同一行)
set/p NUL=处理文件“C:\export\%A”…NUL
::将当前文件中的所有数据输出到临时文件,直到达到上面找到的行号为止
对于/f“tokens=1*delims=:”%%D in('findstr/n.*'%FilePath%\%%A')do(
如果%%D lss%LineNumber%echo.%%E>>%FilePath%\queue.tmp
)>努尔
::使用临时文件中已处理的数据替换当前文件
移动/y“%FilePath%\queue.tmp”“%FilePath%\%%A”>NUL
回音完毕。
)

以下是处理“C:\export”中每个.trs文件的替代方法:

首先,我做了一些错误检查,以避免发生会破坏脚本的事情。接下来,我提取存储在C:\export中的.trs文件列表,并循环遍历每个文件

我使用“findstr/inc:“labour”“C:\export\%%A””获取当前文件中“labour”所在的行号,然后将其导入“findstr/n.”以对结果进行编号,以防找到多个匹配项

然后我使用带有“标记=1,2 delims=:”的for循环来查找第一个结果(如果“%%B”eq“1”),并存储行号(设置行号=%%C

接下来,我使用“findstr/n.*”C:\export\%%A“”读取文件的每一行,“tokens=1*delims=:”再次分离行号,然后将所有数据复制到临时文件,直到达到%LineNumber%为止。这种读取文件的方法(使用findstr并对行进行编号)还可以确保for循环不会跳过任何空行

最后,我用临时文件替换原始文件,然后循环到下一个文件

我试图尽可能精简上述代码。以下是相同的脚本,包含格式、注释、视觉反馈和用户可定义变量:

@echo off

::Set user-defined Variables
set FilePath=C:\export
set FileType=*.trs
set Keyword=labour

::Check for files to process and exit if none are found
if not exist "%FilePath%\%FileType%" echo Error. No files to process.&goto :EOF

::Delete temp file if one already exists
if exist "%FilePath%\queue.tmp" del /q "%FilePath%\queue.tmp"

::List all files in the above specified destination, then process them one at a time
for /f "tokens=*" %%A in ('dir /b "%FilePath%\%FileType%"') do (
  ::Echo the text without a line feed (so that "Done" ends up on the same line)
  set /p NUL=Processing file "C:\export\%%A"... <NUL

  ::Search the current file for the specified keyword, and store the line number in a variable
  for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"%Keyword%" "%FilePath%\%%A" ^| findstr /n .*') do (
    if "%%B" equ "1" set LineNumber=%%C
  )>NUL

  ::Output all data from the current file to a temporary file, until the line number found above has been reached
  for /f "tokens=1* delims=:" %%D in ('findstr /n .* "%FilePath%\%%A"') do (
    if %%D lss %LineNumber% echo.%%E>>"%FilePath%\queue.tmp"
  )>NUL

  ::Replace the current file with the processed data from the temp file
  move /y "%FilePath%\queue.tmp" "%FilePath%\%%A">NUL
  echo Done.
)
@echo关闭
::设置用户定义的变量
设置FilePath=C:\export
设置文件类型=*.trs
set关键字=人工
::检查要处理的文件,如果找不到,则退出
如果不存在“%FilePath%\%FileType%”回显错误。没有要处理的文件。&转到:EOF
::删除临时文件(如果已存在)
如果存在“%FilePath%\queue.tmp”del/q“%FilePath%\queue.tmp”
::列出上述指定目标中的所有文件,然后一次处理一个文件
对于('dir/b“%FilePath%\%FileType%”中的/f“tokens=*”%%A,请执行以下操作(
::在没有换行符的情况下回显文本(以便“完成”结束于同一行)
set/p NUL=处理文件“C:\export\%A”…NUL
::将当前文件中的所有数据输出到临时文件,直到达到上面找到的行号为止
对于/f“tokens=1*delims=:”%%D in('findstr/n.*'%FilePath%\%%A')do(
如果%%D lss%LineNumber%echo.%%E>>%FilePath%\queue.tmp
)>努尔
::使用临时文件中已处理的数据替换当前文件
移动/y“%FilePath%\queue”。
this
is
a
@echo off
if not exist "C:\export\*.trs" goto :EOF
if exist "C:\export\queue.tmp" del /q "C:\export\queue.tmp"
for /f "tokens=*" %%A in ('dir /b "C:\export\*.trs"') do (
  for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"labour" "C:\export\%%A" ^| findstr /n .*') do if "%%B" equ "1" set LineNumber=%%C
  for /f "tokens=1* delims=:" %%D in ('findstr /n .* "C:\export\%%A"') do if %%D lss %LineNumber% echo.%%E>>"C:\export\queue.tmp"
  move /y "C:\export\queue.tmp" "C:\export\%%A">NUL
)
@echo off

::Set user-defined Variables
set FilePath=C:\export
set FileType=*.trs
set Keyword=labour

::Check for files to process and exit if none are found
if not exist "%FilePath%\%FileType%" echo Error. No files to process.&goto :EOF

::Delete temp file if one already exists
if exist "%FilePath%\queue.tmp" del /q "%FilePath%\queue.tmp"

::List all files in the above specified destination, then process them one at a time
for /f "tokens=*" %%A in ('dir /b "%FilePath%\%FileType%"') do (
  ::Echo the text without a line feed (so that "Done" ends up on the same line)
  set /p NUL=Processing file "C:\export\%%A"... <NUL

  ::Search the current file for the specified keyword, and store the line number in a variable
  for /f "tokens=1,2 delims=:" %%B in ('findstr /inc:"%Keyword%" "%FilePath%\%%A" ^| findstr /n .*') do (
    if "%%B" equ "1" set LineNumber=%%C
  )>NUL

  ::Output all data from the current file to a temporary file, until the line number found above has been reached
  for /f "tokens=1* delims=:" %%D in ('findstr /n .* "%FilePath%\%%A"') do (
    if %%D lss %LineNumber% echo.%%E>>"%FilePath%\queue.tmp"
  )>NUL

  ::Replace the current file with the processed data from the temp file
  move /y "%FilePath%\queue.tmp" "%FilePath%\%%A">NUL
  echo Done.
)