Announcement

Collapse
No announcement yet.

Find & Replace

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Find & Replace

    Hi All,

    I need to replace "Robert" with "Adam" in all *.txt files of a directory structure (including subfolders)

    I am using the code below.

    1) What parameter I need to pass in the !!! highlighted !!! row?
    2) Can I do a single pass and process both *.txt and *.dat files?

    Code:
    for x, cFile in pairs(File.Find(SessionVar.Expand("%SamplesFolder%\\"), "*.txt", true, true)) do
          
          -- Read the contents of a text file into a string.
          contents = TextFile.ReadToString(File);  !!!! PROBLEM HERE !!!!
    
    
          -- Check the error code of the last example.
          error = Application.GetLastError();
          
          -- If an error occurred, display the error message.
          if (error ~= 0) then
            Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
          else
            -- Replace every occurrence of the string "Robert" with the string "Adam".
            new_contents = String.Replace(contents, "Robert", "Adam", true);
    
            -- Write out the modified contents of the text file.
            TextFile.WriteFromString("File", new_contents, false);
    
            -- Check the error code of the last example.
            error = Application.GetLastError();
            -- If an error occurred, display the error message.
            if (error ~= 0) then
               Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
          end
       end

  • #2
    "File" is not the same as "cFile"

    Comment


    • #3
      Hi Jassing,

      Even cFile is wrong. It returns a path, not a path+filename. I need the code to find all txt files inside a complex directory structure and replace a string inside them. Please provide it.

      Thanks,

      Alberto

      Comment


      • #4
        You need to wrap it into a function so that you can call it recursively.
        When you have a folder, you need to then concatenate the base string with the cFile value and then call the function recursively.
        When you have a file, you need to concatenate the base string with cFile and then do your file modification.

        You really should more closely follow the examples in the help file -- your method is very slow. Use variables instead of embedding multiple calls at once -- there is nothing to gain by doing it the way you are.

        Comment


        • #5
          I need a ready to use solution now. I will pay with PayPal.

          Same is true for the other post on the gacutil.exe

          Please pm me with a PayPal account as soon as you have both the solutions ready.

          Alberto

          Comment

          Working...
          X