Find/Replace in files batch process?

Find/Replace in files batch process?

6

    Jun 03, 2005#1

    Is it possible to do a batch find/replace in files such that multiple changes are made to multiple files recursively in a folder structure using a predefined table of values in CSV or tab delimited format. For example:

    Find XXXX >> Replace with YYYYY:

    ala_88819 >> ala_143047
    ala_88818 >> ala_143046
    ala_88817 >> ala_143045
    ala_88815 >> ala_143043
    ala_88816 >> ala_143044
    ala_88813 >> ala_143041
    ala_88814 >> ala_143042
    ala_95507 >> ala_143057
    ala_95509 >> ala_143059
    ala_95505 >> ala_143055
    ala_88821 >> ala_143062
    and so on for another 900 some odd changes...

    I suppose just about anything can be done with a macro, but how can one write such a macro if the values on the right side of the table are going to change constantly?

    Thanks in advance.

    Paul

    Stephen_Hatton
    Stephen_Hatton

      Jun 09, 2005#2

      Hi All and Pbnyc

      I too, am trying to find a method of batch replacing in HTML files of links to other pages.

      I have 1100 HTML pages with the possibility that each page has a filename change requiring all the links (upto 1100) to be updated.

      I read somewhere that macro's have a 30Kbyte limit, so a single macro can only change say 100 link names. However a batch macro can call up mutiple single macros.

      This creates a bit of work:
      1. I have to make a search/replace script suitable for the macro.
      2. Divide it up into chunks to put into individual macros (say macro001 to macro012)
      3. Load up say 100 files at a time and apply the macro to all openfiles.
      4. Load up the next set of 100 files and repeat.

      It would be nice to have a macro replace the 1100 links in one hit thoughout the files one by one automatically.

      I would love to be able to feed the macro with a replacement file in this format:

      page001 "tab" page001new
      page002 "tab" page002z
      page003 "tab" page003last

      etc.


      I tried the multi file search & replace (ie. *.html) with the variables, but it took more than 1 hour for 40 HTML files.

      Regards
      Ing. Stephen Hatton
      :idea:

      112
      Power UserPower User
      112

        Jun 09, 2005#3

        Guys,

        Look up the Find command in the help - you'll see the ^c special character. It uses the contents of the clipboard to find things...

        You can create the list of items to search and replace in a file with a suitable separator (such as the tab).

        You then run a macro that switches to the list window, picks up the first search string into the clipboard, switches back to the other window(s) and looks for the string. if it finds it, it deletes the selected text, switches back to the list window, picks up the replacement text and inserts at the deletion point. Repeat down the file... At end of file, move to next item in the search and replace window (perhaps by deleting the first line and going to top), Repeat...

        I hope that's enough to give you an idea of the process.

        I did something similar, but I can't find the macro that did it! :(

        HTH,
        Paolo
        There is no such thing as an inconsistently correct system...
        Therefore, aim for consistency; in the expectation of reaching correctness!

        Stephen_Hatton
        Stephen_Hatton

          Jun 10, 2005#4

          Hi PaoloFCantoni

          Thanks for the reply. I haven't used ^c yet in the search replace command yet, so I will experiment.

          One problem I see is having 1100 files. Ultraedit can't open that many....... I just had a brain wave......

          I will make a macro to open one file at a time (massaged from a directory dump) and run a subroutine macro to search and replace in the file.....

          If I find a simpler solution than what I have at this moment I will post it.

          Got to try something!

          Thanks again.

          Regards
          Ing. Stephen Hatton

          :idea:

          Stephen_Hatton
          Stephen_Hatton

            Jun 13, 2005#5

            Hi Sten

            Thanks.  I know there are many methods of solving this problem, however my programming skills are limited and I don't have the time to learn any more languages as I am trying to setup a shopping cart which has many issues and is consuming most of my time.

            I wanted to use a tool like UE to manage & massage the files precisely they way I want our website to become.

            I could buy programs that do this (and there are 2 or 3 that I have found) but every program has limits and prevent you from being efficient.  UE is the most flexible and proven in file management.  

            One of the main limits with UE is that the macro's are limited to 30K bytes, so a search and replace script for 1000+ items does not fit into one macro and has to be split up manually over 4-8 macro names.

            So I have made macros that
            1.  Creates the search and replace scripts for the HTML links that fit into each of these smaller macros (however it still requires manual cut and paste)
            2.  Runs a batch process of calling these macros and replaces all the terms.
            3.  Renaming the HTML files to the final names before putting them up on the web (through a DOS batch process).

            Why all this effort you may ask?
            1.  My shopping cart is dynamic (php, MySQL, Javascript, smarty templates) so the pages created have abbreviatednames and not easily searched names (ie. not easy for people to find and not search engine friendly).
            2.  The cart does produce HTML catalog files for people to browse which are search engine friendly, but it puts all the generated html files away from the root directory:  http://www.mysite.com/mystore/catalog    which is not search engine friendly.
            3.  So to move all the pages to the root: http://www.mysite.com/    requires all the links to be replaced.  UE does this easily.
            4.  While I am at it I might as well take control of the HTML file names and make sure that they are search friendly and that they don't change over time (ie. give dead links).
            5.  And, finally, I can create a downloadable catalog for people to browse faster on their local PC.  At this moment, our catalog is +200MB of HTML pages with 40MB of pictures.

            Now all of this can be achieved with php scripting language and MySQL database, however my skills are not up in that platform either.

            When I finalise my scripts, I will post them here for others to use or to learn from.

            And maybe UE can learn something from this exercise to incorporate into the future revisions of UE as I can see many people having shopping cart issues like this in the future.

            Thanks and Regards
            Ing. Stephen Hatton
            :idea:

            6

              Jun 21, 2005#6

              I used Paolo's idea (^c) combined with an idea I had which is to use "ReplInFiles" instead of the regular search and replace to create the following macro which works but unfortunately is still too slow for large numbers of files:

              Code: Select all

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOn
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "/classware/ala.do\?isbn=\d\d\d\d\d\d\d\d\d\d&alaid=ala_"
              "/classware/ala.do\?alaid=ala_"
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "/classware/ala.do?isbn=\d\d\d\d\d\d\d\d\d\d&alaid=structure_"
              "/classware/ala.do?isbn=GetString&alaid=structure_"
              FindInFiles MatchCase Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "http://novella.mhhe.com"
              Loop 
              Find RegExp "structure_\d*"
              IfFound
              StartSelect
              Copy 
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "^c"
              "XX"
              Find RegExp "structure_\d*"
              IfFound
              StartSelect
              Copy 
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "XX"
              "^c"
              EndLoop
              IfNotFound
              EndIf
              ExitLoop
              Loop 
              Find RegExp "\d*"
              IfFound
              StartSelect
              Copy 
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "^c"
              "XX"
              Find RegExp "\d*"
              IfFound
              StartSelect
              Copy 
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" "XX"
              "^c"
              EndLoop
              IfNotFound
              EndIf
              ExitLoop
              ReplInFiles MatchCase RegExp Recursive OutputWin Log "I:\TMP\working_directory\" "*.*" ","ala.do?alaid=ala_"
              ","/classware/ala.do?alaid=ala_"
              
              This macro works off of a long file which I'll abbreviate here:

              Code: Select all

              #In this site: 	In this site:
              
              90473	206185
              #Learning	Learning
              
              88819	205403
              88818	205402
              88817	205401
              88815	205399
              88816	205400
              88813	205397
              88814	205398
              #Writing	Writing
              
              135988	205419
              95507	205411
              95509	205413
              95505	205409
              88821	205417
              95508	205412
              95503	205407
              88822	205408
              135986	205415
              88823	205416
              88833	205420
              95451	205405
              94327	205421
              95504	205406
              95506	205410
              90368	205414
              88825	205404
              88834	205418
              #Research	Research
              ↓
              ↓
              structure_89242	structure_206199
              structure_89243	structure_206200
              structure_89244	structure_206201
              structure_89245	structure_206202
              structure_89246	structure_206203
              structure_89247	structure_206204
              structure_89248	structure_206205
              structure_89249	structure_206206
              structure_89250	structure_206207
              structure_89251	structure_206208
              structure_89252	structure_206209
              structure_89253	structure_206210
              structure_89254	structure_206211
              structure_89255	structure_206212
              structure_89256	structure_206213
              structure_89257	structure_206214