2013-03-22 29 views
0

我想在Ruby中的文件夾複製,而是在接收錯誤「無法目錄複製到其自身」如何將一個文件夾複製其在紅寶石內容

我不明白爲什麼會這樣,可有人請解釋一下嗎?我怎樣才能實現它複製一個完整的文件夾與新名稱。這裏是我的代碼,我嘗試在其名稱中使用時間戳複製源文件夾。你[R仍在使用,而不是frontedSubPath

$releaseFolder = 'D:\ruby_workspace\fpsupdater\release' 
$frontendSubPath = '\server\pdixfrontend\tomcat\conf' 
$backendSubPath = '\server\pdixbackend\tomcat\conf\Catalina' 


def println(text) 
    puts $timestamp.inspect + ': ' + text 
end 


def getFileTimestamp() 
stamp = $timestamp.inspect 
stamp.gsub!(/\s+|:|\++/,"_") 
end 

def backupContext() 

target = @configHash["target"] 

if (Dir.exists?(target)) 

    println("backup of target " + target) 
    println("need to backup: " + target + $frontendSubPath) 
    println("need to backup: " + target + $backendSubPath) 
    source = "#{target}#{$frontendSubPath}" 
    backup = "#{target}#{$frontendSubPath}\\" + getFileTimestamp + "_conf" 
    println(source) 
    println(backup) 

    FileUtils.cp_r source , backup 

else 
    puts "cannot backup because target does not exists" 
end 
end 

回答

2

無法目錄複製到同一目錄下,例如:

FileUtils.cp_r "C:/TEST", "C:/TEST" 

你甚至不能使一個遞歸複製到您要複製到的目錄,因爲那樣您可能會以永無止境的循環結束!

FileUtils.cp_r "C:/TEST", "C:/TEST/SUBFOLDER" 

如果您需要這樣做,請使用臨時目錄,然後將其移回。還優化您的代碼,getFileTimestamp可以更好:-)

+0

謝謝,我現在把它複製到一個tmp文件夾並將其移回。這工作。你對時間戳有什麼建議,我是新來的紅寶石,並建議打開:-) – 2013-03-22 09:16:11

+0

如果時間戳是一個對象,嘗試調用一個方法,它會以你需要的形式返回它。您始終可以轉換爲時間格式,然後重新格式化它。祝你好運! – lzap 2013-03-22 09:20:43

0
backup = "#{target}#{$frontendSubPath}\\" + getFileTimestamp + "_conf" 

通知backendSubPath

+0

多數民衆贊成在正確的,我想直接複製文件夾在相同的文件夾中,因爲它是,但與一個新的文件夾名稱 – 2013-03-22 09:10:33

相關問題