【问题标题】:Creating folder using perl使用 perl 创建文件夹
【发布时间】:2011-08-23 00:27:00
【问题描述】:

我想使用 perl 创建文件夹,在同一个文件夹中,存在一个 perl 脚本。我创建了FolderCreator.pl,它需要folder name 的输入参数。

unless(chdir($ARGV[0]){ # If the dir available change current , unless
    mkdir($ARGV[0], 0700);                             # Create a directory
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";    # Then change or stop
}

这只有在我们调用 scipt 时才能正常工作,在它所在的同一文件夹中。如果在另一个文件夹中调用,如果不起作用。

例如。

.../Scripts/ScriptContainFolder> perl FolderCreator.pl New
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl New

第一个工作正常,但第二个没有。有没有办法创建这些文件夹?

【问题讨论】:

  • 顺便说一句,我试过 $path = substr(abs_path($0), 0, index(abs_path($0),'FolderCreator.pl') ); $pwd = pwd;印章($pwd); $path = substr($path, length($pwd));获取相对路径并尝试 mkpath($path.$ARGV[0]) 但它给出错误“mkdir /Darshana: Permission denied at ./Darshana/scripts/FolderCreator.pl line 25”
  • 我的脚本正在生成一些文本文件。所以我想将它们组织到文件夹中。当我想创建这些文本文件时,我创建文件夹然后将文件夹更改为它并创建文本文件。所以 FolderCreator.pl MyDir 将创建 MyDir 文件夹并在其中创建那些文本文件。

标签: perl mkdir


【解决方案1】:

我已经完成了工作,这里是代码...谢谢大家的帮助...

#!usr/bin/perl

###########################################################################################
# Needed variables
use File::Path;
use Cwd 'abs_path';
my $folName     = $ARGV[0];
#############################################################################################
# Flow Sequence 
if(length($folName) > 0){
    # changing current directory to the script resides dir
    $path = abs_path($0);
    $path = substr($path, 0, index($path,'FolderCreator.pl') );
    $pwd = `pwd`;
    chop($pwd);
    $index = index($path,$pwd);
    if( index($path,$pwd) == 0 ) {
        $length = length($pwd);
        $path = substr($path, $length+1);

        $index = index($path,'/');
        while( $index != -1){
            $nxtfol = substr($path, 0, $index);
            chdir($nxtfol) or die "Unable to change dir : $nxtfol"; 
            $path = substr($path, $index+1);
            $index = index($path,'/');
        } 
    }
    # dir changing done...

    # creation of dir starts here
    unless(chdir($folName)){        # If the dir available change current , unless
        mkdir("$USER_ID", 0700);    # Create a directory
        chdir($folName) or $succode = 3;    # Then change or stop
    }
}
else {
    print "Usage : <FOLDER_NAME>\n";    
}
exit 0;

【讨论】:

    【解决方案2】:

    我认为它的工作原理与它所写的完全一样,除了你有一个错字,即缺少chdir 周围的右括号。

    unless(chdir($ARGV[0])) {   #fixed typo
        mkdir($ARGV[0], 0700);
        chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
    }
    

    脚本运行如下:

    1. 如果脚本无法 chdir 到 $ARGV[0] 那么:
    2. 创建目录 $ARGV[0],使用 权限掩码 0700。
    3. 将工作目录更改为 $ARGV[0] 或退出脚本 错误文本“无法 chdir..”。

    脚本的起始目录将是调用它的目录,无论该目录是什么。在 *nix 上,这将是脚本中的 $ENV{PWD} 变量。它会在它有权这样做的任何文件夹中创建一个新文件夹。

    我认为这种行为是合乎逻辑的,而且应该如此。如果您希望您的示例起作用,请执行以下操作:

    .../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New
    

    也可以使用绝对路径,比如

    ?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New
    

    ETA:哦,你当然应该总是把它放在你的脚本中,不管它有多小:

    use strict;
    use warnings;
    

    【讨论】:

      【解决方案3】:

      您可以使用FindBin 模块,它为我们提供了$Bin 变量。它定位脚本 bin 目录的完整路径,以允许使用相对于 bin 目录的路径。

      use FindBin qw($Bin);
      
      my $folder = "$Bin/$ARGV[0]";
      
      mkdir($folder, 0700) unless(-d $folder );
      chdir($folder) or die "can't chdir $folder\n";
      

      【讨论】:

        猜你喜欢
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 2012-03-02
        • 2010-10-09
        • 2017-07-14
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多