Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File & Directory Copy

Status
Not open for further replies.

geokor

Programmer
Aug 16, 2000
126
0
0
US
I'm trying to write a short script that can run under windows (linux later) that will automate my backuping (if that's a word).

I can copy a file but how do I copy a directory AND all the files it contains? Can this be done? Thanks.

George K
 
Hi George,

Nice to see somebody in the Ruby forum. If I understand what you asking, you want to be able to say to ruby: copy this directory, and everything beneath it recursively. This would include all subdirectories, and all their subdirectories, and so on.

I don't think Ruby (or any other language) has a special function just for that. In unix I think you can say:

cp -r /path/to/source_direcory/ /path/to/destination_dir

There may need to be a (*)asterisk after the "source_directory/", and the -r may need to be -R, test it out.

I'm sitting at a Windows ME machine at home and I can't even get a command line. So I don't know what to do on windows/DOS. Although it may be a /s parameter on the end of the copy command:

copy C:\path\from\here C:\path\to\there /s

I'm not sure though.

You could write a recursive function in Ruby to precipitate through the directories and do everything itself, but it would definitely be easier to take advantage of the operating system's capabilities if they are there.

I'm working on getting Linux installed, and once I do, I can write some code and test out my answers. I've not written much in Ruby since work has kept me busy with things other than programming :( So I'm sure that whatever answer I gave would be wrong there.

Talk to ya soon.

--jim
 
Thanks Jim, yeah I know, I run Linux RH 7.2 as my home LAN server and my main box has 2 drives - win98 & SuSE 8.0. On Linux its not a big deal. But under Windoze its fun. In PHP I can use

$today = getdate();
$dirc="C:\\bkp".$today['year'].$today['month'].$today['mday'];
mkdir("$dirc", 0700);

This creates a directory called bkp2002Auguest24 and copies my sourcefile into it as targetfile.

@copy ("C:\sourcedir\sourcefile", "$dirc"."\targetfile");

But I can't find anything under Ruby for copying a directory or a file. I'll keep looking!

George K


...men! When they are learned they think they are wise!...

 
Code:
#creates dir
dirc = "C:\\bkp" + Time.now.strftime("%Y%B%d")
Dir.mkdir( dirc , 0700 )

#opens/creates files
source = File.open("C:/sourcedir/sourcefile")
target = File.open(dirc + "targetfile" , "w")

#copies the source to target, 64 bytes at a time
target.write( source.read(64) ) while not source.eof?
  
#closes files
source.close
target.close
I think this will work, but it's untested.
When you go to windows, you may have to use the binmode method on your file objects before you read/write unless it's just a text file.

Hope this works! Let me know...

--jim
 
It looks like you've got it worked out with Ruby. That's great!

Just so you know, here's how you would want to do it with DOS;

xcopy C:\path\from\here C:\path\to\there /s /e


Another way you could do it is to zip all the files including directories and then move the zip file to the backup place.

tgus

____________________________
Families can be together forever...
 
Thanks Jim & tgus, I havent been on tektips for a few weeks but I'll try these ideas in the next few days and let you know.

BTW tgus, do you recogonize my signature? I like yours.

George K
 
Hi George,

Another good one is;

Hold fast to the iron rod . . .

I might change mine to that some day if I can ever get the 'Personal Profile' link to work for me again.




tgus

____________________________
Families can be together forever...
 
Have you looked at the extension module ftools. This includes copy, cp & syscopy.

This in a Win download of Ruby 1.67 from dev.rubycentral.com

The copy is only of one file so you still have to recursively walk the directory tree but at least the file copy is a one-liner.
 
This works with 1.8, perhaps earlier versions too:

Code:
  require 'fileutils'
  include FileUtils

  # Recursively copy the first directory into the second
  cp_r "/path/to/source", "/path/to/dest"

--
Michael Granger <ged at FaerieMUD dot org>
Rubymage, Architect, Believer
The FaerieMUD Consortium <
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top