[Tool] xFTPconvert

Requests, suggestions, support, and everything else python / plugin related.
Post Reply
tim619
Posts: 204
Joined: Sun Mar 10, 2013 10:22 am
Has thanked: 25 times
Been thanked: 59 times

[Tool] xFTPconvert

Post by tim619 »

This little python script will rename all contents of a given folder to have less equal 42 symbols in the name so they can be transfered directly on ftp.

Note: Don't use the script on games, obviously that wouldn't work. Also on music files the m3u playlist wouldn't fit anymore.
There is no problem running the script multiple times on same folder it will only handle on lengths>42 :)

To run the script open a console/terminal and type:

Code: Select all

python C:\path\to\xFTPconvert.py
Update: The script assumes you are putting it in the folder you want to run through, so If you have your folder on C:\Movies put the script inside C:\Movies and it will run over all files and folders within this directory. (Of course this should also work on Linux and Mac)

Save as xFTPconvert.py or download attached :

Code: Select all

############################################
#                xFTPconvert               #
#  written by tim619 for xbmc4xbox forum   #
############################################

import os

#For manual naming:
#FilesFolderName = "480p" # Here goes the foldername
#Path = os.path.dirname(os.path.realpath(__file__))
#FilesPath = Path+"\\"+FilesFolderName

FilesPath = os.path.dirname(os.path.abspath(__file__))

if not os.path.isdir(FilesPath):
	print("You have to set up the folder name!")
else:
	for root, dirs, files in os.walk(FilesPath):
		for filename in files:
			length = len(filename)# needed, otherwise if checked directly python thinks of an array of files...
			if length>42:
				ext = os.path.splitext(filename)[1][1:].strip().lower()
				lenext = len(ext)+1
				fixed = filename[:42-lenext]
				while fixed.endswith(".") or fixed.endswith(" ") or fixed.endswith("_") or fixed.endswith("-") or fixed.endswith("(") or os.path.isfile(root+"\\"+fixed+"."+ext):
					fixlen = len(fixed)-1
					fixed = fixed[:fixlen]
				fixedfile = fixed+"."+ext
				#print(fixedfile)
				os.rename(root+"\\"+filename,root+"\\"+fixedfile)
	#Now all files are good and we do the same run again on folder names...
	for root, dirs, files in os.walk(FilesPath): #why this way? because its MUCH easier!
		for foldername in dirs:
			folderlen = len(foldername)
			if folderlen>42:
				fixedfolder = foldername[:42]
				fixfolderlen = len(fixedfolder)-1
				while fixedfolder.endswith(".") or fixedfolder.endswith(" ") or fixedfolder.endswith("_") or fixedfolder.endswith("-") or fixedfolder.endswith("(") or os.path.isdir(root+"\\"+fixedfolder[:fixedfolderlen]):
					fixfolderlen = len(fixedfolder)-1
					fixedfolder = fixedfolder[:fixfolderlen]
				print(fixedfolder)
				os.rename(root+"\\"+foldername,root+"\\"+fixedfolder)
			else:
				print(foldername)
Attachments
xFTPconvert.zip
xFTPconvert *Update*
(829 Bytes) Downloaded 104 times
Last edited by tim619 on Thu Aug 21, 2014 4:38 pm, edited 2 times in total.
tim619
Posts: 204
Joined: Sun Mar 10, 2013 10:22 am
Has thanked: 25 times
Been thanked: 59 times

Re: [Tool] xFTPconvert

Post by tim619 »

Update: Hopefully fluent and final version. Fixed if file/folder exists which appears on rom renaming. Added clever renaming keyword and last but not least setting a folder name is now automated by putting the script inside the folder.
User avatar
Oldxboxusa
Posts: 145
Joined: Tue Aug 28, 2012 5:47 pm
Has thanked: 14 times
Been thanked: 3 times

Re: [Tool] xFTPconvert

Post by Oldxboxusa »

Works fine on my Mac thank you.
tim619
Posts: 204
Joined: Sun Mar 10, 2013 10:22 am
Has thanked: 25 times
Been thanked: 59 times

Re: [Tool] xFTPconvert

Post by tim619 »

Your welcome, good to hear that it's working for someone, too ;)
(Should by the way work on both python 2.x and 3.x)
Post Reply