Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid chars filter for file/folder name? (ruby)

Tags:

filenames

ruby

My script downloads files from the net and then it saves them under the name taken from the same web server. I need a filter/remover of invalid characters for file/folder names under Windows NTFS.

I would be happy for multi platform filter too.

NOTE: something like htmlentities would be great....

like image 881
Radek Avatar asked Feb 16 '10 04:02

Radek


People also ask

What characters are allowed in filenames?

Supported characters for a file name are letters, numbers, spaces, and ( ) _ - , . *Please note file names should be limited to 100 characters. Characters that are NOT supported include, but are not limited to: @ $ % & \ / : * ? " ' < > | ~ ` # ^ + = { } [ ] ; !

Why are special characters not allowed in filenames?

To clarify this answer, these special characters could interfere with parsing a command line (or path) if they were in a filename.

Which is the invalid file name in Unix?

Same problem as with the "?". These are not invalid characters to Unix; typically only the NUL character and the / character are invalid filenames (the / being the directory separator).


1 Answers

Like Geo said, by using gsub you can easily convert all invalid characters to a valid character. For example:

file_names.map! do |f|
  f.gsub(/[<invalid characters>]/, '_')
end

You need to replace <invalid characters> with all the possible characters that your file names might have in them that are not allowed on your file system. In the above code each invalid character is replaced with a _.

Wikipedia tells us that the following characters are not allowed on NTFS:

  • U+0000 (NUL)
  • / (slash)
  • \ (backslash)
  • : (colon)
  • * (asterisk)
  • ? (question mark)
  • " (quote)
  • < (less than)
  • (greater than)

  • | (pipe)

So your gsub call could be something like this:

file_names.map! { |f| f.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') }

which replaces all the invalid characters with an underscore.

like image 114
liwp Avatar answered Sep 20 '22 10:09

liwp