Ask a Question

Remove text from a file?

poffin
Occasional Contributor

Remove text from a file?

All I want to do is remove a string from a certain text file. Here's the best way I can think to do that:

 

1. Rename file.

2. Open a new empty text file in its place.

3. Read original file, and write to the empty file line-by-line until I come across the text I want to remove.

4. Skip that text, and keep writing to the new file until I get to the end.

 

Are there any simpler or more elegant ways just to remove a few characters? I could overwrite them, but that only works if the output file contains more characters than the input. All I want to do is delete, not add anything.

6 REPLIES 6
Marsha_R
Community Hero

You can use

aqFile.ReadWholeTextFile

to put the text into a variable then

aqString.Find

to see if the text you want to remove is there and get its location then

aqString.Remove

to get it out and

aqFile.WriteToTextFile

to put the string back to a file

 

https://support.smartbear.com/viewarticle/88752/

https://support.smartbear.com/viewarticle/88987/

 

 

 

 

 

 


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
poffin
Occasional Contributor

Thank you for your suggestion! aqFile.WriteToTextFile can do this via overwrite, but what if the original starting text file was, say, "My favorite color is neon orange" and I wanted to remove 'neon', wouldn't the output be: "My favorite color is orangeorange" Because the original string is longer (32 chars) than the string I want to output (27), only the first 27 characters will get overwritten and the last remaining ones will not get touched, right?

If thats the case, could you not then:

 

1. Check the length of the read in input string.

2. Replace your text.

3. Check the length of the string with the replacement text.

4. If the length is shorter, add on the appropriate number of spaces to the output string (original length - new length = number of spaces to add)

5. Write new text to original file.

 

I guess it will still have spaces at the end. But unless they cause problems elsewhere, should be OK?

Marsha_R
Community Hero

Just blank out the original file before you save the new string over it.

Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
StevenN
Contributor

1. Open and read file.

2. Replace string using aqString["Replace"], code below replaces "neon " with "".

3. Write the updated string to the text file. 

 

function Test()
{
  var fileToUpdate = "C:\\Temp\\Text File.txt";
  var textToRemove = "neon ";

  // Open text file
  var openedTextFile = aqFile["OpenTextFile"](fileToUpdate, aqFile["faRead"], aqFile["ctANSI"]);
  // Read the contents
  var contents = openedTextFile["ReadAll"]();
  // Replace the text
  var updatedString = aqString["Replace"](contents, textToRemove, "");
  // Write updated string to text file
  aqFile["WriteToTextFile"](fileToUpdate, updatedString, aqFile["ctANSI"], true);
}

 

When the aqFile["WriteToTextFile"] OverwriteOrCreate parameter is set to true then it will overwrite everything in the file. So the Text File.txt will end up as "My favourite colour is orange".

m_essaid
Valued Contributor

hi poffin,

 

you may try this

 

sourceFile:= aqFile.OpenTextFile('theFile.txt', aqFile.faRead, aqFile.ctANSI);
destinationFile:= aqFile.Create('theOtherFile.txt');
destinationFile:= aqFile.OpenTextFile('theOtherFile.txt', aqFile.faWrite, aqFile.ctANSI);
sourceFile:= 0;
while not sourceFile:= .IsEndOfFile() do
begin
Line:= sourceFile:= .ReadLine();
destinationFile:= aqString.Replace(Line, 'theStringToRemove', '');
end;
sourceFile.Close;
destinationFile.Close;
end;

cancel
Showing results for 
Search instead for 
Did you mean: