Search code examples
rubyarraysfiledirectorymove

scanned files on array, would like to move them all into a folder with one code block


    class Desktop

      # grab the names of items on the desktop
      desktop_content = Dir.entries("/Users/jorgeolivero/desktop")

      def initialize(desktop_content)
        @desktop_content = desktop_content
      end

      def initlialize(select)
        arr.select { |x| x.include? "y"}
      end

      def initialize(desktop_files)
        @desktop_files = desktop_files
      end

      # grabs the files from the desktop_content array
      desktop_files = desktop_content.select { |c| c.include? '.'}

      puts desktop_files

      desktop_files.each { |files| File.rename('/Users/jorgeolivero/desktop/***feed array items here***', '/Users/jorgeolivero/desktop/folder/***feed array items here***') }

    end

My guts tells me that this might look painful to some of you, so please know this is my first attempt at an app. So if you can remember that process, you understand how "stuck" I am right now.

I want to move all of the items in the "desktop_files" array into a folder with one command. That last piece of code shows how I am trying to do but I can't figure out how to feed each file name onto the end of each path (as denoted by the ***s).

Many thanks.


Solution

  • You can use the following code for copy all your files:

    require 'fileutils'
    
    FileUtils.mv Dir['/Users/jorgeolivero/desktop/*'], 'dest folder'
    

    You don't need to create a class for it. You can run this code on irb or put in a single file.

    PS: Ruby doesn't accept multiple initializers for a class.