Mass change torrent paths

Specific support for Deluge on Microsoft Windows OS
Aeit
New User
New User
Posts: 3
Joined: Wed Apr 14, 2021 10:42 pm

Mass change torrent paths

Post by Aeit »

Today I have reinstalled win10 and removed some drives.
The obvious problem is that all my torrents now point to the path with the old drive letter. And I won't change de drive letter as it leads to issues of installed programs and directiories.

Is there any way to change the drive name in the path of all torrents at once? I can't imagine doing it by hand for thousands of them.
uTorrent had something like resume.dat where it was possible.
mhertz
Moderator
Moderator
Posts: 2182
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Mass change torrent paths

Post by mhertz »

If you search this, then will found inspiration/instructions, e.g I remember our own shamael posted detailed good instructions on how he did it and his experiences,though he did it for trackers, but should be likewise for save_path I would presume - I don't have time now to find shamael's post, but he used sed to replace wanted trackers in torrents.state of deluge profile-folder, with I believe sed, or maybe was search/replace with notepad++ or something. Good idea to backup first, both data and profile(state for sure).

Edit: shamael's post I talked about before, with the actual link posted in his last post there, which is for Linux, but as he stated, is helpful nonetheless for windows users to explain how stop deluge and what needs editing etc. I linked original post of his, instead of the following, because had backstory plus link from Cas, also posted by shamael, with python script from Cas to do this, as alternative solution. Note again, this is for trackers(mass changing passkey of tracker), not paths, but same principle applies. If you Google it, I'm sure you'll find direct examples also, as I remember reading that too vagually. Remember force-recheck afterwards usually fixes errors, if done correctly. Good luck :)

viewtopic.php?t=54208
shamael
Compulsive Poster
Compulsive Poster
Posts: 667
Joined: Sat Oct 08, 2016 9:28 am

Re: Mass change torrent paths

Post by shamael »

Dear @mhertz, thank you for the reference and Hi :).

So to resume you have now the files on a different drive letter and/or different path and you want to reflect this in Deluge.
My procedure should work for this too, how ever my first action will be different:

What if:
-you start Deluge
-pause all torrents
-select them all and use "move storage"

The "move storage" option in the GTK client does well acts on multiple selected torrents, different behaviour than the tracker change so.
The only possible issue I see is the permission on the folder. And for multiple reason in windows do no change permission of a drive but always create a folder and change it there. Once the top folder is created with full permission the "move storage" is able to create subfolder too if needed

Of course if like me you have a specific tree under the configured download folder (movie, TV shows, etc etc) if may be better to replace the path with Notepad++ and Deluge process not running. With a backup copy of your profile (deluge not running there is no risk. But then you will still have to create the folder structure manually, reason I think first option is safer.
mhertz
Moderator
Moderator
Posts: 2182
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Mass change torrent paths

Post by mhertz »

@shamael, Thanks likewise brother, appreciate you and your posting :) Take care mate and good weekend :)
Hansi
New User
New User
Posts: 8
Joined: Sun Nov 19, 2017 8:20 am

Re: Mass change torrent paths

Post by Hansi »

If I wanted to use the second option which files do I have to changed? I would make a backup of the ~/.config/deluge folder and replace all occurances of lets say "/media/disk1/download/" with "/media/disk2/download/" in the relevant files. That's the idea right?
shamael
Compulsive Poster
Compulsive Poster
Posts: 667
Joined: Sat Oct 08, 2016 9:28 am

Re: Mass change torrent paths

Post by shamael »

@mhertz thanks mate, you know the feeling is shared ;

@Hansi the file should be "%appdata%\deluge\state\torrents.state". Do well use Notepad++, and try the path substitution for a couple of torrents. Do not forget to backup the whole directory first. The more backups you take the best.
Hansi
New User
New User
Posts: 8
Joined: Sun Nov 19, 2017 8:20 am

Re: Mass change torrent paths

Post by Hansi »

Thank you. I'll do just that report how it went. I am actually at Linux so I will probably use a different editor.
shamael
Compulsive Poster
Compulsive Poster
Posts: 667
Joined: Sat Oct 08, 2016 9:28 am

Re: Mass change torrent paths

Post by shamael »

