Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying a file from one directory to another with Ruby

Tags:

ruby

dir

Hey I'm trying to move multiple files from one folder to another. In the FileUtils line I am trying to search through all of the 4 character folders in the destination folder and then paste the file in the folder with the same base name as the file.

#!/usr/bin/env ruby  require 'fileutils'  my_dir = Dir["C:/Documents and Settings/user/Desktop/originalfiles/*.doc"] my_dir.each do |filename|    FileUtils.cp(filename, "C:/Documents and Settings/user/Desktop/destinationfolder/****/" + File.basename(filename, ".doc")) end 
like image 814
1dolinski Avatar asked Mar 01 '12 16:03

1dolinski


People also ask

How do you move a file in Ruby?

You don't need fileutils or a systemcall, just rename the file to the new location. File rename won't work across partitions, and throws the error "Invalid cross-device link". FileUtils is a better choice in those cases, but for a simple move in the same partition, rename works.

How do I copy a file from one directory to another in asp net?

YES it's possible, the only concern that you have to watch for is that the CopyTo path should be the full path, not the relative one (ex: c:\websites\myOtherFolder). this way, you can successfully copy/move the file from your ASP.NET code.

How do I copy files from one directory to another in R?

copy() function as shown in the following R syntax. Within the file. copy function, we have to specify the directory path and file names of the first folder from which we want to copy the files, as well as the directory path and file names of the second folder to which we want to copy the files.


1 Answers

Something like this should work.

my_dir = Dir["C:/Documents and Settings/user/Desktop/originalfiles/*.doc"] my_dir.each do |filename|   name = File.basename('filename', '.doc')[0,4]   dest_folder = "C:/Documents and Settings/user/Desktop/destinationfolder/#{name}/"   FileUtils.cp(filename, dest_folder) end 

You have to actually specify the destination folder, I don't think you can use wildcards.

like image 171
David Grayson Avatar answered Sep 22 '22 21:09

David Grayson