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.