Rob
Rob

Reputation: 151

Batch file to remove data from a file

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.

Upvotes: 2

Views: 59

Answers (1)

npocmaka
npocmaka

Reputation: 57242

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

Upvotes: 1

Related Questions