Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking directory for file with certain extension
    primarykey
    data
    text
    <p>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</p> <pre><code>ruby translator.rb Folder1/Folder2 </code></pre> <p>or</p> <pre><code>ruby translator.rb Folder1/Folder2/test.vm </code></pre> <p>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:</p> <pre><code>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? &amp;&amp; !pn.directory? puts "Something is wrong with the file" puts "Either try another file or check the file extension" elsif pn.directory? &amp;&amp; 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 </code></pre> <p>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 <code>pn.each_child {|f| f.extname == '.vm'}</code> 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?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload