在循环中追加:跳过不存在的文件

时间:2016-12-22 13:32:45

标签: gretl

此问题与:Import timeseries via loop (pot. generic)

有关

我想导入同一目录中假定的任意数量的文件,但不会连续标记,例如:file_1,file_4,file_3001

我有一个小的导入脚本,适用于类似于以下内容的连续文件:

scalar first = 1 #the first file is file_1.tsv
scalar last = 5000 #the last file
base_path = "c:/results/file_"
sprintf temp_path "%s%d.tsv",base_path,first
open @temp_path --preserve
setobs 1 1 --special-time-series    
loop for (i=first+1;i<=last;i+=1) --quiet
    sprintf temp_path "%s%d",base_path,i
    append @temp_path 
endloop

现在,当我在上面的设置中使用它时(丢失文件)我收到错误并且脚本停止:

  

无法打开......

是否有可能以某种方式继续/捕捉错误并且&#34;继续&#34; ?

1 个答案:

答案 0 :(得分:1)

我认为“catch”命令可以满足你的需要。看看下面的代码:

# Creating files
open australia.gdt
store 0.gdt PAU
store 1.gdt PUS
store 2.gdt E
store 4.gdt IAU
store 8.gdt IUS
store 10.gdt le

# Opening created files
open 0.gdt
loop i = 1..10
    catch append $i.gdt
endloop