首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl -将文件内容导入另一个文件

Perl -将文件内容导入另一个文件
EN

Stack Overflow用户
提问于 2011-07-15 14:18:11
回答 2查看 957关注 0票数 0

我试图生成的下面的代码试图这样做:

我要做的是运行一个BTEQ脚本,该脚本从DB中获取数据,然后导出到一个平面文件,该平面文件将获得我的Perl脚本(上面的代码),然后在本文中试图让perl导入该文件,然后将其导入一个fastload文件中。这更有意义吗?

代码语言:javascript
复制
while (true) {
    #Objective: open dir, get flat-file which was exported from bteq
    opendir (DIR, "C:/q2refresh/") or die "Cannot open /my/dir: $!\n"; #open directory with the flat-file
    my @Dircontent = readdir DIR;
    $filetobecopied = "C:/q2refresh/q2_refresh_prod_export.txt";  #flatfile exported from bteq
    $newfile = "C:/q2refresh/Q2_FastLoadFromFlatFile.txt"; #new file flat-file contents will be copied to as "fastload"
    copy($filetobecopied, $newfile) or die "File cannot be copied.";
    close DIR;
    my $items_in_dir = @Dircontent;
        if ($items_in_dir > 2) {  # > 2 because of "." and ".."
-->>>>>>      # take the copied FlatFile above and import into a fastload script  located at C:/q2refresh/q2Fastload.txt
        }
        else {sleep 100;}
}

我需要执行上述粗体部分的帮助。如何将C:/q2refresh/Q2_FastLoadFromFlatFile.txt的内容导入位于C:/q2refresh/q2Fastload.txt的fastload脚本。

//如果这有点新意,我很抱歉,但我对Perl并不熟悉。

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-15 15:20:44

代码语言:javascript
复制
if ($items_in_dir > 2) {  # > 2 because of "." and ".."

当包含...,再加上两个q2_refresh_prod_export.txt副本时,目录中总是有两个以上的文件。如果出现这样的情况,即q2_refresh_prod_export.txt未被复制,则脚本将die。因此,else子句永远不会被调用。

而且,如果您打算在一秒钟内将文件复制到另一个地方,那么将文件复制到一个新位置是没有意义的。它不像“剪切和粘贴”,你实际上,物理复制文件到一个新的文件,而不是剪贴板。

如果“导入”意味着要将q2_refresh_prod_export.txt的内容附加到现有的q2Fastload.txt中,那么有一些方法可以做到这一点,比如Troy在另一个答案中建议使用open>> (附加到)。

您必须弄清楚整个$items_in_dir条件是什么意思。您将文件和复制文件保存在该目录中,那么您要检查的到底是什么?文件是否全部被删除(通过其他进程)?

票数 0
EN

Stack Overflow用户

发布于 2011-07-15 14:39:36

我不知道你想做什么。你就想这么做吗?

代码语言:javascript
复制
open SOURCE, $newfile;
open SINK, '>>C:/q2refresh/q2Fastload.txt';
while (<SOURCE>) {
    print SINK $_;
}
close SOURCE;
close SINK;

这将将$newfile的内容附加到您的fastload文件中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6708407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档