2016-06-15 77 views
0

我在一个目录中有385个子文件夹,每个子文件夹都包含一个CSV文件以及几个pdf文件。我试图找到一种方法来浏览每个子文件夹,并将pdf的列表写入一个txt文件。 (我知道有比Ruby更好的语言来做这件事,但我是编程新手,而且它是我唯一知道的语言。)Ruby - 将子文件夹的文件名写入txt文件

我有代码完成工作,但问题是我运行到它是否也列出了子文件夹目录。示例:不是将“document.pdf”写入文本文件,而是写入“subfolder/document.pdf”。

有人可以告诉我如何编写pdf文件名吗?

在此先感谢!这里是我的代码:

class Account 
    attr_reader :account_name, :account_acronym, :account_series 
    attr_accessor :account_directory 

    def initialize 
    @account_name = account_name 
    @account_series = account_series 
    @account_directory = account_directory 
    end 

    #prompts user for account name and record series so it can create the directory 
    def validation_account 
    print "What account?" 
    account_name = gets.chomp 
    print "What Record Series? " 
    account_series = gets.chomp 
    account_directory = "c:/Processed Batches Clone/" + account_name + "/" + account_series + "/Data" 
    puts account_directory 
    return account_directory 
    end 
end 

processed_batches_dir = Account.new 

#changes pwd to account directory 
Dir.chdir "#{processed_batches_dir.validation_account}" 

# pdf list 
processed_docs = [] 

# iterates through subfolders and creates list 
Dir.glob("**/*.pdf") { |file| 
    processed_docs.push(file) 
    } 

# writes list to .txt file 
File.open("processed_batches.txt","w") { |file| 
    file.puts(processed_docs) 
    } 
+0

这很有趣,关于Ruby您的评论。我已经编程了数十年,至少使用了十几种语言,我会推荐任何新的程序员以Ruby开始!这是我最喜欢的。 –

回答

0

有可能是一个更好的办法,但你总是split通路上的最后一个斜线:

Dir.glob('**/*.pdf').each do |file_with_path| 
    processed_docs.push(file_with_path.split('/').last) 
end 
相关问题