views:

26

answers:

2

Hi,

I'm trying to write a simple backup script in Ruby that copies a folder to a specific directory with a timestamp in its name. My code is (simplified):

require 'Fileutils.rb'
time = Time.now
FileUtils.cp_r "C:/somefolder", "D:/somefolder_backup_#{time}" 

But I keep getting

`fu_mkdir': Unknown error - mkdir failed (SystemCallError)

The same happens if I simply want to create a folder with the current time in it:

FileUtils.mkdir "C:/somefolder_#{time}"

It doesn't seem to be a privileges issue, if I leave out the #{time}-thing it works perfectly.

Any advices are appreciated.

+3  A: 

My guess is that there is a character in your time string that Windows doesn't allow in a directory name (your code works fine for me on my Ubuntu machine). Try formatting your time so that it's just numeric, and that'll probably work:

require 'Fileutils.rb'
time = Time.now.strftime("%Y%m%d%H%M%S")
FileUtils.cp_r "C:/somefolder", "D:/somefolder_backup_#{time}" 
Daniel Vandersluis
Duh, so obvious. As rspeicher said, the colons where the problem. Thanks, your code works perfectly!
redfalcon
+1  A: 

The string returned by Time.now has colons in it, which is an illegal character for directory names.

Use Daniel's code to format the time.

rspeicher