Hansi wrote: Thu Aug 26, 2021 6:50 pm Thank you. I'll do just that report how it went. I am actually at Linux so I will probably use a different editor.
This is the Windows subforum, reason of my assumption :).
Check my post here for Linux, it works with Deluge 1.3.x ut I never test it for version 2. You should be able to do just the same with the path substitution instead of tracker
viewtopic.php?f=7&t=54491&p=226225&hili ... er#p226225
Hansi
New User
New User
Posts: 8
Joined: Sun Nov 19, 2017 8:20 am

Re: Mass change torrent paths

Post by Hansi »

Hi,
thank you again for your help. In the end it did not work for me. I mounted the disk at another mount point, tried your little script und produced 100% torrent errors. I then changed the paths manually. As many torrent reside in the same directories it did not took that long. But in the end it was worth it. Thank you!
mhertz
Moderator
Moderator
Posts: 2182
Joined: Wed Jan 22, 2014 5:05 am
Location: Denmark

Re: Mass change torrent paths

Post by mhertz »

Sorry no solution, atleast yet, but anyway just posting because I tested this(as I thought would work) and I also had issues, and so reading up upon it, then it seems binary file-edits needs be same length, and probably the tracker-keys are same length already, but paths often not. It's stated to add null bytes if path shorter, and I tried that, i.e. add '\x00' four times after the changed path as 4 chars shorter, and in vim it's listed correctly with the four '^@' control chars prepended, which is '\x00' in "other writing". I tried using binary mode also, in sed, to no avail and tried bbe, the binary sed alternative, and whatnot, but no luck. I also ran the sed on the fastresume and also deleted it in other try with no change. Only thing working for me where shamael's instructions and a path of exact same char-length, which when also done to fastresume started and was seeding instantously without no recheck needs. I also tried hex-encoder without succes, but mind you, i'm complete noob at all this, so more a testiment of my disabilities than anything else :) I tried running pickling afterwards also, despite not even knowing much what's about honestly, and also trying Cas's script with python2 and still no-go. Strange thing is that when I use sed with the null chars appended, then details tab in deluge shows the path change as correct, but still fails and output from deluge is 'unable to locate download folder' and after I recheck all then '* is a directory', where '*' is some char I don't know, looking like quote char, but again is listed perfectly under details.

Anyway, just wanted to add that and sorry no actual solution to this.

Edit: Sorry, I know this isn't an issue for OP anymore, but just for feature reference I mean, as do come up on occasion.

Edit2: Debug log shows, just for completeness, showing the null bytes, atleast in my way, isn't correct(and original location btw was /home/martin/Downloads, and here using deluge2 on linux):

Code: Select all

20:59:18.356 [DEBUG   ][deluge.core.torrentmanager        :473 ] options: {'add_paused': False, 'auto_managed': True, 'download_location': '/home/martin/video\x00\x00\x00\x00', 'max_connections': -1, 'max_download_speed': -1.0, 'max_upload_slots': -1, 'max_upload_speed': -1.0, 'move_completed': False, 'move_completed_path': '/home/martin/video\x00\x00\x00\x00', 'pre_allocate_storage': False, 'prioritize_first_last_pieces': False, 'remove_at_ratio': False, 'sequential_download': False, 'shared': False, 'stop_at_ratio': False, 'stop_ratio': 2.0, 'super_seeding': False, 'file_priorities': [4], 'mapped_files': {}, 'name': '', 'owner': 'localclient', 'seed_mode': False}
[...]
20:59:18.975 [DEBUG   ][deluge.core.alertmanager          :130 ] torrent_error_alert: archlinux-2021.08.01-x86_64.iso ERROR: (21 Is a directory) /home/martin/video
Edit3: Probably best to unpickle, replace and repickle, to support varying path lengths correctly, so tried change Cas script, which find 'url' indexes in 'trackers' param and replaces, so instead changed to iterate over 'save_path' and made python3/deluge2 script i.e. cPicke to pickle, raw_input() to input() etc, but still have issues as not good at this. I added some print statements, to debug and it ierates and prints save_path correctly for all torrents, but one char on each new line. I'll mess with it little more if have time tomorow, but not much confident in this regarding myself.

