Run Macro on all files within folder

Run Macro on all files within folder

9
NewbieNewbie
9

    Aug 23, 2007#1

    I have a macro that I use to clean database files prior to import into SQL. Until now I have just loaded the files and run the macro on "ALL FILES". However, we are now dealing with a few hundred files at a time.

    Is there a way to make UE run our macro on all files within a folder?

    I would prefer not having to create a UE project to do this. I was thinking a command line batch file may work.
    • Open UltraEdit
    • Run XXX.mac on files *.*
    But I have read that this is not possible due to auto macro and wildcards?

    I am running UE 11.10a+1.

    Thanks in advance for any help

    PS: If I do have all the files listed in a project, how do I get a macro to run on entire project? I think, I sorted this part  :D
    Replace in Files and select Project Files for Select Where option.

      Aug 23, 2007#2

      Okay, after a bit more reading I have found the Replace in Files function quite useful.

      I have used it to create a basic macro that I will now edit with our needed functions, I just needed the initial command to enable the all files and directory name.

      How many files can be edited this way in one go?

      If I specify to edit *.txt in a certain directory, will it have issues if there are 300 .txt files in there?

      Is file size a concern? I have edited single large files before but never in batches like this.

      UPDATE:

      I have managed to get this working for plain text or regular expression.

      Can I do this for HEX?

      I have tried using the same macro but using HexOn instead of HexOff, but the expression is not found. If I actually load one of the files and run the replace function manually, it works well with hex mode.

      Can hex be used with Replace in Files?

      6,677585
      Grand MasterGrand Master
      6,677585

        Aug 23, 2007#3

        Replace in Files has no limits. You can run it on thousands of files each with several MBs or GBs.

        Replace in Files does not support hexadecimal replaces except you use the Perl regular expression engine and appropriate search and replace string in hexadecimal notation. However, you can replace all bytes except a NULL byte (00) with Replace in Files too. You just have to use the ASCII/ANSI characters for the bytes you want to replace.

        Here is a small macro which is able to run a macro on all files specified by you on startup.

        It works only for non FTP files. (To be more precise, I have not tested it with FTP files.)

        It works only for UltraEdit v10.20 or later and all versions of UEStudio. Without the first line it should work also for UltraEdit v9.10 and later, but then you have to run the Find in Files with an empty search string and with option Results to Edit Window checked manually.

        If you use an UltraEdit version prior v12.00 or an UEStudio version prior v5.50 delete the command UnicodeToASCII from the macro code because those versions already create the Find in Files results window as ASCII file and not as Unicode file.

        The conversion from Unicode to ASCII means also that the full file names of the the files found in the specified directory matching the file type pattern cannot contain Unicode characters. I have not tested if this macro would also work without conversion from Unicode to ASCII.

        Further the macro identifies the results window by the string "Search complete, found". This string exists at bottom of the results window only for English version of UltraEdit and only when Find Summary is still enabled and contains the default string at Advanced - Configuration - Search - Set Find Output Format depending on version of UltraEdit. If a localized version of UltraEdit is used, this search string must be modified to the localized string. With an unchecked Find Summary configuration setting the macro will not work because it is not able to identify the document window with the results (=file names).

        The name of the called macro as written in the string "Name of your macro" is case-sensitive. So, make sure that you enter the macro name correct. Also this macro and your called macro must exist in the same macro file. It is not possible to call a macro stored in another macro file. It is further important to first create the called macro and then create this macro or UltraEdit would remove the line with the PlayMacro on close (compilation) because of the missing macro.

        The macro property Continue if a Find with Replace not found or Continue if search string not found must be checked for this macro.

        FindInFiles "directory" "file type" ""
        Loop
        Find MatchCase Up "Search complete, found"
        IfFound
        ExitLoop
        Else
        NextWindow
        EndIf
        EndLoop
        DeleteLine
        SelectToBottom
        IfSel
        Delete
        EndIf
        Top
        UnicodeToASCII
        Loop
        IfEof
        ExitLoop
        EndIf
        StartSelect
        Key END
        IfSel
        Open "^s"
        PlayMacro 1 "Name of your macro"
        CloseFile Save
        EndIf
        EndSelect
        DeleteLine
        EndLoop
        CloseFile NoSave

        9
        NewbieNewbie
        9

          Aug 27, 2007#4

          Thanks Mofi!

          That's great :D


          If I remove the "file type" prompt at the beginning of the macro, will it still work on all files? Most of the files we need this for don't actually have extensions.

          So far I can get it to run and display "Search complete, found '' 0 time(s). (0 files.)" on the screen. So, I think it is having trouble finding the macro to run or trouble finding the files to search.

          I made very simple test macro which is just the command Find "PRD". I also have a few test files with both *.txt extension and no extension. All files contain PRD in them.

          Thanks again for the help, once I get it going, you'll have saved me hours and hours of tedium with this.

          6,677585
          Grand MasterGrand Master
          6,677585

            Aug 27, 2007#5

            The file type specification is nothing special. If you want all files in the directory found, specify "*".

            Note: The directory specification must end with a backslash.

            Do not specify any search string for the FindInFiles command. It is required for this macro that the search string exists, but is empty. Only with this empty search string UltraEdit returns just a list of found files with full file name (path and file name). The result is than a simple file list like using Windows command dir /A-D /B.
            Best regards from an UC/UE/UES for Windows user from Austria

            9
            NewbieNewbie
            9

              Aug 28, 2007#6

              OK, thanks for that Mofi.

              Have done some testing,

              I am now getting the file list showing up and all files are displayed and listed. I've got a collection of *.txt files with missing the extension.

              What happens next is that the macro running/cancel box is displayed and the cursor blinks rapidly. The called macro doesn't appear to run as no changes are made to the files

              Obviously I have an error somewhere in the second half of the macro :?

              Both macros are saved in the same macro file.

              Called macro with name step2:

              Code: Select all

              InsertMode
              ColumnModeOff
              HexOff
              UnixReOff
              Find RegExp "PRD"
              Replace All "XXX"
              SaveAll
              Main macro with name step1:

              Code: Select all

              FindInFiles "D:\Users\macro test\" "*" ""
              Loop 
              Find MatchCase Up "Search complete, found"
              IfFound
              ExitLoop
              Else
              NextWindow
              EndIf
              EndLoop
              DeleteLine
              Top
              Loop 
              IfEof
              ExitLoop
              EndIf
              StartSelect
              Key END
              IfSel
              Open "^s"
              PlayMacro 1 "step2"
              CloseFile Save
              EndIf
              EndSelect
              DeleteLine
              EndLoop
              CloseFile NoSave

              6,677585
              Grand MasterGrand Master
              6,677585

                Aug 28, 2007#7

                Showing the Cancel dialog is controlled by the macro property Show Cancel dialog for this macro of the main macro. Uncheck this option at Macro - Delete Macro/Modify Properties for the main macro.

                Do not use the SaveAll command. It saves all files. The temporary file with the list of files was not saved before and so has no file name. It should not be saved because it is not needed anymore after macro execution and will be empty after macro execution. The main macro contains already CloseFile Save which saves the current file and closes it. So your submacro has nothing to do except the changes you want to do, but no file save and no file close.

                Your search and replace is a simple one. There is no need to use the regular expression option.

                I hope, your real submacro does more than just replace all "PRD" by "XXX" in the current file. This could be also done without any macro by using the command Search - Replace In Files.
                Best regards from an UC/UE/UES for Windows user from Austria

                1
                NewbieNewbie
                1

                  Jan 23, 2008#8

                  I want to replace the hard coded directory in the FindInFiles command with a dynamic value.

                  FindInFiles "D:\Users\macro test\" "*" ""

                  So I tried using the following but neither worked.
                  1. FindInFiles "^c" "*" ""
                    In this case the directory path was copied to the clipboard before executing the macro.
                  2. FindInFiles "^s" "*" ""
                    In this case the directory path was selected text in the current file.
                  Can anyone help?

                  Thanks, Krthk

                  6,677585
                  Grand MasterGrand Master
                  6,677585

                    Jan 24, 2008#9

                    ^c and ^s can be used only for the search string of the FindInFiles macro command. The initial directory and the file type parameters cannot be made variable. The solution is using a script where variables are in general possible - see for example Function to get list of files into an edit window. But that requires UltraEdit v13.20 or later.
                    Best regards from an UC/UE/UES for Windows user from Austria

                    4
                    NewbieNewbie
                    4

                      Jul 09, 2008#10

                      Hi all,

                      I'm using currently UltraEdit 12.20b+1. How do I get this to work on the whole folder?

                      I am trying to run a macro on around 100 files to tidy them up with the following macro:

                      Code: Select all

                      InsertMode
                      ColumnModeOff
                      HexOff
                      UnixReOff
                      Find "<!-- Begin Document Specific Declarations -->"
                      Replace ""
                      Find "<!-- End Document Specific Declarations -->"
                      Replace ""
                      Find RegExp "Multiple
                      procedures"
                      Replace All "Multiple procedures"
                      Find RegExp "single
                      procedure"
                      Replace All "single procedure"
                      Find RegExp "<?MEKON*>"
                      Replace All ""
                      Find "><"
                      Replace All ">^p<"
                      Loop 10
                      Find "^p^p"
                      Replace All "^p"
                      EndLoop
                      Loop 10
                      Find "  "
                      Replace All " "
                      EndLoop
                      Find "^p</XREF>"
                      Replace All "</XREF>"
                      Find "^p</ENTRY>"
                      Replace All "</ENTRY>"
                      
                      Do I add the command to the macro and do I have to run it on command line and if so how?

                      Code: Select all

                      FindInFiles "D:\Users\macro test\" "*" ""
                      or

                      Code: Select all

                      RepInFiles "D:\Users\macro test\" "*" ""
                      Many thanks for your time.

                      Regards, Neil

                      6,677585
                      Grand MasterGrand Master
                      6,677585

                        Jul 10, 2008#11

                        First, I do not understand why you not run these 10 replaces manually with Replace in Files. Do you need it frequently?

                        Second, it would be much faster to use the command ReplInFiles in your macro for that job instead of opening one file after the other and run your macro on every file. Here is the modified version of your macro.

                        InsertMode
                        ColumnModeOff
                        HexOff
                        UnixReOff
                        ReplInFiles "directory" "file type" "<!-- Begin Document Specific Declarations -->"
                        ""
                        ReplInFiles "directory" "file type" "<!-- End Document Specific Declarations -->"
                        ""
                        ReplInFiles "directory" "file type" "Multiple^pprocedures"
                        "Multiple procedures"
                        ReplInFiles "directory" "file type" "single^pprocedure"
                        "single procedure"
                        ReplInFiles RegExp "directory" "file type" "<?MEKON*>"
                        ""
                        ReplInFiles "directory" "file type" "><"
                        ">^p<"
                        ReplInFiles RegExp "directory" "file type" "^p[^r^n]+"
                        "^p"
                        ReplInFiles RegExp "directory" "file type" "  +"
                        " "
                        ReplInFiles "directory" "file type" "^p</XREF>"
                        "</XREF>"
                        ReplInFiles "directory" "file type" "^p</ENTRY>"
                        "</ENTRY>"

                        directory is for example C:\Temp\ and file type is for example *.xml

                        Last, what is your problem with the main macro I have posted? If you have loaded the macro file with your macro, just create a new empty macro, paste my macro code into this new macro (replacing the four standard lines), edit the three red marked strings to your requirements, save the macro and run it once. A macro file can contain more than one macro!
                        Best regards from an UC/UE/UES for Windows user from Austria

                        4
                        NewbieNewbie
                        4

                          Jul 10, 2008#12

                          Hi Mofi,

                          Thanks for your response, I use this macro to tidy up some sgml data on a regular basis.

                          Nothing wrong with your macro, I just don't understand the macro language.

                          Two hours later: Just used your macro. Many thanks, saved me lots of time.

                          Kind regards, Neil

                          12
                          Basic UserBasic User
                          12

                            Apr 30, 2010#13

                            This thread has become a bit old. I read through the posts here and applied the same but it just didn't work for me. I have multiple folders inside a folder. How can I run my macro on all files in those folders since there are a few thousand of them. I have UltraEdit version 16.

                            6,677585
                            Grand MasterGrand Master
                            6,677585

                              Apr 30, 2010#14

                              The macro template in my (Mofi) first post should also work with UE v16.00.0.1040. You just have to use in the first line

                              FindInFiles Recursive "directory" "file type" ""
                              Best regards from an UC/UE/UES for Windows user from Austria

                              12
                              Basic UserBasic User
                              12

                                Apr 30, 2010#15

                                Thanks Mofi,  it worked!