2017-09-01 38 views
0

我有一个阅读excel电子表格的脚本。每个文件在被打开之前被复制到一个新的位置,确保文件中没有其他人同时处于该位置。奇怪的是,有时我的脚本因为文件不能在新的位置找到而中断。Ruby - 如何等待文件在下一个动作之前完成移动?

# copy the file to a new destination 
# the file is originating on a windows file server, 
# the destination is my local downloads folder on a windows machine. 
FileUtils.cp(some_filepath, destination_path) 
# the next line gives an error, because the file doesnt exist at its location yet 
Spreadsheet.open(file_at_new_destination) 

我试图做一个until块,做一次检查,看看是否脚本继续之前该文件存在,但即使不工作,只是一直运行。

# check if file exists 
until File.exist?(file_at_new_destination) do 
    print '.' 
end 

如何确保脚本不会在文件移动完成后继续运行?

这里是我的错误,在我的耙子任务

rake aborted! 
Errno::ENOENT: No such file or directory @ rb_sysopen - 
C:\Users\ALilland\Downloads\tmp_dest\102035 Western Digital Job Status Reports.xls 
+0

我认为命名约定是在这里提出问题。你可以检查控制台File.exist?(路径)?看看我们是否需要逃离一些特殊的字符,如空间或:或反斜杠。 – Mohanraj

回答

1

红宝石显示出来是单线程的,你不必等待文件被复制到目标路径。

使用cp_r改为cp复制包含所有子目录的目录时。

# copy directory to destination directory recursively.  
FileUtils.cp_r(some_filepath, destination_path) 
if File.exist?(file_at_new_destination) 
Spreadsheet.open(file_at_new_destination) 
end 
相关问题