Run macro on multiples files

Run macro on multiples files

982
Advanced UserAdvanced User
982

    9:21 - 17 days ago#1

    I'm trying to run a macro on multiple files.
    Just that, to run several editing steps on files.
    Searching, I found Run Macro on all files within folder

    And I thought something like this:

    Code: Select all

    InsertMode
    ColumnModeOff
    HexOff
    FindInFiles "F:\tests\" "*.srt" "1^p"
    IfFound
    PlayMacro 1 "Fix subtitle"
    EndIf

    All ".srt" files follow this pattern:

    Code: Select all

    1
    00:00:30,600 --> 00:00:42,680
    Free will is your ability to decide by yourself
    what you do. It means that the future is an open
    
    2
    00:00:36,880 --> 00:00:48,440
    arena that you can shape with your actions. It’s
    at the core of human relationships - it means
    
    3
    00:00:42,680 --> 00:00:53,240
    you are responsible for your actions, which
    is the basis of our moral and legal systems.

    Macro "Fix subtitle" goes through all file using a loop and changes timecode positions, until reaches eof.

    Running the main macro, it only open a new window "** Find Results **" with "Search complete. Found", but did not run "Fix subtitle" macro.
    Both macros are on the same ".mac" file.

    Whats is wrong?

    6,651559
    Grand MasterGrand Master
    6,651559

      17:16 - 16 days ago#2

      The macro command IfFound can be used in a macro only after the command Find or the command Replace which must be always below the command Find. The commands FindInFiles and ReplInFiles just run the find/replace in all files according to the used parameters. The macro in the referenced topic runs a FindInFiles with an empty search string to get a list of files in the specified folder matching the specified pattern written into a new file which is nowadays a UTF-16 encoded file. The macro switches next the document windows and searches for the final summary line to find out which of the opened document windows contains the list of file names. After the results file with the file names list is found, the UTF-16 encoded file is converted to ASCII/ANSI. Then a loop is executed in which each file name is selected, the file with selected file name is opened, the macro is run on the opened file, and the just opened and modified file is closed with saving the modifications.

      There is not posted the code of the macro Fix subtitle and how each *.srt file should look like after running this macro. I suppose that a macro is not really needed, just a manually executed Replace in Files (perhaps recorded into a macro) with either a tagged UltraEdit regular expression or with a Perl regular expression using backreferences for reformatting the data in all *.srt files of a folder or even a folder tree. Opening each file in UltraEdit for running a macro which makes the changes on the opened file one after the other is most likely not necessary at all.
      Best regards from an UC/UE/UES for Windows user from Austria

      982
      Advanced UserAdvanced User
      982

        18:13 - 16 days ago#3

        Thank you for clarifying, Mofi.
        But I'm afraid that ReplaceInFiles won't be possible.

        Macro "Fix subtitle" delays the timecode of each subtitle because they were downloaded with error.

        I will try to explain below.

        The subtitle pattern looks like this:
        (The example below was downloaded wrong and must be fixed)

        Code: Select all

        1
        00:00:30,600 --> 00:00:42,680
        Free will is your ability to decide by yourself
        what you do. It means that the future is an open
        
        2
        00:00:36,880 --> 00:00:48,440
        arena that you can shape with your actions. It’s
        at the core of human relationships - it means
        
        3
        00:00:42,680 --> 00:00:53,240
        you are responsible for your actions, which
        is the basis of our moral and legal systems.
        
        The task is to move the 2nd timecode of subtitle 1 (00:00:42,680) to replace the 2nd timecode of subtitle 2 (00:00:48,440).
        The same must be apply to the 2nd and 3rd subtitles.
        And so on, it must do the same to all timecodes, until the end of the file.

        Before the main loop (1000 times), the macro gets the 1st timecode of the 2nd subtitle to fill the blank space left.

        Here is a snapshot comparing the wrong subtitle (left panel) and the fixed subtitle (right panel):
        JDownload - Youtube subtitle error 2.png (88.41KiB)
        I had to copy and paste Clipboard 1 and 2 because I didn't know how to pass the content from one to the other.
        Additionally, I put "loop 1000" because I don't know how to make a generic loop to cover the entire number of lines.

        Below is "Fix subtitle" code:

        Code: Select all

        InsertMode
        ColumnModeOff
        HexOff
        Clipboard 1
        ClearClipboard
        Clipboard 2
        ClearClipboard
        Loop 5
        Key DOWN ARROW
        EndLoop
        StartSelect
        Loop 6
        Key Ctrl+RIGHT ARROW
        EndLoop
        Loop 3
        Key RIGHT ARROW
        EndLoop
        EndSelect
        Clipboard 1
        Copy
        Key HOME
        Loop 4
        Key UP ARROW
        EndLoop
        Loop 8
        Key Ctrl+RIGHT ARROW
        EndLoop
        StartSelect
        Key END
        EndSelect
        Clipboard 2
        Copy
        Delete
        Clipboard 1
        Paste
        ClearClipboard
        " "
        Clipboard 2
        Paste
        StartSelect
        Loop 7
        Key Ctrl+LEFT ARROW
        EndLoop
        EndSelect
        Clipboard 1
        Copy
        Clipboard 2
        ClearClipboard
        Delete
        Key BACKSPACE
        Loop 1000
        Key HOME
        Key DOWN ARROW
        Key DOWN ARROW
        Key DOWN ARROW
        Key DOWN ARROW
        IfEof
        ExitLoop
        EndIf
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        Key Ctrl+RIGHT ARROW
        StartSelect
        Key END
        EndSelect
        Clipboard 2
        Copy
        Delete
        Clipboard 1
        Paste
        ClearClipboard
        " "
        Clipboard 2
        Paste
        StartSelect
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        Key Ctrl+LEFT ARROW
        EndSelect
        Clipboard 1
        Copy
        Clipboard 2
        ClearClipboard
        Delete
        Key BACKSPACE
        EndLoop
        Clipboard 0
        After write the code, I noticed that my solution is very poor and won't solve all subtitles files that I have.
        There are subtitles that has 2 or even 3 lines.
        My solution fails on such cases.
        The first example that I gave above uses 2 lines for each subtitle. The macro fails.
        The snapshot uses only one line for each subtitle, and the macro works well.

        I appreciate if you, please, could give some ideas how to better deal with this.
        The solution would handle 1, 2 or 3 lines for each subtitle.

        6,651559
        Grand MasterGrand Master
        6,651559

          19:15 - 15 days ago#4

          I suggest the following macro for this task tested with UltraEdit for Windows v2024.0.0.35.

          Code: Select all

          PerlReOn
          ReplInFiles MatchCase RegExp Log "F:\tests\\" "*.srt" "(?s)(0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]$)(.*? --> )" "\2\1"
          ReplInFiles MatchCase RegExp Log "F:\tests\\" "*.srt" "(?s)^(1[\t ]*\r?\n0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9] --> )(\r?\n.*?2[\t ]*\r?\n)(0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9])" "\1\3\2\3"
          ReplInFiles MatchCase RegExp Log "F:\tests\\" "*.srt" "0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]\K0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]" ""
          ReplInFiles MatchCase RegExp Log "F:\tests\\" "*.srt" "(?s)(0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]$)(.*?\r?\n)(0[0-9]:[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]) -->" "\1\2\1 -->"
          I tested the macro with the file F:\tests\test_input.srt with initially following lines:

          Code: Select all

          1
          00:00:30,600 --> 00:00:42,680
          Free will is your ability to decide by yourself
          what you do. It means that the future is an open
          
          2
          00:00:36,880 --> 00:00:48,440
          at the core of human relationships - it means
          
          3
          00:00:42,679 --> 00:00:53,240
          first line
          you are responsible for your actions, which
          is the basis of our moral and legal systems.
          The first Perl regular expression replace moves the second time code of each subtitle except the last subtitle for becoming the second time code of next subtitle. The result is:

          Code: Select all

          1
          00:00:30,600 --> 
          Free will is your ability to decide by yourself
          what you do. It means that the future is an open
          
          2
          00:00:36,880 --> 00:00:42,680
          at the core of human relationships - it means
          
          3
          00:00:42,679 --> 00:00:48,44000:00:53,240
          first line
          you are responsible for your actions, which
          is the basis of our moral and legal systems.
          The first subtitle has no second time code anymore.

          This is corrected with the second Perl regular expression replace which copies the first time code of second subtitle as the second time code of first subtitle and producing for the example:

          Code: Select all

          1
          00:00:30,600 --> 00:00:36,880
          Free will is your ability to decide by yourself
          what you do. It means that the future is an open
          
          2
          00:00:36,880 --> 00:00:42,680
          at the core of human relationships - it means
          
          3
          00:00:42,679 --> 00:00:48,44000:00:53,240
          first line
          you are responsible for your actions, which
          is the basis of our moral and legal systems.
          The last subtitle has two second time codes.

          This is corrected with the third Perl regular expression replace removing the last time code on last subtitle producing:

          Code: Select all

          1
          00:00:30,600 --> 00:00:36,880
          Free will is your ability to decide by yourself
          what you do. It means that the future is an open
          
          2
          00:00:36,880 --> 00:00:42,680
          at the core of human relationships - it means
          
          3
          00:00:42,679 --> 00:00:48,440
          first line
          you are responsible for your actions, which
          is the basis of our moral and legal systems.
          There can be seen on the image on right pane on the subtitles 7 and 8 that the first time code of a subtitle can be wrong by a millisecond in comparison to the second time code of the previous subtitle. This is also the case on third subtitle in the example file.

          Such a time accuracy mistake is corrected with the fourth Perl regular expression replace which copies the now hopefully always correct second time code of a subtitle to the next subtitle as first time code with deletion of the first time code of next subtitle. The final result is:

          Code: Select all

          1
          00:00:30,600 --> 00:00:36,880
          Free will is your ability to decide by yourself
          what you do. It means that the future is an open
          
          2
          00:00:36,880 --> 00:00:42,680
          at the core of human relationships - it means
          
          3
          00:00:42,680 --> 00:00:48,440
          first line
          you are responsible for your actions, which
          is the basis of our moral and legal systems.
          Best regards from an UC/UE/UES for Windows user from Austria

          982
          Advanced UserAdvanced User
          982

            9:34 - 15 days ago#5

            Just wonderful!
            Your solution is perfect!

            I think almost all macros can be replaced with some kind of ReplaceInFiles.
            Even when I thought it wouldn't be possible, it was, like in this case.
            And this is the solution when the task involves applying to multiple files.

            But the original question remains: is it possible to apply a macro to many files?
            And if the answer was “yes”, how could this be done?

            6,651559
            Grand MasterGrand Master
            6,651559

              19:20 - 14 days ago#6

              The solution running a macro like your Fix subtitle macro on all *.srt files in the directory F:\tests\ is the usage of the macro posted by me at Run macro on all files within folder.

              The main macro code:

              FindInFiles "F:\tests\\" "*.srt" ""
              Loop 0
              Find MatchCase Up "Search complete, found"
              IfFound
              ExitLoop
              Else
              NextWindow
              EndIf
              EndLoop
              DeleteLine
              SelectToBottom
              IfSel
              Delete
              EndIf
              Top
              UnicodeToASCII
              Loop 0
              IfEof
              ExitLoop
              EndIf
              StartSelect
              Key END
              IfSel
              Open "^s"
              PlayMacro 1 "Fix subtitle"
              CloseFile Save
              EndIf
              EndSelect
              DeleteLine
              EndLoop
              CloseFile NoSave

              Find summary must be configured for Search complete, found as shown in the image below.
              find_summary_configuration.png (6.14KiB)
              Find summary configuration as defined by default in English UltraEdit
              Well, your Fix subtitle macro does not work for your example with two lines of text per subtitle. It is written for just one line of text per subtitle. But with Fix subtitle and macro main currently loaded in UltraEdit because of being stored both in the same macro file or there is first loaded the macro file containing just Fix subtitle and next is created the macro main with the code above and then the macro main is played once, each *.srt file in the directory F:\tests\ is first opened by the macro main. Next the macro Fix subtitle is run once on the opened *.srt file. Then the macro main saves and closes the modified *.srt file before deleting the file name from the **Find Results ** document window with the list of file names. The macro main processes one file name after the other until having opened, played the macro, saved and closed each file in the list which becomes finally empty. The never saved **Find Results ** file being finally empty is closed without saving it as last action by the macro main.

              The macro main contains Loop 0. The value 0 instructs UltraEdit to run this loop more or less endless until the loop is exited either with the command ExitLoop or the entire macro execution is exited with the command ExitMacro.

              IfEof with ExitLoop as next command must be used with care on using Loop 0 in a macro because of the condition IfEof is true only if the text cursor (caret) has reached the end of the file. A Find not finding anymore the searched string up to end of the file does not move the caret to the end of the file. The caret remains at the current position in the file. The command Key DOWN ARROW could result in caret being kept somewhere left to the end of the last line and therefore before end of the file if the last line of a text file does not have a line termination at all. In both cases IfEof becomes never true and the loop runs really endless resulting in an endless running macro.
              Best regards from an UC/UE/UES for Windows user from Austria

              982
              Advanced UserAdvanced User
              982

                22:59 - 14 days ago#7

                Fully explained and solved.

                Many thanks.
                👍