Bulk renaming files / Changing the format of the date - Page 1
Forum section: electronics / general

author: Halcyon
author_rank: Unknown
timestamp: March 09, 2022, 01:36:55 am
content: 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: ...txte.g.: Log Ending 18.01.2020.txtI need to be able to rename all the files so that the dates are expressed in ISO 8601 format and without seperators: .txte.g.: Log Ending 20200118.txtAny ideas? I've tried a bunch of file renaming tools and none of them do what I need.

author: Cerebus
author_rank: Super Contributor
timestamp: March 09, 2022, 02:23:06 am
content: 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 justRename all the files in directoryXthat match the pattern "[Text, any text at all] ...txt",or it is itRename all the files in directoryXthat match the pattern "[Text that I just specified for this particular renaming] ...txt"?Is the space in the input filename optional or mandatory? Isdirectory Xgoing to be specified, or is it just the current directory?Also what do you want the behaviour to be for files in directoryXthat don't match your basic pattern: ignore them, ask you what to do, do something else?

author: Halcyon
author_rank: Unknown
timestamp: March 09, 2022, 02:36:39 am
content: Quote from: Cerebus on March 09, 2022, 02:23:06 amIt'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.

author: Halcyon
author_rank: Unknown
timestamp: March 09, 2022, 03:04:25 am
content: 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:from pathlib import Pathp= Path('.')a = list(p.glob('**/*.txt'))for x in a:a = xx = str(x).split(' ')y = x.split('.')print(x, y[:-1])k = (x + ' '+x + ' '+x + ' '+y+y+y + '.txt')a.rename(k)

author: Cerebus
author_rank: Super Contributor
timestamp: March 09, 2022, 03:05:58 am
content: Quote from: edavid on March 09, 2022, 02:21:34 amYou 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 .*bothat 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 TEXTyour pattern here\.txt$

author: Cerebus
author_rank: Super Contributor
timestamp: March 09, 2022, 03:09:57 am
content: Quote from: Halcyon on March 09, 2022, 03:04:25 amYes, 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.

author: edpalmer42
author_rank: Super Contributor
timestamp: March 09, 2022, 03:17:43 am
content: 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?

author: magic
author_rank: Super Contributor
timestamp: March 09, 2022, 08:49:18 am
content: My way:Code:ls |sed/awk "transform old filename into 'mv "old" "new"'" >tmpless tmp # sanity check before it's too late ;)sh <tmp

author: PKTKS
author_rank: Super Contributor
timestamp: March 09, 2022, 11:02:56 am
content: Quote from: Halcyon on March 09, 2022, 01:36:55 amTrivial scripdodadiCode:#!/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 helpPaul

author: Halcyon
author_rank: Unknown
timestamp: March 10, 2022, 01:04:07 am
content: Quote from: edpalmer42 on March 09, 2022, 03:17:43 amI 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.

author: PKTKS
author_rank: Super Contributor
timestamp: March 10, 2022, 04:41:31 pm
content: Quote from: Halcyon on March 10, 2022, 01:04:07 amNeither am i aprogrammer....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 themostcritical 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 asprogrammer..I will just tell him to FUCK OFF !! loudlyI 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 donePaulPS> 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...

author: eti
author_rank: Super Contributor
timestamp: October 24, 2022, 07:10:45 am
content: Quote from: PKTKS on March 10, 2022, 04:41:31 pmThe only things with “Java” in their names should be brewed and drunk!

author: eti
author_rank: Super Contributor
timestamp: October 24, 2022, 07:13:22 am
content: Quote from: edpalmer42 on March 09, 2022, 03:17:43 amBless dear Windows. How quaint. 😁

author: westfw
author_rank: Super Contributor
timestamp: October 27, 2022, 02:18:06 am
content: QuoteI 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.

author: AntiProtonBoy
author_rank: Frequent Contributor
timestamp: November 04, 2022, 01:16:22 am
content: Quote from: Halcyon on March 09, 2022, 01:36:55 amUse 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.

author: DavidAlfa
author_rank: Super Contributor
timestamp: November 04, 2022, 01:43:18 am
content: If all dates are placed at the end of the file following "02.10.2022.txt" format:Code: (rename.sh)for file in *.txt; doname="${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"doneCode:./rename.shOld name: new file 23.10.2022.txtNew name: new file 20221023.txtOld name: some other file 10.02.2022.txtNew name: some other file 20220210.txtOld name: this is a long named file 11.12.2022.txtNew name: this is a long named file 20221211.txt

author: SiliconWizard
author_rank: Super Contributor
timestamp: November 04, 2022, 03:19:42 am
content: Should all be something easy to do using your favorite scripting language.

author: ledtester
author_rank: Super Contributor
timestamp: November 04, 2022, 03:58:16 am
content: Quote from: PKTKS on March 09, 2022, 11:02:56 amFor perl fans I'd suggest the perl rename utility... use perl regexps from the command line to perform the renaming, e.g.:Code:$ rename 's/(.*)(\d\d).(\d\d).(\d\d\d\d).txt/$1$4$3$2.txt/' *.txtUse the -n option to just see what's going to happen without doing any renaming.Installable on Debian systems via: apt install renameA stackoverflow discussion about it:https://stackoverflow.com/questions/22577767/get-the-perl-rename-utility-instead-of-the-built-in-rename