rickman
rickman

Reputation: 11

How to get file count with Ruby

I have the following code :

system('cls')
Dir.chdir("testing")
puts Dir.pwd

Dir.glob('*.csv').each do|csv_filename|
    next if csv_filename == '.' or csv_filename == '..'
     puts "\t" + csv_filename

 end

file_Num= Dir[".testing/*"].length
     puts "file count = " + file_Num.to_s

I am trying to display all csv filenames within the testing directory and get a count of such csv files, not directories. The above renders only the correct csv file names as expected but file count always = 0. Yes, I am new trying to teach myself Ruby but I've searched for what I am trying to accomplish and cannot seem to put the pieces together. I need a file count because if that num is > 3 I would like to send an alert to the screen of some type. Wold appreciate any help on this. Thanks!

Upvotes: 0

Views: 249

Answers (1)

Huy Ha
Huy Ha

Reputation: 179

Look like you put the wrong path in file_Num= Dir[".testing/*"].length Should be

file_Num = Dir["*.csv"].length

as you already change dir to testing.

In case you would like to count all csv files in subdirectories

file_Num = Dir["**/*.csv"].length

Upvotes: 1

Related Questions