How to get and compare file lists in directory?

N

nkb

Hi.

I've a directory with a long list of files (e.g. *.rb, *.txt, etc.). All
my ruby files are named like 00001.rb, 00002.rb up to 05000.rb or so.
What I am really interested is to get the ruby file list and compare it
with an existing list. And if this new list has additional files, I
would invoke another program with just the integer part of the file as
the arugment. (i.e. runmyprogram (5000) instead of runmyprogram (05000.rb))

My ruby knowledge allows me to write up to here:
directory = Dir
puts Dir.pwd
puts Dir.entries(Dir.pwd)

Could anyone help or point me to how to get set the rest of my
requirement? Thanks!!
 
B

Brian Schröder

nkb said:
Hi.

I've a directory with a long list of files (e.g. *.rb, *.txt, etc.). All
my ruby files are named like 00001.rb, 00002.rb up to 05000.rb or so.
What I am really interested is to get the ruby file list and compare it
with an existing list. And if this new list has additional files, I
would invoke another program with just the integer part of the file as
the arugment. (i.e. runmyprogram (5000) instead of runmyprogram (05000.rb))

Please note, that in ruby you have to but the argument list with the
method call. ie run_my_program(5000).
Also for methods snake case is used, so for better readablity enter "_"
between the words.
My ruby knowledge allows me to write up to here:
directory = Dir
puts Dir.pwd
puts Dir.entries(Dir.pwd)

Could anyone help or point me to how to get set the rest of my
requirement? Thanks!!

Warning: Typed directly into the email:

oldfiles = (File.exist?'old_rb_files.lst') ?
File.open('old_rb_files.lst'){|file|file.read.split("\n")} : []
new_files = Dir['*.rb'] - old_files
new_files.each do | new_file |
puts "Do something with #{newfile}"
end

File.open('old_rb_files.lst', 'a+') do | file |
if /(\d+).rb/ =~ file
run_my_program($1.to_i)
end


regards,

Brian
 
A

Aron Stansvik

s/newfile/new_file

nkb said:
Hi.

I've a directory with a long list of files (e.g. *.rb, *.txt, etc.). All
my ruby files are named like 00001.rb, 00002.rb up to 05000.rb or so.
What I am really interested is to get the ruby file list and compare it
with an existing list. And if this new list has additional files, I
would invoke another program with just the integer part of the file as
the arugment. (i.e. runmyprogram (5000) instead of runmyprogram (05000.rb))

Please note, that in ruby you have to but the argument list with the
method call. ie run_my_program(5000).
Also for methods snake case is used, so for better readablity enter "_"
between the words.
My ruby knowledge allows me to write up to here:
directory = Dir
puts Dir.pwd
puts Dir.entries(Dir.pwd)

Could anyone help or point me to how to get set the rest of my
requirement? Thanks!!

Warning: Typed directly into the email:

oldfiles = (File.exist?'old_rb_files.lst') ?
File.open('old_rb_files.lst'){|file|file.read.split("\n")} : []
new_files = Dir['*.rb'] - old_files
new_files.each do | new_file |
puts "Do something with #{newfile}"
end

File.open('old_rb_files.lst', 'a+') do | file |
if /(\d+).rb/ =~ file
run_my_program($1.to_i)
end

regards,

Brian
 
B

Brian Schröder

Aron said:
s/newfile/new_file

nkb said:
Hi.

I've a directory with a long list of files (e.g. *.rb, *.txt, etc.). All
my ruby files are named like 00001.rb, 00002.rb up to 05000.rb or so.
What I am really interested is to get the ruby file list and compare it
with an existing list. And if this new list has additional files, I
would invoke another program with just the integer part of the file as
the arugment. (i.e. runmyprogram (5000) instead of runmyprogram (05000.rb))

Please note, that in ruby you have to but the argument list with the
method call. ie run_my_program(5000).
Also for methods snake case is used, so for better readablity enter "_"
between the words.

My ruby knowledge allows me to write up to here:
directory = Dir
puts Dir.pwd
puts Dir.entries(Dir.pwd)

Could anyone help or point me to how to get set the rest of my
requirement? Thanks!!

Warning: Typed directly into the email:

oldfiles = (File.exist?'old_rb_files.lst') ?
File.open('old_rb_files.lst'){|file|file.read.split("\n")} : []
new_files = Dir['*.rb'] - old_files
new_files.each do | new_file |
puts "Do something with #{newfile}"
end

File.open('old_rb_files.lst', 'a+') do | file |
if /(\d+).rb/ =~ file
run_my_program($1.to_i)
end

regards,

Brian

Oh, thats true. And also I did not follow my own advice to use
underscores. So s/oldfiles/old_files/ too.

regards,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top