2

I am trying to remove any data from a file after the entry [TEST]

E.g.

The text file is:

Random Text 1

Random Text 2

Random Text 3

[TEST] Random Text 4

Random Text 5

Random Text 6 ....

After I run the batch file I just want it to remove any data after the string [TEST] so the new file will look like:

Random Text 1

Random Text 2

Random Text 3

Any help is greatly appreciated.

Community
  • 1
  • 1
Rob
  • 151
  • 1
  • 2
  • 11
  • [replace text in file](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) . Just replace `[TEST]` with empty string. – npocmaka May 03 '16 at 13:17
  • 1
    Wouldn't that just remove [TEST] ? Wouldn't I still be left with a file with data after that point? – Rob May 03 '16 at 13:24
  • aah. Misunredstood the question. – npocmaka May 03 '16 at 13:31
  • Use a `for /F` loop to read the text file; in the loop body, check the line for occurrence of `[TEST]`; if not found, echo the line; if found, put `goto :Label`; place the target `:Label` after the loop... – aschipfl May 03 '16 at 13:33

1 Answers1

1
@echo off

set file_to_process=#

for /f "skip=2 tokens=1 delims=[]" %%# in  ('find /i /n "[test]" "%file_to_process%"') do (
    set line=%%#
    goto :break_for
)
:break_for
echo %line%

break>"%temp%\empty"&&fc "%temp%\empty" "%file_to_process%" /lb  %line% /t |more +4 | findstr /B /E /V "*****" > temp

rem move temp "%file_to_process%"

set the path to the file you want to process at the second line.If content in the temp file is ok you can uncomment the last line.

npocmaka
  • 55,367
  • 18
  • 148
  • 187