Remove text from a file?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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".
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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;
