如何在Erlang中跨目录复制文件?

时间:2012-08-01 20:51:04

标签: file erlang copy

这有效:

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'

如何在Erlang中跨目录复制文件?

非常感谢,

LRP

2 个答案:

答案 0 :(得分:14)

问题是sites/test.html有特殊字符,必须在单引号内。尝试:

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

或者您可以使用字符串:

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

答案 1 :(得分:0)

在erlang中复制/移动它们时,某些大文件会导致问题。使用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]).