Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:18:58+00:00 2026-06-13T02:18:58+00:00

I am trying to check whether a directory entered through the command line contains

  • 0

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

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:

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?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:19:00+00:00Added an answer on June 13, 2026 at 2:19 am
    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to check whether an NSInteger is odd or even. I
I am trying to check whether an xml config file exists. The file has
I am trying to check whether a certain domain is live or not. My
I am trying to check whether a list contains a value, which is user
Possible Duplicate: Check whether a Directory Exists in PHP I am trying scandir() to
I'm trying to check whether an html element with a certain id exists before
I'm trying to check whether the entered chars are digits or not with pattern
I am trying to check whether the users have a cookie stored in their
I'm trying to check whether a string contains a substring in C like: char
can anyone help me in trying to check whether JavaScript is enabled in client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.