2009-04-02 11 views
5

मैं एक निर्देशिका संरचना की प्रतिलिपि बनाने के लिए एक रूबी स्क्रिप्ट लिखना चाहता हूं, लेकिन कुछ फ़ाइल प्रकारों को बाहर कर दें। तो, यह देखते हुए निम्नलिखित निर्देशिका संरचना:रूबी में निर्देशिका संरचना की प्रतिलिपि कैसे करें, कुछ फ़ाइल एक्सटेंशन को छोड़कर

folder1 
    folder2 
    file1.txt 
    file2.txt 
    file3.cs 
    file4.html 
    folder2 
    folder3 
    file4.dll 

मैं इस संरचना की प्रतिलिपि बनाना चाहते हैं, लेकिन exlcude .txt और .cs फ़ाइलें। तो, जिसके परिणामस्वरूप निर्देशिका संरचना इस तरह दिखना चाहिए:

folder1 
    folder2 
    file4.html 
    folder2 
    folder3 
    file4.dll 

उत्तर

1

मुझे यकीन है कि क्या अपने शुरुआती बिंदु है नहीं कर रहा हूँ, या आप मैन्युअल रूप से चल रहा है, लेकिन यह सोचते हैं आप फ़ाइलों का एक संग्रह से अधिक पुनरावृत्ति कर रहे हैं द्वारा क्या मतलब है, आप बुलियन स्थिति के मूल्यांकन के आधार पर वस्तुओं को बाहर करने के लिए अस्वीकार विधि का उपयोग कर सकते हैं।

उदाहरण:

Dir.glob(File.join('.', '**', '*')).reject {|filename| File.extname(filename)== '.cs' }.each {|filename| do_copy_operation filename destination} 

इस उदाहरण में, ग्लोब (निर्देशिकाओं सहित) फ़ाइल नाम का एक गणनीय संग्रह देता है। आप उन वस्तुओं को बहिष्कृत करते हैं जिन्हें आप अस्वीकार फ़िल्टर में नहीं चाहते हैं। फिर आप एक विधि लागू करेंगे जो प्रतिलिपि बनाने के लिए एक फ़ाइल नाम और गंतव्य लेती है।

आप सरणी विधि का उपयोग कर सकते हैं? जिओ से खोज उदाहरण की तर्ज पर, अस्वीकार ब्लॉक में भी।

Dir.glob(File.join('.', '**', '*')).reject {|file| ['.cs','.txt'].include?(File.extname(file)) } 
9

आप का उपयोग मॉड्यूल मिल सका। यहां एक कोड स्निपेट है:


require "find" 

ignored_extensions = [".cs",".txt"] 

Find.find(path_to_directory) do |file| 
    # the name of the current file is in the variable file 
    # you have to test it to see if it's a dir or a file using File.directory? 
    # and you can get the extension using File.extname 

    # this skips over the .cs and .txt files 
    next if ignored_extensions.include?(File.extname(file)) 
    # insert logic to handle other type of files here 
    # if the file is a directory, you have to create on your destination dir 
    # and if it's a regular file, you just copy it. 
end 
0

शायद कुछ शैल स्क्रिप्ट का उपयोग करें?

files = `find | grep -v "\.\(txt\|cs\)$"`.split 
संबंधित मुद्दे

 संबंधित मुद्दे