0

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

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

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

我已经搜索过了,但有些命令超出了我的想象。谁能给我一个完整的批处理文件的剪切和粘贴打开,拜托。

4

2 回答 2

3

我相信这是您正在寻找的代码(在批处理文件中)以删除“劳动”一词上方的所有行。让我知道是否需要对代码进行修改(例如文件中是否有多个“劳动力”实例)。

@echo OFF

setLocal EnableDelayedExpansion

cd C:\export
for /f "delims=" %%I in ('findstr /inc:"labour" "test.trs"') do (
set /A"line=%%I"
)

set count=0

for /f "delims=" %%A in (test.trs) do (
If !count! GEQ %line% goto ExitLoop
echo %%A >>temp.txt
set /A count+=1
echo !count!
)

:ExitLoop

type temp.txt > test.trs
del temp.txt

endlocal

输出:

test.trs(更改前)

this
is
a
labour
test
of
the
results

test.trs(更改后)

this
is
a
于 2012-10-01T19:10:03.027 回答
3

这是处理“C:\export”中每个 .trs 文件的另一种方法:

@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
)

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

我使用' findstr /inc:"labour" "C:\export\%%A" ' 来获取在当前文件中找到 "labour" 的行号,然后将其通过管道传递到 ' findstr /n .* ' 以进行编号找到多个匹配项时的结果。

然后,我使用带有“ tokens=1,2 delims=: ”的 for 循环来查找第一个结果(if "%%B" equ "1")并存储行号(set LineNumber=%%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.
)
于 2012-10-26T06:42:48.187 回答