[英]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.我正在尝试在测试目录中显示所有 csv 文件名,并获取此类 csv 文件的计数,而不是目录。 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.
以上仅按预期呈现正确的 csv 文件名,但文件计数始终 = 0。是的,我是新来的,试图自学 Ruby,但我已经搜索了我想要完成的内容,但似乎无法将这些部分组合在一起。 I need a file count because if that num is > 3 I would like to send an alert to the screen of some type.
我需要一个文件计数,因为如果该数字大于 3,我想向某种类型的屏幕发送警报。 Wold appreciate any help on this.
非常感谢您对此的任何帮助。 Thanks!
谢谢!
Look like you put the wrong path in file_Num= Dir[".testing/*"].length
Should be看起来你把错误的路径放在
file_Num= Dir[".testing/*"].length
应该是
file_Num = Dir["*.csv"].length
as you already change dir to testing
.因为您已经将 dir 更改为
testing
。
In case you would like to count all csv files in subdirectories如果您想计算子目录中的所有 csv 文件
file_Num = Dir["**/*.csv"].length
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.