FIND & REPLACE



  • Hello All

    I have 650 files to be renamed and they look like this:
    P_158_2016_ypsun_loren_P_TCE_RJ_2214773_2018
    PROC_065_2016_outro_nome_quanquer_PROC_TCE_RJ_8277204_2016

    I need to rename to:

    CPF_543210987654321_P_158_2016
    CPF_123456789012345_P_065_2016

    (123456789012345 = random numbers)
    Can anybody help me? Through \ … or regular expression
    Thanks a lot

    Cezar Moniz



  • @Cezar-Moniz ,

    Wow, that was hard to tell what was common between the input and output lines. When asking for help, try to be as clear as possible.

    First, Notepad++ is a text editor. If you mean that you have a list of filenames in an open textfile, and you want to change the filenames in that textfile, then it’s a reasonable task to assume that Notepad++ might be able to do. If you really want to do the renames from within Notepad++, that’s not something Notepad+ does natively in an automated fashion. You would have to use one of the scripting plugins, which makes it a programming challenge, not a text-editor/regex challenge. There are also plenty of fancy-rename utilities out there (but that’s then completely off topic).

    For the “list of files in a file open in Notpead++”, where the contents of your file look something like

    P_158_2016_ypsun_loren_P_TCE_RJ_2214773_2018
    PROC_065_2016_outro_nome_quanquer_PROC_TCE_RJ_8277204_2016
    

    with one filename per line, and the interesting digits always after P_ or PROC_:

    Then part of what you want (moving the 158_2016 or 065_2016 from the “second term” to the “end”) wouldn’t be difficult in regex:

    • FIND = (?-s)^P(?:ROC)?_(\d{3}_\d{4}).*$
    • REPLACE = CPF_543210987654321_P_$1
    • MODE = regular expression

    But part (“random numbers”) is not a feature of Notepad++'s regex engine (or any others that I’ve heard of). You’ll notice my example used a constant value for that string of numbers.

    If you wanted to convert your textfile-based list of filenames into a windows .bat file that performs the rename, you could do it, as long as you don’t expect different values for the “random”

    • FIND = (?-s)^P(?:ROC)?_(\d{3}_\d{4}).*$
    • REPLACE = ren "$0" "CPF_543210987654321_P_$1"
    • MODE = regular expression

    You would then have to save it as a .bat and run it from windows.

    If you want random numbers there, but don’t care how many digits, you can use the %RANDOM% pseudo-variable from Windows cmd.exe, which gives a 1 to 5 digit number. Using 3 of them in a row, it would give a random length between 3 and 15 characters when the batch file was run, like:

    • REPLACE = ren "$0" "CPF_%random%%random%%random%_P_$1"

    Again, save the results as .bat and run the batch file.

    If you absolutely have to have exactly 15 random digits, and you are willing to install the PythonScript plugin, someone could probably hack up a script for you. But first, you need to clarify exactly what you want, and what your needs are, and why you think a text editor is the right place for renaming files.

    Good luck.



  • I was curious: there is a trick in windows batch files to create a random number of a specific number of digits between 1 and 4. For anything longer than that, you could concatenate.

    To get a three-digit number (from 100-999):

    set /A RAND=%RANDOM% %% 900+10
    

    To get a 15-digit number (never starting with 0), concatenate 5 of those together (so you could define R1 through R5, and then %R1%%R2%%R3%%R4%%R5%.

    Of course, in your original question, the search-and-replace would have to include the multiline replace to insert the 5 set /A commands before each ren command, but I’ll leave that as an exercise to the reader. I still maintain, however, that PythonScript (or an external python program – or other language) would be better than trying to hack it with search-and-replace.


Log in to reply