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

On Sat, Sep 3, 2011 at 2:59 PM, Jeffrey S. [email protected]
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…

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 Sat, Sep 3, 2011 at 3:27 PM, Jeffrey S. [email protected]
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:

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

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 S. [email protected]
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

On Sat, Sep 3, 2011 at 4:26 PM, Jeffrey S. [email protected]
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?

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

Hassan S. ------------------------ [email protected]
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.

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 Sun, Sep 4, 2011 at 2:35 AM, Jeffrey S. [email protected]
wrote:

and starting IRB there?


Hassan S. ------------------------ [email protected]
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 `’

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