How to convert a directory listing to a batch file for renaming files using regular expressions?

How to convert a directory listing to a batch file for renaming files using regular expressions?

71
NewbieNewbie
71

    Dec 13, 2015#1

    I need to rename a few hundred files like:

    Code: Select all

    3M_By_element14_Batch_1.lbr                                                     3M.lbr
    The file list was made with dir >list.txt and that is what I'm editing.

    With a macro I would start at a newline, insert 'rename ', find lbr move one space past the end of lbr, do select and find any ASCII char (the 3 in 3M.lbr), back arrow one char, delete all between the rename xxx and 3M.lbr, move 3M.lbr to one space from the old file name THEN find ^n and end.

    The problem I'm having is how do you enter regular expression in the find window?

    HELP is no help. I use [0-9][a-z][A-Z] and it finds 5XX 86 lines down from the start.
    I have Regular Expressions checked and Unix selected. (NOTE: In Configuration there is more to set, but I haven't modified anything. They wouldn't have me roaming around to multiple location to enable regular expressions?)

    I've tried:

    Code: Select all

    ^[0-9][a-z][A-Z]
    "[0-9][a-z][A-Z]"
    ([0-9][a-z][A-Z])
    Then I discovered \w works, but where is the Close after find check box in the Find window?

    As it is after a Find the focus is set to the Find window and if I do anything I lose the selected text.
    In the old day I would get throw back into the edit window, position the cursor then delete the selected text, now I can't get to the edit window without losing the selected text.

    You don't use it, you loss it. :(

    6,676579
    Grand MasterGrand Master
    6,676579

      Dec 13, 2015#2

      You have not written which version of UltraEdit you use which would have been helpful to better help you on your task.

      The topic Quick Find versus Find and how to customize the usage of both (Ctrl+F behavior) contains a collage of all the find windows available in UltraEdit v21.00 and later where the various find options can be seen which are available after clicking on gearwheel button. In UE for Windows prior v21.10 the button to show/hide the (advanced) options was a labeled button with label Advanced in English UE. In very old versions of UltraEdit the regular expression engine could be selected only in configuration, see announcement Readme for the Find/Replace/Regular Expressions forum for details.

      You need to open the Replace window for example by pressing Ctrl+R or clicking on menu item Replace in menu Search.

      To change in list file

      Code: Select all

      3M_By_element14_Batch_1.lbr
      to

      Code: Select all

      rename "3M_By_element14_Batch_1.lbr" "3M.lbr"
      the Unix or Perl regular expression search string

      ^([0-9A-Za-z]*)(_.+)(\..+)$

      and as replace string

      rename "\1\2\3" "\1\3"

      could be used on running a Replace All from top of file.

      The option Match Case does not matter with this search string because of 0-9A-Za-z in square brackets.

      This is a Unix/Perl regular expression using backreferences, a feature which is supported also by UltraEdit regular expression engine using a tagged regular expression.

      There is the button with .* (in older versions a magnifying glass or a triangle arrow) which opens the list of regular expressions suitable for search or replace string. For UltraEdit and Unix regex engines the lists are complete, for Perl regex engine the list is just a short summary of most often needed special characters and expressions as Perl regular expression engine is so powerful that it fills entire books and complete websites.

      What does the Unix/Perl search string mean?

      ^ ... start each search at beginning of a line.

      (...) ... first capturing group. The string found be the expression inside the group can be back-referenced with \1 in replace string (and with Perl regex engine also in search string).

      [0-9A-Za-z]* ... a character set definition which finds a digit or an ASCII letter 0 or more times. This expression matches the string left to first underscore.

      (...) ... second capturing group. The string found by the expression inside this group is referenced with \2 in replace string.

      _.+ ... underscore and any character 1 or more times greedy except newline characters. Greedy means for this search string not stopping matching any character on next period found, but on last period found in line resulting in a positive match for the entire search string. This expression matches everything from first underscore up to character before last period.

      (...) ... third capturing group. The string found by the expression inside this group is referenced with \3 in replace string.

      \..+ ... find a period which must be escaped with a backslash to be interpreted as literal character and any character except newline characters 1 or more times. This expression matches the period before file extension and the file extension.

      $ ... end of line without selecting the line termination. With Unix regex engine the last line of the file must have also a line termination. With Perl regex engine the last line in file could be also just a string at end of file, i.e. there is no line termination at end of file.

      A batch file solution for this renaming task:

      Code: Select all

      @echo off
      setlocal EnableDelayedExpansion
      for /F "delims=" %%F in ('dir /A-D /B /ON *.lbr 2^>nul') do (
          for /F "tokens=1 delims=_" %%I in ("%%~nF") do set "NewFileName=%%I"
          rename "%%F" "!NewFileName!%%~xF"
      )
      endlocal
      pause
      
      How often do I rename files or folders using a batch file or creating a list file and use a regular expression in UltraEdit to convert the list into a batch file for renaming all the files/folders?

      Never, because I'm using Total Commander as file manager which has a Multi-Rename-Tool built-in making it possible to do any file/folder renames on any file/folder list with easy to define patterns including Perl regular expressions if really needed and other options like a counter with getting displayed the new file names according to pattern and options currently set before even running the file rename operation. And if I detect having made a mistake after running the rename, I can undo the rename, even after having multi-rename-tool dialog already closed.

      Tip: Use an advanced text editor like UltraEdit for advanced text editing tasks, but use an advanced file manager like Total Commander for advanced file operation tasks like renaming lots of files, and it saves you a lot of time which means often a lot of money even if the applications are not free.
      Best regards from an UC/UE/UES for Windows user from Austria