Running d:\ruby\test.rb is always successful.
Running the copy of this file path which is at d:\программирование\test.rb fails, apparently because it contains non-ASCII, Cyrillic in this case, symbols:
No such file or directory - D:\... (Errno::ENOENT)
What should I do to make it work?
I'm using Ruby 1.9 and Windows.
If you are creating your path like:
"d:\программирование\test.rb"
Then Ruby is treating the "\t" character as if is is escaped: It is converting \t into a tab before passing the filename to any routine. That character is illegal in a filename. Well, maybe not illegal, but a real pain to deal with and not what you expect.
Instead, use:
'd:\программирование\test.rb'
Or, better yet, let Ruby do the lifting and reverse your backslashes when you define the name. Ruby should do the right thing and convert them on the fly for you:
"d:/программирование/test.rb"
Related
I need to parse a basename in ruby a from file path which I get as input. Unix format works fine on Linux.
File.basename("/tmp/text.txt")
return "text.txt".
However, when I get input in windows format:
File.basename("C:\Users\john\note.txt")
or
File.basename("C:\\Users\\john\\note.txt")
"C:Usersjohn\note.txt" is the output (note that \n is a new line there), but I didn't get "note.txt".
Is there some nice solution in ruby/rails?
Solution:
"C:\\test\\note.txt".split(/\\|\//).last
=> "note.txt"
"/tmp/test/note.txt".split(/\\|\//).last
=> "note.txt"
If the Linux file name doesn't contain \, it will work.
Try pathname:
require 'pathname'
Pathname.new('C:\Users\john\note.txt').basename
# => #<Pathname:note.txt>
Pathname docs
Ref How to get filename without extension from file path in Ruby
I'm not convinced that you have a problem with your code. I think you have a problem with your test.
Ruby also uses the backslash character for escape sequences in strings, so when you type the String literal "C:\Users\john\note.txt", Ruby sees the first two backslashes as invalid escape sequences, and so ignores the escape character. \n refers to a newline. So, to Ruby, this literal is the same as "C:Usersjohn\note.txt". There aren't any file separators in that sequence, since \n is a newline, not a backslash followed by the letter n, so File.basename just returns it as it receives it.
If you ask for user input in either a graphical user interface (GUI) or command line interface (CLI), the user entering input needn't worry about Ruby String escape sequences; those only matter for String literals directly in the code. Try it! Type gets into IRB or Pry, and type or copy a file path, and press Enter, and see how Ruby displays it as a String literal.
On Windows, Ruby accepts paths given using both "/" (File::SEPARATOR) and "\\" (File::ALT_SEPARATOR), so you don't need to worry about conversion unless you are displaying it to the user.
Backslashes, while how Windows expresses things, are just a giant nuisance. Within a double-quoted string they have special meaning so you either need to do:
File.basename("C:\\Users\\john\\note.txt")
Or use single quotes that avoid the issue:
File.basename('C:\Users\john\note.txt')
Or use regular slashes which aren't impacted:
File.basename("C:/Users/john/note.txt")
Where Ruby does the mapping for you to the platform-specific path separator.
Aptana is returning:
Invalid escape character syntax
File.open("C:\Users\C*****\Documents\RubyProjects\text.txt
What do I do?
\ is an escape charecter in most languages, so the compiler expects an escaped char after it, in this case its also \, so you just need to use 2 of them
File.open("C:\\Users\\C*****\\Documents\\RubyProjects\\text.txt
Ruby doesn't need you to use reverse slashes. In your string
"C:\Users\C*****\Documents\RubyProjects\text.txt"
you're confusing Ruby because you have reverse-slashes, which denote escapes in a double-quoted (interpreted) string and make Ruby throw up. Instead use:
"C:/Users/C*****/Documents/RubyProjects/text.txt"
From the IO documentation:
Ruby will convert pathnames between different operating system conventions if possible. For instance, on a Windows system the filename "/gumby/ruby/test.rb" will be opened as "\gumby\ruby\test.rb". When specifying a Windows-style filename in a Ruby string, remember to escape the backslashes:
"c:\\gumby\\ruby\\test.rb"
Our examples here will use the Unix-style forward slashes; File::ALT_SEPARATOR can be used to get the platform-specific separator character.
I've uploaded a big number of files including their folder structure to my Ubuntu 12.04 LTS Server using WinSCP.
The goal is to access these files in Owncloud.
However, all files that contain special character like German Umlauts cause problems. In Ownclouds view, their name is cut off at the special character and trying to view that folder or file will send you back to the folder root.
Using ls, the special character is always displayed as a question mark, e.g. "Moterschwei?en1.jpg"
What works is manually renaming them through "mv" in the shell. Inserting the special char properly, e.g. "Motorschweißen1.jpg" for this example, does work, but doing this for all of them would take ages.
Using find . -name "?" will not yield any hits.
Is there any way to replace all of those special characters, e.g. with an underscore?
Try the command rename:
rename 'y/\W/_' *
The above command will replace all non alphanumeric characters with _. See http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators and http://perldoc.perl.org/perlre.html#Special-Backtracking-Control-Verbs for the documentation of perl regex expression.
Without using slashes (/) or backslashes (\), is there any way I can create a file on an external drive using a relative path on windows?
Or, put less confusingly...
Say I have to append whatever I do to the path:
C:\Application Data\blib\
Is there any way I can do something like:
C:\Application Data\blib\<WINDOWS_SORCERY>external:\wherever\file.txt
I've tried inserting delete characters (\x7f) and backspace characters (\x08), and the reserved characters, but nothing doing. And from what I can tell the reserved characters would only be helpful if the file were being created from within a batch script; it's being created in a Java program.
edit: By 'nothing doing' I mean I get an error message; the program doesn't strip those characters out.
I'm trying to catalog a bunch of files on OSX using ruby, essentially doing this:
hash = Digest::SHA1.hexdigest(File.open(fullpath).read)
This is failing on filenames that contain apostrophes, which are legal characters for a filename.
The File.open works, but I get an "Errno::EINVAL: Invalid argument" error from the read. The filenames are coming directly out of a Dir[] glob.
I've tried escaping them with backslashes, but that doesn't seem to work.
What's the right way to escape these filenames?