Edit4: For feature reference, then this is adapted version of Cas's python2 tracker-key-change script now for python3 and deluge2, sorry to lazy to find and rebuild old versions of boost and libtorrent and install, test, uninstall, for testing on deluge1/python2 - i dropped all the tracker/url/index stuff and adapted to iterate and replace 'save_path' instead, so it now asks for old/new paths and afterwards unpickle, replaces save_path and repickle, with old state backed up - also original script used own user deluge-profile-dir, so I changed it to ask you for path to your torrents.state file(all inputs can be copy/pasted in), so works for deluge user also, but then run script under sudo i.e. 'sudo python3 <scriptname>':

Code: Select all

import os
import sys 
import platform
import shutil
import pickle

orig_tracker_url = input('Original path: ')
new_tracker_url = input('New path: ')

if platform.system() in ('Windows', 'Microsoft'):
    state_file_path = os.path.join(os.environ.get('APPDATA'), 'deluge', 'state', 'torrents.state')
    deluge_dir = os.path.join(os.environ['ProgramFiles'], 'Deluge')
    if os.path.isdir(deluge_dir):
        sys.path.append(deluge_dir)
        sys.path.append(os.path.join(deluge_dir, 'Lib', 'site-packages'))
else:
    state_file_path = input('Path to your torrents.state file: ')

state_file = open(state_file_path, 'rb')
state = pickle.load(state_file)
state_file.close()

state_modified = False
for torrent in state.torrents:
    if torrent.save_path == orig_tracker_url:
        if new_tracker_url:
            torrent.save_path = new_tracker_url
            state_modified = True

if state_modified:
    shutil.copyfile(state_file_path, state_file_path + '.old')
    state_file = open(state_file_path, 'wb')
    pickle.dump(state, state_file)
    state_file.close()
Edit5: Last edit, sorry :) I adapted my ammended Cas script to deluge1/python2 and tested it on linux. Also, as this is the windows sub-forum, then I exceptionally tested this on windows also, by ssh/vnc'ed into a family-member's windows PC and borrowed it for some quick testing, and note you need use the python2 x32 installer for some reason(even despite changing the os.environ['ProgramFiles'] to the x86 version, and verified it, but still complained about not being able to load a dll when using x64 python, so use x32 instead). Anyway, I used latest v2.7.18, but if using a py2.6 deluge install instead of py2.7, then use a python 2.6 x32 installer instead. You don't need add path to torrents.state here, and no admin rights needed, and when entering old/new paths then enter normal windows paths(single backslashes) without quotes and spaces allowed.

I also edited previous script for deluge2/python3 to work also on the unofficial deluge2 installers for windows, but note you need the x64 python installer or x64 python-embedded zip package, in same version(just major and minor version important and not micro) as the python included in the unofficial installer, which you can check in deluge install folder which have a file named e.g. python38.dll for python 3.8.x and python39.dll for 3.9.x etc. As said, I made the script work with those, but however, for unofficial installers specifically, then easiest is to just open a cmd-prompt into installed deluge folder and run 'python <PATH-TO-SCRIPT>', so don't even need install any python additionally to run script, but only works for unofficial deluge2 installers. I tested this with unofficial installers own included python, python installer and python-embedded zip package, for petersasi's and my version.

Anyway, here follows the deluge1/python2 script as mentioned in beginning of this 'Edit5':

Code: Select all

import os
import sys
import platform
import shutil
import cPickle

orig_tracker_url = raw_input('Original path: ')
new_tracker_url = raw_input('New path: ')

if platform.system() in ('Windows', 'Microsoft'):
    state_file_path = os.path.join(os.environ.get('APPDATA'), 'deluge', 'state', 'torrents.state')
    deluge_dir = os.path.join(os.environ['ProgramFiles'], 'Deluge')
    if os.path.isdir(deluge_dir):
        sys.path.append(deluge_dir)
        for item in os.listdir(deluge_dir):
            if item.endswith(('.egg', '.zip')):
                sys.path.append(os.path.join(deluge_dir, item))
else:
    state_file_path = raw_input('Path to your torrents.state file: ')

state_file = open(state_file_path, 'rb')
state = cPickle.load(state_file)
state_file.close()

state_modified = False
for torrent in state.torrents:
    if torrent.save_path == orig_tracker_url:
        if new_tracker_url:
            torrent.save_path = new_tracker_url
            state_modified = True

if state_modified:
    shutil.copyfile(state_file_path, state_file_path + '.old')
    state_file = open(state_file_path, 'wb')
    cPickle.dump(state, state_file)
    state_file.close()
Post Reply