Author Topic: Bulk renaming files / Changing the format of the date  (Read 9695 times)

0 Members and 1 Guest are viewing this topic.

Offline HalcyonTopic starter

  • Global Moderator
  • *****
  • Posts: 5629
  • Country: au
Bulk renaming files / Changing the format of the date
« on: March 09, 2022, 01:36:55 am »
This is one for the Python/programming/Powershell nerds. I'm trying to work out a quick way to rename hundreds of files which follow the same format/pattern, for example:

[Text] [dd].[mm].[yyyy].txt
e.g.: Log Ending 18.01.2020.txt

I need to be able to rename all the files so that the dates are expressed in ISO 8601 format and without seperators:

[Text] [yyyymmdd].txt
e.g.: Log Ending 20200118.txt

Any ideas? I've tried a bunch of file renaming tools and none of them do what I need.
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: Bulk renaming files / Changing the format of the date
« Reply #1 on: March 09, 2022, 02:23:06 am »
On a unix/unixish box it's a fairly straightforward shell script - with choice of a few basic strategies on how to build a list of candidate files and do the necessary regular expression processing. On a Windows machine one ought to be able to come up with a similar Powershell script, but my few experiences with Powershell tell me that it's rarely as simple/straightforward as I thought it was going to be.

There's one thing I think you need to specify a little more clearly first, before anyone dives in, and that is "How are you selecting the files to rename?".
Is it just
  • Rename all the files in directory X that match the pattern "[Text, any text at all] [dd].[mm].[yyyy].txt",
or it is it
  • Rename all the files in directory X that match the pattern "[Text that I just specified for this particular renaming] [dd].[mm].[yyyy].txt"?

Is the space in the input filename optional or mandatory? Is directory X going to be specified, or is it just the current directory?

Also what do you want the behaviour to be for files in directory X that don't match your basic pattern: ignore them, ask you what to do, do something else?
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 

Offline HalcyonTopic starter

  • Global Moderator
  • *****
  • Posts: 5629
  • Country: au
