Opening a file with variable name

Hello All,
I have what I believe to be an easy question. Is it possible to open a file that is defined as a variable name?If it is possible, how would I go about it?

I have tried File.open(variable), but I always get an error message stating "Invaild argument - path to file (Errno::EINVAL).

Thanks,
Jeff

ruby-1.9.2-p290 > variable = "foo.txt"
=> "foo.txt"
ruby-1.9.2-p290 > File.open(variable)
=> #<File:foo.txt>
ruby-1.9.2-p290 >

In the above case I know there's a file with that name in the same
directory where I started irb.

Maybe you should look at what your variable actually contains...

···

On Sat, Sep 3, 2011 at 2:59 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

I have tried File.open(variable), but I always get an error message stating "Invaild argument - path to file (Errno::EINVAL).

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

Hello Hassan,

Thanks for the quick response. When I do a puts "path", this is what I get: \\faxgw04\c$\myapp\jobs\Processed\7.7.7\App Fax Service\sd3n4v1.20110902.1024.55435838.JMym.txt". This is the path to the file I want to open.

Jeff

···

On Sep 3, 2011, at 6:16 PM, Hassan Schroeder wrote:

On Sat, Sep 3, 2011 at 2:59 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

I have tried File.open(variable), but I always get an error message stating "Invaild argument - path to file (Errno::EINVAL).

ruby-1.9.2-p290 > variable = "foo.txt"
=> "foo.txt"
ruby-1.9.2-p290 > File.open(variable)
=> #<File:foo.txt>
ruby-1.9.2-p290 >

In the above case I know there's a file with that name in the same
directory where I started irb.

Maybe you should look at what your variable actually contains...

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan Schroeder | about.me
twitter: @hassan

? What OS is this? Regardless, try changing all those '\' to '/' first :slight_smile:

···

On Sat, Sep 3, 2011 at 3:27 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

Thanks for the quick response. When I do a puts "path", this is what I get: \\faxgw04\c$\myapp\jobs\Processed\7.7.7\App Fax Service\sd3n4v1.20110902.1024.55435838.JMym.txt". This is the path to the file I want to open.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

This text is a top post. The convention here is to bottom post :slight_smile:

···

On Sat, Sep 3, 2011 at 5:27 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

Hello Hassan,

Thanks for the quick response. When I do a puts "path", this is what I get:
\\faxgw04\c$\myapp\jobs\Processed\7.7.7\App Fax
Service\sd3n4v1.20110902.1024.55435838.JMym.txt". This is the path to the
file I want to open.

Jeff

What do these give you? (run from the script, not from irb)

p File.exist?(variable)
p File::SEPARATOR
p File.expand_path(__FILE__)
p Dir.pwd

I changed the slashed and escaped the file path ( I noticed after the fact that my path has spaces), and no change in the error at all.

Jeff

···

On Sep 3, 2011, at 6:48 PM, Hassan Schroeder wrote:

On Sat, Sep 3, 2011 at 3:27 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

Thanks for the quick response. When I do a puts "path", this is what I get: \\faxgw04\c$\myapp\jobs\Processed\7.7.7\App Fax Service\sd3n4v1.20110902.1024.55435838.JMym.txt". This is the path to the file I want to open.

? What OS is this? Regardless, try changing all those '\' to '/' first :slight_smile:

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan Schroeder | about.me
twitter: @hassan

Have you tried 1) opening a file that's in the same directory you've
started IRB in? 2) changing directories to the one with the target file
and starting IRB there?

···

On Sat, Sep 3, 2011 at 4:26 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

I changed the slashed and escaped the file path ( I noticed after the fact that my path has spaces), and no change in the error at all.

Again, what OS? <<

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

The OS is Windows 2003.

I can open the file from the cmd line (not IRB), and when I hardcode the path in File.open(\\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\xxx) it works fine.

I created the variable by combining two other variables, and when I try the File.exists? it fails. If I assign the complete path including file (i.e. \\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\sd3n4v1.20110902.1924.55435838.JMym.epic) it works fine.

Could it be the way I'm creating the variable? This is the process:

ertf = "\\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\"+filevariable

Jeff

···

On Sep 3, 2011, at 7:39 PM, Hassan Schroeder wrote:

On Sat, Sep 3, 2011 at 4:26 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

I changed the slashed and escaped the file path ( I noticed after the fact that my path has spaces), and no change in the error at all.

Again, what OS? <<

Have you tried 1) opening a file that's in the same directory you've
started IRB in? 2) changing directories to the one with the target file
and starting IRB there?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan Schroeder | about.me
twitter: @hassan

I changed the slashed and escaped the file path ( I noticed after the fact that my path has spaces), and no change in the error at all.

Again, what OS? <<

Have you tried 1) opening a file that's in the same directory you've
started IRB in? 2) changing directories to the one with the target file
and starting IRB there?

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan Schroeder | about.me
twitter: @hassan

The OS is Windows 2003.

I can open the file from the cmd line (not IRB), and when I hardcode the path in File.open(\\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\xxx) it works fine.

This cannot work. You need at least quotes to make that valid Ruby
code. Please show the *exact* code you are using.

I created the variable by combining two other variables, and when I try the File.exists? it fails. If I assign the complete path including file (i.e. \\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\sd3n4v1.20110902.1924.55435838.JMym.epic) it works fine.

Could it be the way I'm creating the variable? This is the process:

ertf = "\\\\sdcfaxgw04\\c$\\epic\\jobs\\Processed\\7.7.7\\Epic Print Service\\"+filevariable

What is in filevariable? Do you happen to read "filevariable" from
stdin? Then this is what happens:

irb(main):001:0> File.open("price.sql").close
=> nil

OK, file is there. Now the test:

irb(main):002:0> filevariable = gets
price.sql
=> "price.sql\n"
irb(main):003:0> File.open(filevariable).close
Errno::ENOENT: No such file or directory - price.sql

  from (irb):3:in `initialize'
  from (irb):3:in `open'
  from (irb):3
  from /usr/local/bin/irb19:12:in `<main>'

Note the trailing newline. You can get rid of it by doing

irb(main):004:0> filevariable.chomp!
=> "price.sql"
irb(main):005:0> filevariable
=> "price.sql"

Kind regards

robert

···

On Sun, Sep 4, 2011 at 2:35 AM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

On Sep 3, 2011, at 7:39 PM, Hassan Schroeder wrote:

On Sat, Sep 3, 2011 at 4:26 PM, Jeffrey Smith <jesmith2095@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/