判断目录中是否存在文件的最佳方法是什么?

发布于 2024-08-26 04:45:15 字数 399 浏览 1 评论 0原文

我正在尝试移动文件,但我想在这样做之前确保它存在。在 Perl 中执行此操作的最简单方法是什么?

我的代码是这样的。我查找了 open 命令,但我不确定这是否是最简单的方法。

if  #Parser.exe exist in directory of Debug
{
    move ("bin/Debug/Parser.exe","Parser.exe");
}
elsif  #Parser.exe exist in directory of Release
{
    move ("bin/Release/Parser.exe","Parser.exe");
}
else
{
    die "Can't find the Parser.exe.";
}

谢谢。

I'm trying to move a file but I want to ensure that it exists before I do so. What's the simplest way to do this in Perl?

My code is like this. I looked up the open command, but I am not sure it is the simplest way or not.

if  #Parser.exe exist in directory of Debug
{
    move ("bin/Debug/Parser.exe","Parser.exe");
}
elsif  #Parser.exe exist in directory of Release
{
    move ("bin/Release/Parser.exe","Parser.exe");
}
else
{
    die "Can't find the Parser.exe.";
}

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗失的美好 2024-09-02 04:45:15

您需要的是 文件测试运算符检查文件是否存在。具体来说,您需要 -e 运算符来检查文件 e 是否存在。

if (-e "bin/Debug/Parser.exe")
{
    move ("bin/Debug/Parser.exe","Parser.exe");
}
elsif (-e "bin/Release/Parser.exe")
    move ("bin/Release/Parser.exe","Parser.exe");
else
{
    die "Can't find the Parser.exe."
}

What you need is a file test operator to check if the file exists. Specifically, you need the -e operator which checks if a file exists.

if (-e "bin/Debug/Parser.exe")
{
    move ("bin/Debug/Parser.exe","Parser.exe");
}
elsif (-e "bin/Release/Parser.exe")
    move ("bin/Release/Parser.exe","Parser.exe");
else
{
    die "Can't find the Parser.exe."
}
染柒℉ 2024-09-02 04:45:15

您可以使用 -e file test 来检查文件是否存在:

use File::Copy;

if(-e "bin/Debug/parser.exe") {
 copy("bin/Debug/parser.exe","Parser.exe") or die "Copy failed: $!";
} elsif(-e "bin/Release/Parser.exe") {
 copy("bin/Release/parser.exe","Parser.exe") or die "Copy failed: $!"; 
} else {
 die "Can't find the Parser.exe.";
}

You can make use of -e file test to check for file existence:

use File::Copy;

if(-e "bin/Debug/parser.exe") {
 copy("bin/Debug/parser.exe","Parser.exe") or die "Copy failed: $!";
} elsif(-e "bin/Release/Parser.exe") {
 copy("bin/Release/parser.exe","Parser.exe") or die "Copy failed: $!"; 
} else {
 die "Can't find the Parser.exe.";
}
只想待在家 2024-09-02 04:45:15

就我个人而言,我不喜欢这些解决方案中文件/路径名的重复 - 就我自己而言,我怀疑我可能会意外地将其更改为

  if(-e "pathone....")... { copy("pathtwo...","Parser.exe")

我会做类似的事情

   copy("bin/Debug/parser.exe","Parser.exe")   or 
   copy("bin/Release/parser.exe","Parser.exe") or  
   die "Can't find the Parser.exe.";

,或者如果这有点危险

   copy_parser("bin/Debug")   or 
   copy_parser("bin/Release") or  
   die "Can't find the Parser.exe.";

sub copy_parser {
    my $path = shift ;
    my $source = File::Spec-> catfile ( $path, 'Parser.exe' ) ; 

    if ( -e $source ) {
       copy( $source, "Parser.exe") or die "Copy or $source failed: $!";
       return 1 ;
    }
    return 0 ;
}

Personally I don't like the duplication of the file/ path name in these solutions - speaking for myself I suspect I might change accidently it to

  if(-e "pathone....")... { copy("pathtwo...","Parser.exe")

I would do something like

   copy("bin/Debug/parser.exe","Parser.exe")   or 
   copy("bin/Release/parser.exe","Parser.exe") or  
   die "Can't find the Parser.exe.";

Or if that is a bit risque

   copy_parser("bin/Debug")   or 
   copy_parser("bin/Release") or  
   die "Can't find the Parser.exe.";

sub copy_parser {
    my $path = shift ;
    my $source = File::Spec-> catfile ( $path, 'Parser.exe' ) ; 

    if ( -e $source ) {
       copy( $source, "Parser.exe") or die "Copy or $source failed: $!";
       return 1 ;
    }
    return 0 ;
}
最美的太阳 2024-09-02 04:45:15

当 Justintime 注意到重复并试图消除它时,他就走上了正确的道路。我在最小化方面比他更进一步。

不过,通过封装列表迭代来删除所有重复项是有意义的,而不是仅封装代码的复制/移动部分。

我将子例程放入模块中,以便以后可以根据需要重用。这也减少了重复代码。

use SearchMove;

my $found = search_and_move( 
    src  => 'Parser.exe',
    dest => 'Parser.exe',
    dirs => [ 
        "bin/Debug",  
        "bin/Release",
    ],
);

die "Can't find the Parser.exe\n"
    unless defined $found;

print "Found Parser.exe in $found";

在SearchMove.pm

包中SearchMove;

使用严格;
使用警告;

use Exporter 'import';

our @EXPORT_OK = qw( search_and_move );
our @EXPORT = @EXPORT_OK;    

sub search_and_move {
    my %arg = @_;

    croak "No source file"  unless exists $args{src};
    croak "No dest file"    unless exists $args{dest};
    croak "No search paths" unless exists $args{dirs};

    my $got_file;

    for my $dir ( @{$arg{dirs}} ) {

         my $source = "$dir/$arg{src}";

         if( -e $source ) {
             move( $source, $arg{dest} );
             $got_file = $dir;
             last;
         }

    }

    return $got_file;
}

1;

现在您可以在许多不同的项目中使用search_and_move

justintime is on the right track when he notes the repetition and seeks to eliminate it. I took the minimization a step farther than he did.

Rather than encapsulate only the copy/move portion of the code, though, it makes sense to remove as all the repetition by encapsulating the list iteration.

I put the subroutine in a module so it can be reused later as needed. This also reduces repeated code.

use SearchMove;

my $found = search_and_move( 
    src  => 'Parser.exe',
    dest => 'Parser.exe',
    dirs => [ 
        "bin/Debug",  
        "bin/Release",
    ],
);

die "Can't find the Parser.exe\n"
    unless defined $found;

print "Found Parser.exe in $found";

In SearchMove.pm

package SearchMove;

use strict;
use warnings;

use Exporter 'import';

our @EXPORT_OK = qw( search_and_move );
our @EXPORT = @EXPORT_OK;    

sub search_and_move {
    my %arg = @_;

    croak "No source file"  unless exists $args{src};
    croak "No dest file"    unless exists $args{dest};
    croak "No search paths" unless exists $args{dirs};

    my $got_file;

    for my $dir ( @{$arg{dirs}} ) {

         my $source = "$dir/$arg{src}";

         if( -e $source ) {
             move( $source, $arg{dest} );
             $got_file = $dir;
             last;
         }

    }

    return $got_file;
}

1;

Now you can use search_and_move in many different projects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文