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?

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

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.

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

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

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?

oops, don’t miss the ‘}’ at the end

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

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

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

Brad

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?

Joz P. 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 do
|f| File.basename(f)
end

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 ruby file from your current directory.

I’m still getting an error:

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

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

Brad

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
?

Joz P. 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.

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