-Alex- Posted October 4, 2008 Share Posted October 4, 2008 Basically, I have some code to recursively remove part of a file name, for example: #!/bin/sh for f in "$@" do mv "$f" "${f/\yup/}" done For example, running ./test.sh *.avi would remove 'yup' from every file ending .avi. I just need something like the above but to rename '101 Blah.avi' to '101 - Blah.avi', where the file name will always be '### A few words.avi' Any help appreciated, thanks. :) Link to comment Share on other sites More sharing options...
Lechio Posted November 6, 2008 Share Posted November 6, 2008 If by now you haven't found a solution to this here's something that could help you: #!/usr/bin/env python # -*- coding: iso-8859-15 -*- # Leetmover by Lechio # This program is free software; you can redistribute it and/or modify it under the terms of the # GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or (at your option) any later version. # Copyright (C) 2008 Lechio (TM) import os,sys#The imports. #Get the working directory. try:workdir=str(sys.argv[1]) except:#Output an error when working directory is missing. print "Invalid Syntax. Usage: 'leetmover <working directory>'" sys.exit() #Directory listing operations. dirlist,filelist,copylist=[],[],[] if os.path.isdir(str(workdir)):#Check if the working directory is a valid directory. try:dirlist=os.listdir(str(workdir))#List the members of that directory. except:print "Error listing directory"#Output an error if listing fails. for i in range(len(dirlist)):#Isolate the files (cause that's what you want to rename). if os.path.isfile(os.path.join(workdir,dirlist[i])): filelist.append(dirlist[i]) else:print "Invalid directory. Please insert a valid directory." #Output an error if the working directory is not a valid directory. #String operations. for i in range (len(filelist)): curstr=curstrb=filelist[i] curstr=curstr[0:4].replace(" ","")#Get the 3 first characters and at the same time remove the " ". curstrb=curstrb[5:]#Get the other missing characters. curstrtot=str(curstr)+' - '+str(curstrb)#Sum both words. curstrtot=os.path.join(workdir,curstrtot) copylist.append(curstrtot) #Copy operations. for i in range(len(copylist)): try: orig=os.path.join(workdir,filelist[i])#The origin file dest=copylist[i]#The destination file os.system("mv -fv '%s' '%s' "%(orig,dest))#Make the copy except:print "Copy failed."#Output an error here if something fails in this section. It's written in python. The file is full with comments so it's easy do edit. Here's how it works: two files, "100 ABC.avi" and "101 ABC.avi", located at directory, let's say "/media/videos". To make the copy we would run from bash: python leetmover.py "/media/videos" The resulting files would be named: "/media/videos/101 - ABC.avi" "/media/videos/100 - ABC.avi" Hope this is what you want, if there is something that need to be changed let me know. Python file attached for easy use. leetmover.zip Link to comment Share on other sites More sharing options...
Recommended Posts