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?
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.
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
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.