Re: Bulk renaming files / Changing the format of the date
« Reply #2 on: March 09, 2022, 02:36:39 am »
    • Rename all the files in directory X that match the pattern "[Text that I just specified for this particular renaming] [dd].[mm].[yyyy].txt"?

    Is the space in the input filename optional or mandatory? Is directory X going to be specified, or is it just the current directory?

    Also what do you want the behaviour to be for files in directory X that don't match your basic pattern: ignore them, ask you what to do, do something else?

    It's the same string for this particular renaming task. It only needs to be run once.

    All files are in the same directory.

    There are no other files which won't match the pattern in said directory.
     

    Offline HalcyonTopic starter

    • Global Moderator
    • *****
    • Posts: 5629
    • Country: au
    Re: Bulk renaming files / Changing the format of the date
    « Reply #3 on: March 09, 2022, 03:04:25 am »
    I managed to butcher together something. It works. There is probably a much neater way to do this, but it was a single-use script.

    Code: [Select]
    from pathlib import Path

    p= Path('.')
    a = list(p.glob('**/*.txt'))

    for x in a:
        a = x
        x = str(x).split(' ')
        y = x[3].split('.')
        print(x, y[:-1])
        k = (x[0] + ' '+x[1] + ' '+x[2] + ' '+y[2]+y[1]+y[0] + '.txt')
        a.rename(k)
     

    Offline Cerebus

    • Super Contributor
    • ***
    • Posts: 10576
    • Country: gb
    Re: Bulk renaming files / Changing the format of the date
    « Reply #4 on: March 09, 2022, 03:05:58 am »
    Pretty easy to do in good old Bourne shell:

    Code: [Select]
    #!/bin/sh

    for old in "$@"
    do
            new=`echo "$old" | sed -e 's/\(.*\)\([0-3][0-9]\).\([0-1][0-9]\).\([0-9][0-9][0-9][0-9]\)\(.*\)/\1\4\3\2\5/'`
            mv "$old" "$new"
    done

    You haven't escaped the literal dots; they'll happily match any character including dots. This may have unintended consequences, always a risky thing. On this occasion it's probably harmless, but in combination with .* both at the start and end of the string you've got a floating match - best for these kind of matches to anchor the beginning and end as explicity as you can. e.g. ^FIXED TEXT your pattern here \.txt$

    Anybody got a syringe I can use to squeeze the magic smoke back into this?
     

    Offline Cerebus

    • Super Contributor
    • ***
    • Posts: 10576
    • Country: gb
    Re: Bulk renaming files / Changing the format of the date
    « Reply #5 on: March 09, 2022, 03:09:57 am »
    I managed to butcher together something. It works. There is probably a much neater way to do this, but it was a single-use script.

    Yes, there is.  :)

    But frankly, for a one off where you throw the code away the second you've finished with it, "Who cares?". I've done much worse in my time, usually when trying to clean up a butchered directory without having to rename every file by hand.
    Anybody got a syringe I can use to squeeze the magic smoke back into this?
     

    Online edpalmer42

    • Super Contributor
    • ***
    • Posts: 2260
    • Country: ca
    Re: Bulk renaming files / Changing the format of the date
    « Reply #6 on: March 09, 2022, 03:17:43 am »
    I've used the Bulk Rename Utility ( https://www.bulkrenameutility.co.uk/ ) under Windows for similar chores.  Was that one of the ones you looked at?

     

    Offline magic

    • Super Contributor
    • ***
    • Posts: 6732
    • Country: pl
    Re: Bulk renaming files / Changing the format of the date
    « Reply #7 on: March 09, 2022, 08:49:18 am »
    My way:
    Code: [Select]
    ls |sed/awk "transform old filename into 'mv "old" "new"'" >tmp
    less tmp # sanity check before it's too late ;)
    sh <tmp
     

    Offline PKTKS

    • Super Contributor
    • ***
    • Posts: 1766
    • Country: br
    Re: Bulk renaming files / Changing the format of the date
    « Reply #8 on: March 09, 2022, 11:02:56 am »
    This is one for the Python/programming/Powershell nerds. I'm trying to work out a quick way to rename hundreds of files which follow the same format/pattern, for example:

    [Text] [dd].[mm].[yyyy].txt
    e.g.: Log Ending 18.01.2020.txt

    I need to be able to rename all the files so that the dates are expressed in ISO 8601 format and without seperators:

    [Text] [yyyymmdd].txt
    e.g.: Log Ending 20200118.txt

    Any ideas? I've tried a bunch of file renaming tools and none of them do what I need.

    Trivial  scripdodadi
    Code: [Select]
    #!/bin/perl

    $|=1; $, = ' '; $\ = "\n";
    open PIPE, "find . -xdev -type f |" or die('Failed to open PIPE');
    while(<PIPE>) {
    print STDOUT $1.' '.$4.$3.$2.'.txt' if (m/^(.*)([0-9]{2}?)\.([0-9]{2}?)\.([0-9]{4}?)\.txt/i);
    }
    close PIPE;
    wait;

    try first a dry run...  mama won't help

    Paul

    « Last Edit: March 09, 2022, 11:06:10 am by PKTKS »
     

    Offline HalcyonTopic starter

    • Global Moderator
    • *****
    • Posts: 5629
    • Country: au
    Re: Bulk renaming files / Changing the format of the date
    « Reply #9 on: March 10, 2022, 01:04:07 am »
    I've used the Bulk Rename Utility ( https://www.bulkrenameutility.co.uk/ ) under Windows for similar chores.  Was that one of the ones you looked at?

    I used Winsome File Renamer which is pretty powerful, but in this instance, it wouldn't do what I wanted.

    Thanks everyone for your help. I'm not a programmer/coder in any sense of the word. I mostly fumble my way through until I get the shits.
     

    Offline PKTKS

    • Super Contributor
    • ***
    • Posts: 1766
    • Country: br
    Re: Bulk renaming files / Changing the format of the date
    « Reply #10 on: March 10, 2022, 04:41:31 pm »
    I've used the Bulk Rename Utility ( https://www.bulkrenameutility.co.uk/ ) under Windows for similar chores.  Was that one of the ones you looked at?
    (..)
    Thanks everyone for your help. I'm not a programmer/coder in any sense of the word. I mostly fumble my way through until I get the shits.

     ;D ;D ;D

    Neither am i a programmer.... 

    It happens that dealing with MCUs and CPUs over past 4 decades...
    By necessity of having a fully working capable CAD/EDA workstations...
    and network  Infrastructure to handle them...

    By necessity I ended up being fluent in almost 25 dialects and 5 more lame limp some..
    Mostly:
    - ASM for 8080/Z80/x86 8051/AVRs  (which I do not code anymore really..)
    -  A*LOT* of C/ C++ - pure pristine code - not trashed alternatives..
    - PERL - a lot of it as it is the most critical tool for system maint.
    - all the GNU lingos  AWK/SED/ED some LISP
    - A LOT of object Oriented PASCAL (includes DELPHI and TurboVision)
    - RUBY + PHP and some Python
    - a lot of BASIC .. oooh geezz BASIC wtf is this .. and VB as well..

    and some other strange creatures I rather not touch..

    If someone offers me a job as programmer..
    I will just tell him to FUCK OFF !! loudly

    I spent a lot invested a lot of being a competent EE and repair man.

    COMPUTERs .. as they concerns me ..are  my TOOL set..
    just tools to get things done

    Paul  :popcorn:

    PS> BTW..  if some folk wondering these lingos... ask where is JAVA or JS...
      the answer is .. flushed..  i never needed them I don't like nothing about them...
      I rather get rid of them...
    « Last Edit: March 10, 2022, 05:16:04 pm by PKTKS »
     

    Offline eti

    • Super Contributor
    • ***
    • !
    • Posts: 1801
    • Country: gb
    • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
    Re: Bulk renaming files / Changing the format of the date
    « Reply #11 on: October 24, 2022, 07:10:45 am »
    I've used the Bulk Rename Utility ( https://www.bulkrenameutility.co.uk/ ) under Windows for similar chores.  Was that one of the ones you looked at?
    (..)
    Thanks everyone for your help. I'm not a programmer/coder in any sense of the word. I mostly fumble my way through until I get the shits.

     ;D ;D ;D

    Neither am i a programmer.... 

    It happens that dealing with MCUs and CPUs over past 4 decades...
    By necessity of having a fully working capable CAD/EDA workstations...
    and network  Infrastructure to handle them...

    By necessity I ended up being fluent in almost 25 dialects and 5 more lame limp some..
    Mostly:
    - ASM for 8080/Z80/x86 8051/AVRs  (which I do not code anymore really..)
    -  A*LOT* of C/ C++ - pure pristine code - not trashed alternatives..
    - PERL - a lot of it as it is the most critical tool for system maint.
    - all the GNU lingos  AWK/SED/ED some LISP
    - A LOT of object Oriented PASCAL (includes DELPHI and TurboVision)
    - RUBY + PHP and some Python
    - a lot of BASIC .. oooh geezz BASIC wtf is this .. and VB as well..

    and some other strange creatures I rather not touch..

    If someone offers me a job as programmer..
    I will just tell him to FUCK OFF !! loudly

    I spent a lot invested a lot of being a competent EE and repair man.

    COMPUTERs .. as they concerns me ..are  my TOOL set..
    just tools to get things done

    Paul  :popcorn:

    PS> BTW..  if some folk wondering these lingos... ask where is JAVA or JS...
      the answer is .. flushed..  i never needed them I don't like nothing about them...
      I rather get rid of them...


    The only things with “Java” in their names should be brewed and drunk!
     

    Offline eti

    • Super Contributor
    • ***
    • !
    • Posts: 1801
    • Country: gb
    • MOD: a.k.a Unlokia, glossywhite, iamwhoiam etc
    Re: Bulk renaming files / Changing the format of the date
    « Reply #12 on: October 24, 2022, 07:13:22 am »
    I've used the Bulk Rename Utility ( https://www.bulkrenameutility.co.uk/ ) under Windows for similar chores.  Was that one of the ones you looked at?

    Bless dear Windows. How quaint. 😁
     

    Offline westfw

    • Super Contributor
    • ***
    • Posts: 4195
    • Country: us
    Re: Bulk renaming files / Changing the format of the date
    « Reply #13 on: October 27, 2022, 02:18:06 am »
    Quote
    I need to be able to rename all the files
    I am occasionally faced with problems like this, and I usually give up, redirect a directory listing to a file, then use some sort of editor macro (or occasionally a C program) to change the file listing into a trivial script of (many, individual) "rename" commands.  This is usually nicely OS-independent.
     

    Offline AntiProtonBoy

    • Frequent Contributor
    • **
    • Posts: 988
    • Country: au
    • I think I passed the Voight-Kampff test.
    Re: Bulk renaming files / Changing the format of the date
    « Reply #14 on: November 04, 2022, 01:16:22 am »
    This is one for the Python/programming/Powershell nerds. I'm trying to work out a quick way to rename hundreds of files which follow the same format/pattern, for example:

    [Text] [dd].[mm].[yyyy].txt
    e.g.: Log Ending 18.01.2020.txt

    I need to be able to rename all the files so that the dates are expressed in ISO 8601 format and without seperators:

    [Text] [yyyymmdd].txt
    e.g.: Log Ending 20200118.txt

    Any ideas? I've tried a bunch of file renaming tools and none of them do what I need.

    Use Total Commander. It has a multi-rename tool that allows you to use wild cards, shuffle text subranges, and even use regular expressions as part of your renaming efforts.
     

    Online DavidAlfa

    • Super Contributor
    • ***
    • Posts: 5836
    • Country: es
    Re: Bulk renaming files / Changing the format of the date
    « Reply #15 on: November 04, 2022, 01:43:18 am »
    If all dates are placed at the end of the file following "02.10.2022.txt" format:
    Code: (rename.sh) [Select]
    for file in *.txt; do
      name="${file%??????????????}"
      date="$(echo -n "${file%????}" | tail -c10 | awk -F"." '{ print $3$2$1 }')"
      name="$name$date.txt"
      echo Old name: "$file"
      echo New name: "$name"
      # Remove the # to modify the files
      #mv "$file" "$name"
    done
    Code: [Select]
    ./rename.sh
    Old name: new file 23.10.2022.txt
    New name: new file 20221023.txt
    Old name: some other file 10.02.2022.txt
    New name: some other file 20220210.txt
    Old name: this is a long named file 11.12.2022.txt
    New name: this is a long named file 20221211.txt
    « Last Edit: November 04, 2022, 04:19:56 am by DavidAlfa »
    Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
    Stm32 Soldering FW      Forum      Github      Donate
     

    Online SiliconWizard

    • Super Contributor
    • ***
    • Posts: 14297
    • Country: fr
    Re: Bulk renaming files / Changing the format of the date
    « Reply #16 on: November 04, 2022, 03:19:42 am »
    Should all be something easy to do using your favorite scripting language.
     

    Offline ledtester

    • Super Contributor
    • ***
    • Posts: 3032
    • Country: us
    Re: Bulk renaming files / Changing the format of the date
    « Reply #17 on: November 04, 2022, 03:58:16 am »

    Code: [Select]
    #!/bin/perl
    ...

    For perl fans I'd suggest the perl rename utility... use perl regexps from the command line to perform the renaming, e.g.:

    Code: [Select]
    $ rename 's/(.*)(\d\d).(\d\d).(\d\d\d\d).txt/$1$4$3$2.txt/' *.txt

    Use the -n option to just see what's going to happen without doing any renaming.

    Installable on Debian systems via: apt install rename

    A stackoverflow discussion about it:

    https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename
    « Last Edit: November 04, 2022, 04:17:40 am by ledtester »
     


    Share me

    Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
    Smf