delete lines in textfile that comes precedes a word

April 25, 2015 at 06:55:59
Specs: Windows 7
I have many text files in a path. I want to write a Batch script for the first time and am struck.

The requirement is: I want to delete all the lines in a text file that comes after the line that starts with the letters "TRAI......".

Example: My test file looks like:

123 sdefef dhufheij 123232
234 ddefef mijijijj 232323
345 jcdhence 345987
TRAILER0000034
456 edrftg nbuyfjjf 678655

Result should be:

123 sdefef dhufheij 123232
234 ddefef mijijijj 232323
345 jcdhence 345987


See More: delete lines in textfile that comes precedes a word

Report •


#1
April 25, 2015 at 08:19:13
Here your assignment and do not post twice!

@echo off & setlocal EnableDelayedExpansion

cd /D Here your path to the target directory
set STRING=Here your letter/string to check
set N=Here the lenght of the previous string

for %%i in (*.txt) do call :READ "%%i"
goto :EOF
:READ
for /F "delims=" %%j in ('type "%~1"') do (
  set row=%%j
  if "!row:~0,%N%!"=="%STRING%" (goto :EOF) else (echo.!row!)
) >> "%~n1.new"
goto :EOF

WARNING: Script edited to remove two minor bugsl

message edited by IVO


Report •

#2
April 25, 2015 at 08:24:15
Thanks a lot.. Between, if i want to check for all .txt files ina given bath, this works? What is N= length od previous string mean. What value should i give there. Please explain.

Report •

#3
April 25, 2015 at 08:34:33
The script is coded to process all files in a given path. Set N at the lenght of the string you code in variable STRING, e.g. STRING=cat N=3 - STRING=college N=7.

Beware the script is not tested, you have to test it.


Report •

Related Solutions

#4
April 26, 2015 at 07:48:22
Its not working.. :(

Report •

#5
April 26, 2015 at 07:58:32
Watch again and accurately the script I posted; I tested it and after removing two minor bugs due to a typo it worked perfectly. I used your sample data coding STRING=TRIA and N=4.

Report •

#6
April 26, 2015 at 17:09:55
Hi IVO, Thanks lot for ur time. I have one doubt. "%~n1.new" used in the last line of a script is the new file into which all lines above the line where TRAIL% exist is written into? IF i want to write the files to the same file?

Report •

#7
April 27, 2015 at 02:41:12
A batch script can't update the file it is reading since sequential access only is allowed. So you have to write the output stream to a new file and then rename deleting the original source.

In your case an input named e.g. FileText.txt is written as FileText.new (%~n1.new).
To have output named as the original input add the following two
lines after the first for statement before the first goto :EOF. Beware this deletes your original source files.


del *.txt
ren *.new *.txt

message edited by IVO


Report •

#8
April 27, 2015 at 07:20:50
IVO,

Thanks for ur patience. edited to save the .new output files as .doc files... when I open the .doc files generated.. it just shows !row!
ECHO is off.
!row!
ECHO is off.
!row!
ECHO is off.
!row!
ECHO is off.
!row!
ECHO is off.
!row!
ECHO is off.

Where are the lines of data.. Am wondering.. Can u check this. Plz

message edited by radhiramya


Report •

#9
April 27, 2015 at 08:06:52
Let us examine the situation ordinately

- Did you copy and paste in notepad the script I posted in post #1?
- Did you code correctly the full path of the directory holding the text files to be processed? (e.g. if files are in test then C:\User\Sam Taylor\docs\test)
- String and N seem to be correct assumed TRAILER is at the beginning of the marking line (pay attention uppercase/lowercase makes difference)..

Examine some file with .txt and .new extension to be sure of their contents. I repeat I tested the script and it performed perfectly.


Report •

#10
April 27, 2015 at 09:02:10
What you post in your edited message suggests the first line is not correct especially the setlocal EnableDelayedExpansion that enables the dynamic variables marked by ! and the echo is off suggests a wrong coding of an echo command.

Report •

#11
April 27, 2015 at 09:03:58
Typed the script looking @ yours. It is exact.
Yes, I have given the correct path
Also, a line starts with word TRAILER along with numbers (Eg: TRAILER12342324) and is in upper case. I can send u my file if u have a mail id to share.
Thank u so much, IVO.

Report •

#12
April 27, 2015 at 11:44:12
It works... Thanks a lot... IVO...

Report •

#13
April 27, 2015 at 12:30:16
Please, just curious, may you report the problem blocking the script? More let me know your location and obviously ignore my previous private message.

Ciao (Bye) Ivo Luigi Bertani - Milano (Italy - EU)


Report •

Ask Question