Passing folder as argument ARGV?

Is there an easy way to pass multiple files on the command line?

I stuck all my files into a folder and passed the folder name as an
argument.
In my code then I assign a variable to ARGV, my_var = ARGV, and then
iterate through each element in ARGV to access all the files.

But it doesn't work and I get an error message. Is there an easy way to
do this?

···

--
Posted via http://www.ruby-forum.com/.

How are you traversing the directory you pass in on the command line ?

···

On Sun, Oct 28, 2012 at 1:21 PM, Joz Private <lists@ruby-forum.com> wrote:

Is there an easy way to pass multiple files on the command line?

I stuck all my files into a folder and passed the folder name as an
argument.
In my code then I assign a variable to ARGV, my_var = ARGV, and then
iterate through each element in ARGV to access all the files.

But it doesn't work and I get an error message. Is there an easy way to
do this?

--
Posted via http://www.ruby-forum.com/\.

First ARGV is an array of all arguments to Ruby program. So regardless
of which OS you are using, do something like the following:

  my_var = ARGV[0]
  dir = File.expand_path(my_var)
  Dir["#{dir}/*"].each {|f| puts f }

Or depending on wether you need the full file path name or just bare
filename, something allong the following:

  p Dir["#{File.expand_path(my_var)}/*"]
  p Dir["#{File.expand_path(my_var)}/*"].each.map { |f| File.basename(f)
}

Regards, igor

···

--
Posted via http://www.ruby-forum.com/.

oops, don't miss the '}' at the end

p Dir["#{File.expand_path(my_var)}/*"].each.map { |f| File.basename(f) }

···

--
Posted via http://www.ruby-forum.com/.

What os are you using? Here is one way to get things to work on OSX or
Linux. First the script

--- fred.rb ---
#!/usr/bin/env ruby

p ARGV

[~]$ ./fred.rb Projects/
["Projects/"]

[~]$ ./fred.rb Projects/*
["Projects/fredkin.bas", "Projects/parsepng.pl", "Projects/visible_xml.xslt"]

It all comes down to how you call it. This does assume that the files
you want are all in the directory you specify and not in directories
inside the directory given in the argument. Also OSX and Linux will
complain if there are too many files.

Brad Smith wrote in post #1081684:

How are you traversing the directory you pass in on the command line ?

my_files = ARGV

my_files.each do |file| ...

Pretty much just like that?

Btw, is it sufficient to simply pass the name of the folder (which is in
the same directory as the executing file), or is there some syntax?

···

--
Posted via http://www.ruby-forum.com/\.

Have you tried to add a debug statement inside your loop so you can
see what is going on.
Also, what is the error code you get.

Btw, is it sufficient to simply pass the name of the folder (which is in
the same directory as the executing file), or is there some syntax?

You could use either the full directory name or you could use dot wildcard
For example,
./myscript ~/*
would give all files under your home directory--nix os

···

On Sun, Oct 28, 2012 at 2:12 PM, Joz Private <lists@ruby-forum.com> wrote:

Brad Smith wrote in post #1081684:

How are you traversing the directory you pass in on the command line ?

my_files = ARGV

my_files.each do |file| ...

Pretty much just like that?

Btw, is it sufficient to simply pass the name of the folder (which is in
the same directory as the executing file), or is there some syntax?

--
Posted via http://www.ruby-forum.com/\.

Thanks guys.

Is each file in the directory given its own element in ARGV, is there
one element ARGV[0] that corresponds to the directory itself?

···

--
Posted via http://www.ruby-forum.com/.

Is each file in the directory given its own element in ARGV,

yes.

ARGV[0] would give the first element in ARGV array.

Brad

···

On Sun, Oct 28, 2012 at 2:32 PM, Joz Private <lists@ruby-forum.com> wrote:

Thanks guys.

Is each file in the directory given its own element in ARGV, is there
one element ARGV[0] that corresponds to the directory itself?

--
Posted via http://www.ruby-forum.com/\.

Joz Private wrote in post #1081694:

Thanks guys.

Is each file in the directory given its own element in ARGV, is there
one element ARGV[0] that corresponds to the directory itself?

  p Dir["#{File.expand_path(ARGV[0])}/*"].each.map { |f|
File.basename(f) }

With the above code you can give the folder name to your program, like
the following:

  $ ruby get_files.rb folder_name

Use {{ p ARGV }} or {{ puts ARGV }} to see what is in it. If you run the
above {{ $ ruby get_files.rb }} in the terminal or console on the
command line then ARGV[0] will have in it the string 'folder_name'. If
you run {{ $ ruby get_files.rb *.rb }}, each element in the ARGV array
will have one file from your current directory.

···

--
Posted via http://www.ruby-forum.com/\.

Joz Private wrote in post #1081694:

... passed the folder name as an argument.

Is each file in the directory given its own element in ARGV

No. The only thing that the ARGV array will contain is the folder name
as a String.

···

--
Posted via http://www.ruby-forum.com/\.

I'm still getting an error:

in 'initialize': Permission denied - ./my_files (Errno:EACCES)

···

--
Posted via http://www.ruby-forum.com/.

You do not have permissions on the directory or file.
Check..

Brad

···

On Oct 28, 2012 2:52 PM, "Joz Private" <lists@ruby-forum.com> wrote:

I'm still getting an error:

in 'initialize': Permission denied - ./my_files (Errno:EACCES)

--
Posted via http://www.ruby-forum.com/\.

Brad Smith wrote in post #1081698:

You do not have permissions on the directory or file.
Check..

Brad

I run the terminal as Admin and they are just .txt files? how do I check
?

···

--
Posted via http://www.ruby-forum.com/\.

If on a nix is, type this.
ls -al the_directory
Reply back with output

···

On Oct 28, 2012 2:56 PM, "Joz Private" <lists@ruby-forum.com> wrote:

Brad Smith wrote in post #1081698:
> You do not have permissions on the directory or file.
> Check..
>
> Brad

I run the terminal as Admin and they are just .txt files? how do I check
?

--
Posted via http://www.ruby-forum.com/\.