Page 1 of 1

[Script] Relocate files in qBT

Posted: 12 Apr 2024, 19:51
by rednoah
This post-processing script will relocate files in qBT after moving / renaming files with FileBot:

Groovy: Select all

def api = 'http://localhost:8080/api/v2'
def login = [username: 'YOUR_USERNAME', password: 'YOUR_PASSWORD']

// post HTML login form and receive login cookie
submit "$api/auth/login", login

// retrieve qBT torrent index
def info = curl "$api/torrents/info"

def renameFile = [] as Set
def setLocation = [] as Set

// find torrent for each processed file
args.eachWithIndex{ target, i ->
	def source = model[i].f
	def torrent = info.find{ source.path.startsWith(it.content_path) }
	if (torrent) {
		def content = curl "$api/torrents/files?hash=$torrent.hash"
		content.each{
			if (source == torrent.save_path / it.name) {
				renameFile += [hash: torrent.hash, oldPath: it.name, newPath: target.name]
				setLocation += [hashes: torrent.hash, location: target.dir]

				// handle companion files
				content.each{
					def oldPath = torrent.save_path / it.name
					def newPath = target.dir / oldPath.name
					if (oldPath !in model.f && oldPath.exists() && !newPath.exists()) {
						action(oldPath, newPath)
						renameFile += [hash: torrent.hash, oldPath: it.name, newPath: newPath.name]
					}
				}
			}
		}
	} else {
		println "Torrent not found: $source"
	}
}

// send relocate requests
renameFile.each{ submit "$api/torrents/renameFile", it }
setLocation.each{ submit "$api/torrents/setLocation", it }

// clear login cookie
submit "$api/auth/logout", [:]



Did you arrive here via a search?

:idea: You may prefer to use --action hardlink to simply have the same file twice at two or more file paths. No qBT relocation necessary.

:idea: You may prefer to use --action keeplink to replace the original file path with a symlink to the new file path. No qBT relocation necessary.