检查具有特定扩展名的文件目录

[英]Checking directory for file with certain extension


I am trying to check whether a directory entered through the command line contains files with a certain file extension. For example, if I have a folder "Folder1" with another folder in it "Folder 2" and Folder2 contains several files, "test.asm", "test.vm", "test.tst". I am taking either a directory or a file through the command line like this

我正在检查通过命令行输入的目录是否包含具有特定文件扩展名的文件。例如,如果我有一个文件夹“Folder1”,其中包含另一个文件夹,“文件夹2”和Folder2包含几个文件,“test”。asm”、“测试。vm”、“test.tst”。我通过命令行获取一个目录或一个文件,如下所示

ruby translator.rb Folder1/Folder2

or

ruby translator.rb Folder1/Folder2/test.vm

What I'm trying to do is error checking. I already have checks for whether the input is a folder and now I need to check whether the folder actually contains a .vm file. What I've done so far is this:

我要做的是错误检查。我已经检查了输入是否为文件夹,现在需要检查该文件夹是否实际包含.vm文件。到目前为止,我所做的是:

require 'pathname'

pn = Pathname.new(ARGV[0])

if ARGV.size != 1
    puts "Proper usage is: ruby vmtranslator.rb file_directory\file.vm \nOR \nruby vmtranslator.rb file_directory\ where file_directory has multiple vm files test".split("\n")
elsif !pn.exist? && !pn.directory?
    puts "Something is wrong with the file"
    puts "Either try another file or check the file extension"
elsif pn.directory? && pn.children(false).extname.include?('.vm')
    puts "this should print if Folder1 is the folder, but not if Folder2 is.."
    vm_file1 = File.open("OPEN FILES WITH .vm AS EXTENSION)
elsif pn.exist? || pn.file?
    puts "this is right"
    vm_file = File.open(ARGV[0], "r")
    asm_file = File.new(ARGV[0].sub('.vm', '.asm'), "w")
end

So what that should do is check whether there is only 1 argument first, if so, then it checks if it's a file or directory else it outputs an error, then what I'm doing is checking if it's a directory. If so, I need to check if the directory actually contains .vm files. I tried pn.each_child {|f| f.extname == '.vm'} but that only checks the first value before it returns true. Is there any easier way to check the whole array before returning true, other than just setting some boolean?

所以这应该做的是首先检查是否只有一个参数,如果是,然后它检查它是一个文件还是一个目录它输出一个错误,然后我要做的就是检查它是否是一个目录。如果是的话,我需要检查这个目录是否包含。vm文件。我试着pn。each_child { | | f。extname = =”。但这只在第一个值返回true之前检查它。是否有更简单的方法在返回true之前检查整个数组,而不是只设置一些布尔值?

Some of the code up there isn't done, I'm just asking if there is any way to check a directory for a file of a certain extension. I can't find anything with my searches so far.

上面的一些代码还没有完成,我只是在问是否有任何方法可以检查目录中的某个扩展名文件。到目前为止我没有找到任何线索。

3 个解决方案

#1


4  

str = ARGV[0]

proc = ->(f) { puts "doing something with #{f.path}" }

if Dir.exists?(str)
  Dir.glob(File.join(str, File.join('**', '*.vm'))).each do |entry|
    proc[File.open(entry)]
  end
elsif File.exists?(str) && File.extname(str) == '.vm'
  proc[File.open(str)]
else
  puts "couldn't do anything with #{str}"
end

#2


1  

Dir["Folder1/Folder2/*.vm"].empty?

will return false if there are any .vm files in Folder1/Folder2.

如果Folder1/Folder2中有任何.vm文件,将返回false。

#3


1  

require 'pathname'

def directory_has_vm_files?(path)
  Dir.glob(path.join('*.vm')).size > 0
end

unless ARGV[0]
  puts %{
    Proper usage is:

    ruby vmtranslator.rb file_directory or file.vm

    OR

    ruby vmtranslator.rb file_directory
    where file_directory has multiple vm files
  }
else
  path = Pathname.new(ARGV[0])

  if path.exist?
    if path.file?
      if File.extname(path) == '.vm'
        puts "Valid VM file"
      else
        puts "Not a VM file"
      end
    else
      if directory_has_vm_files?(path)
        puts "Valid Directory - contains vm files"
      else
        puts "#{path} does not contain any VM file"
      end
    end
  else
    puts "Invalid path"
  end
end
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2012/10/18/54d0918339b450f89f979ac74886155a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告