How can i rename files based on history?

Support for Ubuntu and other Desktop Linux distributions
Post Reply
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

How can i rename files based on history?

Post by hama4reall »

I'm trying to re-rename files based on the history.xml but I don't see any ways documented in the forum or google.
I'm moving from my previous gnome seedbox to my own debian server, and now I want to re-hardlink all my files just like it was on my previous server but it seems when i was using filebot previously I didn't know of xattr and so no xattr metadata are saved so I'm trying to use the history i just updated the parent directory of my files in the history.xml and now want to know how can i hardlink them i know i might be able to do it with

Shell: Select all

ln Old_file_path New_file_path
but there might files i have renamed it couple of time so that might create duplicate files somewhere else.
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

You'll probably need to write your own script for that. The history.xml file will tell you all the source / destination files.


Here's something to get you started:

Shell: Select all

#!/bin/sh
filebot -script fn:history --log OFF | while IFS=$'\t' read -r SRC DST
do
	echo "$SRC -> $DST"
done
:idea: Please read the FAQ and How to Request Help.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

Thanks very much, do you have any idea to how i can avoid the duplicate matches like files that are renamed couples times and they have couple matches in the history?
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

hama4reall wrote: 14 Jul 2024, 07:43 Thanks very much, do you have any idea to how i can avoid the duplicate matches like files that are renamed couples times and they have couple matches in the history?
What should happen if you have a couple of matches in the history? For $SRC? For $DST? For both? Define exactly what you want to do, consider all corner cases, and then modify your code accordingly.


Presumably, you'd check if $SRC or $DST already exist and then do nothing in that case:

Shell: Select all

#!/bin/sh
filebot -script fn:history --log OFF | while IFS=$'\t' read -r SRC DST
do
	# require SRC to exist and require DST to not exist
	if [ -f "$SRC" ] && [ ! -f "$DST" ]
	then
		ln -v "$SRC" "$DST"
	fi
done
:idea: Please read the FAQ and How to Request Help.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

They have the same $SRC but different $DST and in this case the one which is most recent must be linked and the old ones to be ignored.

For example in the history.xml we have a file called

Code: Select all

Bear.Man.2023.1080p.Shafilm.mp4
this file has two records which both point to two different destinations now i want the newer record to be processed and hard linked.
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

What does filebot -script fn:history print for the file at hand?

Shell: Select all

filebot -script fn:history | grep 'Bear.Man.2023.1080p.Shafilm.mp4'
:idea: Please read the FAQ and How to Request Help.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

I've been trying to make filebot show history with

Shell: Select all

filebot -script fn:history
For an hour, in the

Console Output: Select all

~/.filebot/history.xml
file i see all the history but with the command it just prints

Console Output: Select all

Done ヾ(@⌒ー⌒@)ノ 
This is a fresh filebot installation in GUI i imported the history from my old server and closed the program the history.xml file appeared but filebot in cli doesn't show it.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

So i renamed a file in the cli and run

Shell: Select all

filebot -script fn:history
Now it prints

Console Output: Select all

/home/kurdflim/Videos/After.Everything.2023.1080p.AMZN.WEB-DL.Shafilm.m4v   /home/kurdflim/Videos/After Everything (2023).m4v
Done ヾ(@⌒ー⌒@)ノ 

But when i do

Shell: Select all

tail ~/.filebot/history.xml
It prints

Console Output: Select all

    <sequence date="2024-07-14T00:21:28.737+03:00">
        <rename dir="/home/kurdflim/files/CDN5/4K/Movie" from="The.Bikeriders.2023.2160p.WEB-DL.DDP5.1.SDR.H265-AOC.mp4" to="/home/kurdflim/Media/4KMovies/English/The Bikeriders (2024) [tmdbid-1008409]/The Bikeriders (2024).mp4"/>
    </sequence>
    <sequence date="2024-07-14T00:26:56.304+03:00">
        <rename dir="/home/kurdflim/files/CDN5/Series/Kimse.Bilmez.2019" from="Kimse.Bilmez.S01E07.1080p.Shafilm.mp4" to="/home/kurdflim/Media/Shows/Turkish/Kimse Bilmez (2019) [tmdbid-90210]/Season 01/Kimse Bilmez (2019) - S01E07 - Episode 7.mp4"/>
    </sequence>
    <sequence date="2024-07-14T13:11:19.726+03:00">
        <rename dir="/home/kurdflim/Videos" from="After.Everything.2023.1080p.AMZN.WEB-DL.Shafilm.m4v" to="After Everything (2023).m4v"/>
    </sequence>
</history>
Did i imported it wrong or is this expected behavior?
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

I see. Unlucky there. Looks like filebot -script fn:history only prints history entries where the destination file currently exists:
https://github.com/filebot/scripts/blob ... .groovy#L6


Looks like you'll have to write the code entirely by yourself... Pick the language of your choice, parse the XML, do the hardlink operations. It'll be ~20 lines of code.
:idea: Please read the FAQ and How to Request Help.
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

If you're having a hard time writing generic code by yourself / with ChatGPT then here's how you could do it with FileBot Scripting:

Groovy: Select all

#!/usr/bin/env -S filebot -script
getPersistentRenameLog().each{ src, dst ->
	println "* $src -> $dst"

	if (src.exists()) {
		if (!dst.exists()) {
			println src.duplicate(dst)
		} else {
			println "$dst already exists"
		}
	} else {
		println "$src does not exist"
	}
}
:idea: Please read the FAQ and How to Request Help.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

Hi, sorry for my late reply that day i was trying to write a script with gemini all day and i gave up from frustrating and didn't notice your comment, i just tested it and it works but i think it copies the file? Instead of hardlink is this the case?
User avatar
rednoah
The Source
Posts: 23922
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: How can i rename files based on history?

Post by rednoah »

:idea: src.duplicate(dst) will hardlink if possible, and copy if necessary.


:?: Are src and dst are on the same file system?


:?: What is that file system? (NOTE: if it's btrfs then src.duplicate(dst) might reflink)
:idea: Please read the FAQ and How to Request Help.
hama4reall
Posts: 33
Joined: 13 May 2023, 12:21

Re: How can i rename files based on history?

Post by hama4reall »

Got i just checked it's hardlinking them so this is it, the file system is ext4 and both src and dst are one the same file system. So all of the files is hardlinked now so thanks a lot appreciate it.
Post Reply