l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 24: LUGOD election season has begun!
Page last updated:
2003 Jul 01 16:27

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
[vox-tech] Another Perl Question: File::Find module not working as expected
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vox-tech] Another Perl Question: File::Find module not working as expected



The following script addresses the problem I had earlier of moving files
from a Windows computer to a Unix server when the directory structures are
different.  Within each subfolder on the Unix server there is a directory
called "messages".  For some reason, it seems that File::Find is not
traversing the directory tree properly and going into the messages folder
at all.

Any suggestions would be more than welcome.

#!/usr/local/bin/perl -w

# FILE:		vton.pl
# PURPOSE:	Retrieve current discussion board messages
#		from Vader and put them on Neo
# AUTHOR:	Richard Crawford (rcrawford@unexmail.ucdavis.edu)
# CREATED:	25 June 2003
# EDIT HISTORY:
# ===============================================================
# DATE          AUTHOR  EDIT
# 07/01/03	RC	Updated script to retrieve multiple files
# ===============================================================

use strict;
use File::Find;
use Net::FTP;

my $vaderIP = "xxx.xxx.xxx.xxx";

my $start_dir = shift or die "Usage: $0 <start_dir>\n";

unless (-d $start_dir) {
  die "Start files $start_dir is not a directory.\n";
}

# Connect to Vader
my $VaderConnect = Net::FTP->new($vaderIP) or die ("VTON: Can't connect to
Vader: $!\n");

$VaderConnect->login('Richard','password') or die ("VTON: Can't log in to
Vader: $!\n");
$VaderConnect->ascii();

print "\nVTON: Successfully connected to Vader.\n\n";


# traverse directory tree and move files
find (\&move, $start_dir) or die ("Couldn't get to $File::Find::dir/$_:
$!\n");

# Disconnect from Vader
$VaderConnect->quit();

sub move { #subroutine to move files

  # Find out the IP address of the current computer
  my $interface="hme0";
  my $ifconfig="/usr/sbin/ifconfig";
  my @lines = qx|$ifconfig $interface| or die "Could not execute ifconfig:
$!\n";
  my $neoIP;
  foreach(@lines) {
    if (/inet ([\d.]+)/) {
      my $neoIP = $1;
    }
  }

  # Find the current directory
  my $neoDir = $File::Find::dir;

  # Move to the equivalent directory on Vader
  my $baseDir = $neoDir;
  $baseDir =~ s#/space/usr/jrun4/servers/default/cfusion/DLC/##g;
  my $vaderDir = "/HTTPHome/$baseDir";
  $vaderDir =~ s#/extras##g;

  $VaderConnect->cwd($vaderDir);

  # Get all the files on Vader in this directory

  my @VaderFiles = $VaderConnect->ls($vaderDir) or die ("Cannot get list
of files: $!\n");

  # Now get each item from Vader and process them
  my $currentFile;

  print ("Number of files in $vaderDir: $#VaderFiles\n"); # return;

  foreach $currentFile (@VaderFiles)
  {

    $currentFile =~ s(^.*/)();

    return unless (
      ($currentFile =~ m/[0-9]+.html$/) or #message files
      ($currentFile =~ m/data.txt$/) or    #data.txt
      ($currentFile =~ m/wwwboard.html$/) #wwwboard.html
    );

    print "\nCurrent File is $currentFile\n";

    # Get the file from Vader
    $VaderConnect->get("$currentFile");

    # Replace "action=wwwboard.pl" with direct path to wwwboard.pl
    # Create an absolute path to wwwboard.pl
    $neoDir =~ s#/space/usr/jrun4/servers/default/cfusion/##g;
    my $URL="http://$neoIP/$baseDir/wwwboard.pl";;
    $URL =~ s#/messages##g;
    my $VaderURL="http://152.79.198.9/$baseDir/wwwboard.pl";;
    $VaderURL =~ s#/messages##g;
    unless (open IN, $currentFile) {
      die "VTON: Can't open file $currentFile for reading: $!\n";
    }
    unless (open OUT, "> fixedfile") {
      die "VTON: Can't open output file: $!\n";
    }
    while (<IN>) {
      # Fix form action tags.
      s/action="wwwboard\.pl"/action="$URL"/gi;
      s/action="$VaderURL"/action="$URL"/gi;
      # Fix style sheet call:
      s#href="../../../../Library/#href="../../../../../Library/#g;
      if ($currentFile =~ m#[0-9]+.html$#) {
        s#href="../../../../../Library/#href="../../../../../../Library/#g;
      }
      print(OUT $currentFile) or die "VTON: Can't write to fixedfile: $!\n";
    }
    close (IN);  close (OUT);
    rename("$currentFile","$currentFile.old");
    rename("fixedfile","$currentFile");

    chmod 0777, $currentFile;

    print "VTON: Copied and fixed $File::Find::dir/$currentFile\n";
  }
}




Sliante,
Richard S. Crawford

http://www.mossroot.com
AIM: Buffalo2K ICQ: 11646404 Y!: rscrawford
MSN: underpope@hotmail.com

"It is only with the heart that we see rightly; what is essential is
invisible to the eye." --Antoine de Saint Exupéry


_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.