Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing unique pathnames

Status
Not open for further replies.

ricksj

Technical User
Sep 2, 2009
3
0
0
I'm processing hundreds of files in a directory structure that looks similar to this:

./gadget/path1
{contains several files...}
./gadget/path2
{contains several files...}
./gadget/path3
{contains several files...}

What I would like to do is print to the console progress by displaying the end path name once and only change when the path changes, such as:
Processing ./path1
Processing ./path2...

Can someone recommend a fix or an alternative method
Here's what I have coded so far;

Code:
#!/usr/bin/perl -w
use warnings;
use strict;
use File::Find;
use File::Copy;
 
my $prevdir;
my $path = $ARGV[0];
chdir($path) or die "Cannot chdir to $path";

find(\&CopyContent, $path);

sub CopyContent
{
  my $file = $File::Find::name;
  my $dir = $File::Find::dir;
  #check if file is a file (not a directory) and a Text file
  if (-f $file && -T $file)
  {
    $dir =~ s/$path//g;
    my $currdir = $dir;
    if ($currdir ne $prevdir) {
      print "Processing directory: \.$currdir \n";
      my $prevdir = $currdir;
    }
    ### manipulate file code goes here....
  }
}
 
Code:
#!/usr/bin/perl -w
use warnings;
use strict;
use File::Find;
use File::Copy;
 
my $prevdir;
my $path = $ARGV[0];
chdir($path) or die "Cannot chdir to $path";

find(\&CopyContent, $path);

sub CopyContent
{
  my $file = $File::Find::name;
  my $dir = $File::Find::dir;
  #check if file is a file (not a directory) and a Text file
  if (-f $file && -T $file)
  {
    $dir =~ s/$path//g;
    my $currdir = $dir;
    [fuchsia]if ($currdir ne $prevdir)[/fuchsia] [blue]{
      print "Processing directory: \.$currdir \n";[/blue]
      [red]my $prevdir = $currdir;[/red]
    [blue]}[/blue]
    ### manipulate file code goes here....
  }
}

The red line makes $prevdir be local only inside that block (blue). So, the if line (magenta) where it checks $prevdir... $prevdir doesn't exist at that point in the code, it's created 2 lines below and only exists inside that block and is destroyed after the block ends.

You'll wanna define $prevdir somewhere else where its available where you want it and its value won't be reset every time you want it (probably near the very start of the subroutine).

Anyway, -f tests if a file is a regular file, and -d tests if a file is a directory. So if you want it to only print once when it opens a directory, just add an extra test

Code:
if (-d $file) {
   print "On directory: $file";
}
elsif (-f $file && -T $file) {

Kirsle.net | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top