How create a bak file only if search/replace has made change?

How create a bak file only if search/replace has made change?

emmchild

    May 27, 2005#1

    The macro below performs a search/replace command on all open files, saving and closing each file as it goes.

    I have two questions concerning the "bak" files that are created automatically for each file the macro processes.

    1. Is there a macro command to prevent the bak files from being created entirely?

    2. Is there a way to have a bak file created ONLY in the event that changes are actually made to the file? That is, if after the search/replace, the file has not been changed, don't save it, or save it but don't create a bak file.

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    UnixReOn
    
    Loop 
    
    IfNameIs ""
    ExitLoop
    EndIf
    
    Find RegExp "([^,\n])"([^,\n])"
    Replace All "\1(qt)\2"
    CloseFile Save
    
    EndLoop
    

    206
    MasterMaster
    206

      May 27, 2005#2

      This is untested. You can turn off .bak creation entirely in the UE Configuration.

      InsertMode
      ColumnModeOff
      HexOff
      UnixReOn

      Loop

      IfNameIs ""
      ExitLoop
      EndIf

      Find RegExp "([^,\n])"([^,\n])"
      IfFound
      Replace All "\1(qt)\2"
      CloseFile Save
      Else
      CloseFile NoSave
      EndIf
      EndLoop
      Software For Metalworking
      http://closetolerancesoftware.com

      emmchild
      emmchild

        May 28, 2005#3

        That'll do it. Thanks!

        emmchild
        emmchild

          May 28, 2005#4

          Actually, I have several find/replace expressions. I'd like to save the file if one or more of the finds have succeeded.

          Can't see how to do it with IsFound (and without the ability to create and test variables in a macro). Any further suggestions?

          Code: Select all

          Find RegExp "([^,\n])"([^,\n])"
          Replace All "\1(qt)\2"
          Find RegExp "([^,\n])"([^,\n])"
          Replace All "\1(qt)\2"
          Find RegExp "([\s][,])"|([,][\s])""
          Replace All "\1(qt)"
          

          206
          MasterMaster
          206

            May 28, 2005#5

            I haven't tried this before.

            A macro to do this:

            Create a blank line at the top of your file

            Do the first search/replace

            If the search string is found, replace  then go to the top of the file and put a @ (or some other character) in the blank line - this becomes your flag

            If not found, do the next search/replace just like the first, add a @ if found

            Do as many search/replace sequences as you need, all with a @ added to the top line if the search string is found

            After they're all done, go to the top and see if the first character is @ - which it will be if even one search string was found

            If yes, delete the line, close with save

            If no, close with no save.

            I may be overlooking something, let me know how it works out.
            Software For Metalworking
            http://closetolerancesoftware.com

            emmchild
            emmchild

              Jun 02, 2005#6

              mrainey56 wrote:If the search string is found, replace  then go to the top of the file and put a @ (or some other character) in the blank line - this becomes your flag.
              Great idea! I set out to implement it and ran into this strange error, which I can't figure out.  

              This segment of macro code causes a "Incorrect macro command in macro line" error on attempt to close the macro editor. Paste it into your editor and you'll see.  

              Code: Select all

              Find RegExp "([^,\n])"([^,\n])"
              IfFound 
              Replace All "\1(qt)\2"
              EndIf

              6,687587
              Grand MasterGrand Master
              6,687587

                Jun 02, 2005#7

                Best regards from an UC/UE/UES for Windows user from Austria

                emmchild
                emmchild

                  Jun 02, 2005#8

                  Thanks Mofi and thanks mrainey56. Your idea worked fine.

                  Code: Select all

                  InsertMode
                  ColumnModeOff
                  HexOff
                  UnixReOn
                  
                  Loop 
                  IfNameIs ""
                  ExitLoop
                  EndIf
                  
                  Top
                  "
                  "
                  
                  Find RegExp "([^,\n])"([^,\n])"
                  IfFound
                  Find RegExp "([^,\n])"([^,\n])"
                  Replace All "\1(qt)\2"
                  Top
                  "@"
                  EndIf
                  
                  [...]
                  
                  Top
                  IfCharIs "@"
                  DeleteLine
                  CloseFile Save
                  Else
                  CloseFile NoSave
                  EndIf
                  EndLoop