Forum Discussion

poffin's avatar
poffin
Occasional Contributor
8 years ago

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

    • poffin's avatar
      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?

      • Colin_McCrae's avatar
        Colin_McCrae
        Community Hero

        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's avatar
    Marsha_R
    Champion Level 3
    Just blank out the original file before you save the new string over it.
  • 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's avatar
    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;