Topic "Rename files and repalce illegal characters for fatx"

Author Message
mzd

Guest


Hello all.

I was wondering if there is a way to write a script so that WinSCP, using the FTP protocol, will rename and remove illegal characters so that they are compatible with the fatx file system (used on original xbox).
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
Can you provide more details about the process you suggest? What characters, what should they be replaced with, etc...
_________________
Martin Prikryl
mzd

Guest


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.:

Quote:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - The Best is Yet to Come.avi
becomes
Quote:
Sinatra, Frank & Tony Bennet & Sammie.avi
and
Quote:
Sinatra, Frank & Tony Bennet & Sammie Davis Jr - New York, New York.avi
becomes
Quote:
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
Quote:
Sinatra, Frank & Tony Bennet & Sammie01.avi
Quote:
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

Quote:
/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

Quote:
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
Quote:
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:

Code:


#!/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.
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License