ruby reading files in many directories

Discussion in 'Ruby' started by Peter Smith, Jun 4, 2008.

  1. Peter Smith

    Peter Smith Guest

    Hi,

    i have a question. I have a working script, that scans some txt files in
    a directory ,reads the lines and gives a few of that lines out in new
    files.
    But now, i wanted to scan the full subdirectorys for txt files. And copy
    some lines out of that files. The files are in different directorys
    belonging to the same subdirectory.

    for example
    \subdirectory1\txtdirectory1
    \subdirectory1\txtdirectory2
    \subdirectory1\txtdirectory3

    \subdirectory2\txtdirectory1
    \subdirectory2\txtdirectory2
    \subdirectory2\txtdirectory3
    and so on

    i used from wiki the example

    Dir['*.txt'].each do |txt|
    next unless File.file?(txt)

    but this only scans the same directory the ruby file is in.
    Now i changed this in

    Dir['/nameofdirectory/*.txt'].each do |txt|

    this doesnt work.
    then i tried
    Dir['*.txt'].each do |txt|
    next file.directory(".")

    At the end I also wanted to copy the processed files into a new
    directory.
    Its no problem with
    File.new("newfile", "w+") or file.new to make new datas, but i cant
    change the directory with file.open.
    the directory should be called converted or so on and should be in
    subdirectory1\converted
    ...
    I needed hours to get run the script in the same directory and spent
    also hors to get that run with different directorys. But i didnt
    finished the script.
    Thank for answers.
    Cheers
    Bastian
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Smith, Jun 4, 2008
    #1
    1. Advertising

  2. Peter Smith

    Damjan Rems Guest

    Try this:

    def scan_file(file_name)
    if File.ftype(file_name) == 'file'
    puts file_name
    elsif File.ftype(file_name) == 'directory'
    Dir[file_name + "/*"].each { |f| scan_file(f) }
    end
    end
    scan_file(ARGV.first)

    call: ruby test.rb /somedir


    by
    TheR


    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Jun 5, 2008
    #2
    1. Advertising

  3. Peter Smith

    Onur Gungor Guest

    Peter Smith wrote:
    > Hi,
    >
    > i have a question. I have a working script, that scans some txt files in
    > a directory ,reads the lines and gives a few of that lines out in new
    > files.
    > But now, i wanted to scan the full subdirectorys for txt files. And copy
    > some lines out of that files. The files are in different directorys
    > belonging to the same subdirectory.


    try this:
    Dir['./**/*.txt']

    --
    Posted via http://www.ruby-forum.com/.
     
    Onur Gungor, Jun 5, 2008
    #3
  4. Peter Smith

    Ryan Davis Guest

    On Jun 5, 2008, at 04:43 , Onur Gungor wrote:

    > Peter Smith wrote:
    >> i have a question. I have a working script, that scans some txt
    >> files in a directory ,reads the lines and gives a few of that lines
    >> out in new
    >> files. But now, i wanted to scan the full subdirectorys for txt
    >> files. And copy some lines out of that files. The files are in
    >> different directorys belonging to the same subdirectory.

    >
    > try this:
    > Dir['./**/*.txt']


    well... you have to deal with the top level dir as well:

    Dir['./**/*.txt'] + Dir['*.txt']

    that's always bugged me about "**".

    You can also use find for more flexibility:

    require 'find'

    txt = []

    Find.find(".") do |path|
    Find.prune if File.basename(path)[0] == ?.
    txt << path if path =~ /txt$/
    end
     
    Ryan Davis, Jun 5, 2008
    #4
  5. Peter Smith

    Bastian G. Guest

    >> try this:
    ..

    hello thanks for the answers I will try this tomorrow.
    I helped me with a dirthy solution. The script is needed for windows, so
    i wrote a batch file, now it works but this solution is only a rescue
    aid. I am very new on ruby, so I will try and learn...
    --
    Posted via http://www.ruby-forum.com/.
     
    Bastian G., Jun 5, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    0
    Views:
    510
    Joel Finkel
    Sep 12, 2003
  2. Jeffry van de Vuurst
    Replies:
    2
    Views:
    552
    Jeffry van de Vuurst
    Jul 30, 2003
  3. dee
    Replies:
    2
    Views:
    417
  4. Geoff Cox
    Replies:
    20
    Views:
    300
    Geoff Cox
    Aug 19, 2003
  5. Adam Petrie
    Replies:
    8
    Views:
    333
    Adam Petrie
    Oct 11, 2004
Loading...

Share This Page