2012-08-01 40 views
3

這工作:如何在Erlang的目錄中複製文件?

1> file:copy(test.html, test1.html). 
{ok,2384} 

但這並不:

2> file:copy(test.html, sites/test.html). 
    ** exception error: bad argument in an arithmetic expression 
    in operator '/'/2 
    called as sites/'test.html' 

我怎樣才能在二郎跨目錄中的文件複製?

非常感謝,

LRP

回答

0

一些大的文件會導致問題,當你複製/二郎內移動它們。使用os:cmd/1有時更安全。像這樣:

 
move(Source, Destination)-> 
    %% For Windows 
    Command = "MOVE \"" ++ Source ++ "\" \"" ++ Destination ++ "\"", 
    %% For Unix/Linux 
    %%Command = "mv \"" ++ Source ++ "\" \"" ++ Destination ++ "\"", 
    spawn(os,cmd,[Command]).
copy(Source, Destination)-> %% For Windows Command = "XCOPY \"" ++ Source ++ "\" \"" ++ Destination ++ "\"", %% For Unix/Linux %%Command = "cp \"" ++ Source ++ "\" \"" ++ Destination ++ "\"", spawn(os,cmd,[Command]).

+0

並不是說'Source'和'Destination'變量可能包含'正斜槓'或'反斜槓',這取決於系統是運行windows還是unix-linux – 2012-08-06 15:00:31

14

的問題是,sites/test.html有特殊字符,必須是單引號內。嘗試:

file:copy(test.html, 'sites/test.html'). 

或者你也可以使用字符串:

file:copy("test.html", "sites/test.html"). 
+0

謝謝Isac! – 2012-08-02 15:28:35

相關問題