Perl目录步行问题 - 无法正确地备份多于1个目录 [英] Perl Directory Walking issue - Can't go back up more than 1 directory properly

查看:142
本文介绍了Perl目录步行问题 - 无法正确地备份多于1个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我没有能力使用File :: Find。

First off, I don't have the ability to use File::Find.

所以我有我的脚本走过目录,找到某种类型的文件。但是,如果我深入多个子目录,我的脚本不能正常退出所有的方式备份到起始目录。我想我需要一个$ previousDir变量来跟踪最后一个目录,所以当我在子目录中完成后,我可以说回去。但是我已经尝试把它放在多个地方没有成功...

So I have my script to walk through directories and find a certain type of file. But if I go more than one sub-directory deep, my script doesn't properly exit all the way back up to the starting directory. I think I need to have a $previousDir variable that keeps track of the last directory so I can say to go back out to that one when I'm done in the sub-directory. But I've tried putting it in multiple places without success...

文件结构( BOLD 是Dir, Italic 是一个文件):

File Structure (BOLD is a Dir, Italic is a file):

startingdirectory / Logs - AAA Dir1 zzz adstatlog.299,adstatlog.tgz,file

startingdirectory/Logs - AAA, Dir1, zzz, adstatlog.299, adstatlog.tgz, file

/ AAA - / em>

/AAA - filefile

/ Dir1 - / Dir2 config.tar.gz

/Dir1 - /Dir2, config.tar.gz

/ Dir2 - EMPTY

/Dir2 - EMPTY

/ zzz - inzzz

这是我目前的脚本:

# specify the directory where you want to start the search
my $startingDir = $ARGV[0];
my $directoryCount = 0;
my $directory = shift;
my $previousDir;
my @directories;
my $tarOutput;

# Calling the Subroutine, which searches the Directory
readDirectory($startingDir);

sub readDirectory
{
    # Open and close the startingDir
    opendir(DIR, @_[0]) or die("ERROR: Couldn't open specified directory $!");
    my @files = grep { $_ !~ /^\.{1,2}$/ } readdir DIR;
    closedir DIR;

    print "------------------------------------------------------------------------\n\n";

    foreach my $currentFile (@files)
    {   
        print "Current File: ", $currentFile, "\n\n";

        #Directory currently searching through
        print "Searching in $directory\n\n";

        my $fullPath = "$directory/$currentFile";
        print "FULL PATH: $fullPath\n\n";
        if ( -d $fullPath )
        {
            print "Found New Directory: ", $currentFile, "\n\n";
            push (@directories, $currentFile);
            $directoryCount++;
            print "Current number = $directoryCount\n\n";
            print "Directories: @directories \n\n";
            $previousDir = $directory;
            $directory = $fullPath;
            # The Subroutine is calling hisself with the new parameters
            readDirectory($directory);
        }

        elsif ( $currentFile =~ /\.tar.gz$/i || $currentFile =~ /\.tar$/i || $currentFile =~ /\.tgz$/i)
        {
            print "File: ", $currentFile, "\n\n";
            my $tarOutput = `tar -tvzf $currentFile`;
            print $tarOutput, "\n";
            $previousDir = $directory;
        }

        print "PREVIOUSDIR: $previousDir\n\n";

        print "-----------------------------------------------------------------------\n\n";

        $directory = $previousDir;
    }   
}

输出:(向下滚动查看问题开始)

And the output: (scroll down to see where issue begins)

------------------------------------------------------------------------

Current File: AAA

Searching in /home/gackerma/Logs

FULL PATH: /home/gackerma/Logs/AAA

Found New Directory: AAA

Current number = 1

Directories: AAA

------------------------------------------------------------------------

Current File: filefile

Searching in /home/gackerma/Logs/AAA

FULL PATH: /home/gackerma/Logs/AAA/filefile

PREVIOUSDIR: /home/gackerma/Logs

 ------------------------------------------------------------------

 PREVIOUSDIR: /home/gackerma/Logs

 ------------------------------------------------------------------

Current File: Dir1

Searching in /home/gackerma/Logs

FULL PATH: /home/gackerma/Logs/Dir1

Found New Directory: Dir1

Current number = 2

Directories: AAA Dir1

------------------------------------------------------------------------

Current File: DIR2

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/DIR2

Found New Directory: DIR2

Current number = 3

Directories: AAA Dir1 DIR2

------------------------------------------------------------------------

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------

Current File: configs.tar.gz

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/configs.tar.gz

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------

PREVIOUSDIR: /home/gackerma/Logs/Dir1  ***THIS IS WHERE THE ISSUE STARTS - 
                                          PREVIOUSDIR SHOULD BE /Logs!!***

------------------------------------------------------------------

Current File: file

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/file

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------

Current File: adstatlog.299

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/adstatlog.299

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------

Current File: zzz

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/zzz

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------

Current File: adstatlog.tgz

Searching in /home/gackerma/Logs/Dir1

FULL PATH: /home/gackerma/Logs/Dir1/adstatlog.tgz

PREVIOUSDIR: /home/gackerma/Logs/Dir1

------------------------------------------------------------------


推荐答案

如果可以,我真的会使用 File :: Find

I would really use File::Find if you can.

以下是递归尝试的工作,简化版本:

Here's a working, simplified version of your recursive try:

use warnings;
use strict;

die "Usage: $0 (abs path to dir) " if @ARGV != 1;

my $dir = shift @ARGV;

file_find($dir);

sub file_find {
    my $dir = shift;

    opendir my $dh, $dir or warn "$dir: $!";
    my @files = grep { $_ !~ /^\.{1,2}$/ } readdir $dh;
    closedir $dh;

    for my $file ( @files ) { 
        my $path = "$dir/$file";

        if( $path =~ /(\.tar\.gz|\.tar|\.tgz)$/ ) { 
            print "do tar for $path\n";
        }   
        file_find($path) if -d $path;
    }   
}

这篇关于Perl目录步行问题 - 无法正确地备份多于1个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