- 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?
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)
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
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.
ls |sed/awk "transform old filename into 'mv "old" "new"'" >tmp
less tmp # sanity check before it's too late ;)
sh <tmp
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.
#!/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;
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'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.
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.
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
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...
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 need to be able to rename all the files
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.
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
./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
Code: [Select]#!/bin/perl
...
$ rename 's/(.*)(\d\d).(\d\d).(\d\d\d\d).txt/$1$4$3$2.txt/' *.txt