【发布时间】:2012-10-21 21:56:37
【问题描述】:
我正在将数据提取到某个文本文件,但首先我希望我的脚本检查文件是否存在,然后在某个文件夹中创建副本。如果它仍然存在于同一个文件夹中,请保存它,但根据最后一个文件的值附加值 _1 或 _2 ...。
这是我目前的脚本,
if (-e "/tmp/POP_Airtime_Week1.txt"){
copy("/tmp/POP_Airtime_Week1.txt","/tmp/POP") || die "cannot copy file";
# If the file exists create a copy in /tmp/POP
#################################
# IF FILE EXISTS IN /tmp/POP copy the file but rename it to
# POP_Airtime_Week1_1.txt then increase the numbers each time
# the script is run and a new copy needs to be created.
##################################
unlink ("/tmp/POP_Airtime_Week1.txt");
}
如果/tmp/POP/POP_Airtime_Week1.txt 存在,则将其复制但另存为/tmp/POP/POP_Airtime_Week1_1.txt。下次我运行脚本并且/tmp/POP/POP_Airtime_Week1.txt 存在时,将其复制并保存为/tmp/POP/POP_Airtime_Week1_2.txt 等...
我该怎么做?
【问题讨论】:
-
缺少的只是一个循环和一个计数器。
-
为什么不使用现有的工具,比如 logrotate?
-
不应该有“use File::Copy;”吗?在代码的开头?
标签: perl file-exists file-copying