2016-04-19 7 views
0

Rでfile.copyコマンドを使用していますが、エラーがスローされますが、原因を特定できません。file.cでRが動作していない

file.copy(from="Z:/Ongoing/Test", to = "C:/Users/Darius/Desktop", overwrite = TRUE, recursive = TRUE) 

Warning message: 
In file.copy(from = "Z:/Ongoing/Test",: 
problem copying Z:/Ongoing/Test to C:/Users/Darius/Desktop/Test: No such file or directory 

誰でも問題を見ることができますか?コマンドラインは、警告メッセージだけを表示しても機能しません。

+0

さて、あなたは、ソースパスが有効であることを確認していますか?ファイルは存在しますか? – michel404

+0

'Z:/ Ongoing/Test'はファイルまたはディレクトリですか? "test"というファイルとディレクトリはありますか? –

+0

list.files( "Z:/ Ongoing /" return?) "test"という名前のファイルがありますか? "test.doc"または "Test.xlsx"というファイルが存在する可能性があります。 –

答えて

4

実際、私はディレクトリをコピーするための簡単な方法はないと思います。私はあなたを助けるかもしれない機能を書いた。ディレクトリは

仮定をコピーする先の場所:から

この関数は、入力二つの引数を取り

をコピーするディレクトリの完全パスからと1つのディレクトリだけのパスです。

dir.copy <- function(from, to){ 

    ## check if from and to directories are valid 
    if (!dir.exists(from)){ 
    cat('from: No such Directory\n') 
    return (FALSE) 
    } 
    else if (!dir.exists(to)){ 
    cat('to: No such Directory\n') 
    return (FALSE) 
    } 

    ## extract the directory name from 'from' 
    split_ans <- unlist(strsplit(from,'/')) 

    dir_name <- split_ans[length(split_ans)] 

    new_to <- paste(to,dir_name,sep='/') 

    ## create the directory in 'to' 
    dir.create(new_to) 

    ## copy all files in 'to' 
    file_inside <- list.files(from,full.names = T) 

    file.copy(from = file_inside,to=new_to) 

    ## copy all subdirectories 
    dir_inside <- list.dirs(path=from,recursive = F) 

    if (length(dir_inside) > 0){ 
    for (dir_name in dir_inside) 
     dir.copy(dir_name,new_to) 
    } 

    return (TRUE) 
} 
+0

これはファイルではなく、 "test"という名前のフォルダ全体です。私が探しているのは、フォルダ全体とファイルを新しい場所にコピーすることです。 – Darius

+0

@Stibu質問が投稿された瞬間、質問は明確ではありませんでしたが、今では要件を満たすソリューションを編集しました。 –

+1

ありがとうございます@KunalPuri。それはすばらしい答えでした。 – Darius