I am writing a gallery maker (HTML) in ruby and could need some help…
I already know how to write to files.
What I don’t know is following:
How can i execute some native commands in Ruby? (For example ls | grep “jpg” >
test.txt)
What I am planning to do is executing this linux command, so that all
image files names get stored in a local file on the HD.
And after that i want to read the filenames in my ruby application one by
one, cause each file has an own line in this output file.
So, it should look like this:
def pictureMethod
executeNativeCommand(“ls | grep “jpg” > pictures.txt”)
pictureFile = File.open(“pictures.txt”,“r”)
while(pictureFile.hasMoreLines)
array.attach(pictureFile.actualLine)
pictureFile.goOneLineDown
end
end
I don’t know the Ruby syntax that’s why I wrote it in kind of “spoken code”
Hope someone can help me out,
Thanks in advance, Benjamin
···
Do You Yahoo!?
Yahoo! Health - Feel better, live better
I just wanted to point this out as a caveat in particular as someone
mentioned that filenames and directories can contain newlines.
db
Didn’t think of that at all - I think any of the other solutions posted that
called ‘ls’ have this problem too. The only way to do it correctly by making
calls to ls seems to be to parse the output of the dired mode (which isn’t
hard, but hard to do in one line).
You can also have ls output strings meant for C which are properly escaped and
quoted, but I don’t know of any way to unescape them without eval (also
possibly causing issues if for some strange reason a #{} sequence is in a
filename):
ls -Q1 --color=never *.jpg.split(“\n”).collect do |item| eval(“#{item}”) end
So I guess the pure ruby methods are better.
btw, to make one of these crazy files in bash (this took me a bit of work) type
the following line:
touch ‘test^v^jing’
where the ^v is typed as control-v
-kyle
···
On Sat, Jul 27, 2002 at 07:00:01AM +0900, Daniel Bretoi wrote:
On Sat, Jul 27, 2002 at 03:55:11AM +0900, Kyle Rawlins wrote:
or even something like
def pictureMethod ls -1 --color=never *jpg*.split(“\n”)
end