I need the maximum file name and directory name to be 38 characters long.
As well, if shortening the file name ends up making all of the files in that directory have the same name, then I would like the script to recognize this problem and then append numbers to the end of the file name. For example, the files shown below would have the same name if shortened to 38 characters.:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - The Best is Yet to Come.avi
becomes
Sinatra, Frank & Tony Bennet & Sammie.avi
and
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - New York, New York.avi
becomes
Sinatra, Frank & Tony Bennet & Sammie.avi
so they have the same name, hence I would like to add a suffix of two digit numbers, starting at 01. So the originals would become
Sinatra, Frank & Tony Bennet & Sammie01.avi
Sinatra, Frank & Tony Bennet & Sammie02.avi
As well, I need to replace any occurrence of "[<>=?:;"*+,|]" in both the directory and file name with an underscore "_".
So, it would need to modify the following directory
/home/music/The Best of the Rat Pack + more/
to
[QUOTE]/home/music/The Best of the Rat Pack _ more/
AND it would need to change the following file names located in that directory
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - The Best is Yet to Come.avi
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - New York, New York.avi
to
Sinatra_ Frank & Tony Bennet & Sammie01
Sinatra_ Frank & Tony Bennet & Sammie02
I would need this to work recursively on all of the directories below /home/music/.
I found this "ruby" script using google, but it does not help with appending numbers to file names that are the same once truncated. Perhaps someone knows how to modify it so it does what I want it do do. I hope this helps:
#!/usr/bin/ruby
# usage xbox_prep.rb DIRECTORY_NAME
DirName = ARGV[0]
file_name = "" # name of the file to be checked
old_full_path = "" # name and path of the file to be checked
new_full_path = "" # new name and path of the file (modified if necessary)
basename = "" # basename of the file
extension = "" # extension of the file to be checked
if DirName == nil
puts "usage:"
puts ""
puts "xbox_prep.rb DIRECTORY_NAME"
puts ""
else
Dir.foreach(DirName){
|file_name|
if file_name != "." and file_name !=".."
old_full_path = DirName + "/" + file_name
basename = File.basename(old_full_path, ".*")
extension = File.extname(old_full_path)
# Modify the basename if neccessary
basename.slice!(38,128) # shorten basename to 38 chars
basename.gsub!(/[<>=?:;"*+,|]/,'_') # Remove invalid characters
# rename the file
new_full_path = DirName + "/" + basename + extension
File.rename(old_full_path, new_full_path)
puts basename + extension
end
}
end
Many thanks in advance for the help with this.