Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2011, 12:35 PM
Registered User
 
Join Date: Jul 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default Watch Folder when XML files are placed and then process

Hi All,

I have problem with XML string manipulation for the new files are placed in the specified watch folder location. It break my head from yesturday to till.

I've posted the code as below,

Code:
#!/usr/bin/perl

# watchdir:

use strict;
use warnings;

# global variables
my $interval = 6;    # time (in seconds) between checks

# If no command-line argument, use the current directory
my $dirname = "G:/Temp";
my $myContents;

die "$dirname is not a directory\n" unless -d $dirname;

sub get_dir_entries($)
{
    my ($dirname) = @_;

    my %entries = ();
    opendir(DIR, $dirname) or die "can't opendir $dirname: $!\n";
    while (defined(my $filename = readdir(DIR)))
    {
        next if $filename =~ /^\.\.?$/;     # skip . and ..
        $entries{$filename} = 1;
    }
    closedir(DIR);

    return \%entries;
}

sub diff_hash_keys($$)
{
    my ($ref_hashA, $ref_hashB) = @_;

    my %hashA = %$ref_hashA;
    my %hashB = %$ref_hashB;
    my @A_not_B = ();
    my @B_not_A = ();

    foreach (keys %hashA)
    {
        push(@A_not_B, $_) unless exists $hashB{$_};
    }
    foreach (keys %hashB)
    {
        push(@B_not_A, $_) unless exists $hashA{$_};
    }

    return (\@A_not_B, \@B_not_A);
}

#Main Function are here.
#***********************

MAIN
{
    $| = 1; # enable autoflushing of output so this works better when piped
    my $prev = undef;
    while (1)
    {
        my $date = localtime();
        my $curr = get_dir_entries($dirname);
        if (defined($prev))
        {
            my ($added, $removed) = diff_hash_keys($curr, $prev);
            foreach (sort @$added)
            {
                print "$date: Added file $_\n";

		print $dirname . "/" . $_;

		open(inputFile, "+< $dirname" . "/" . $_) or die "dfsfjs dfjsdfdsl";

		while(<inputFile>)
		{
			chomp;
			$myContents = $myContents . $_;
		}
		my $ReplaceText = $myContents;
		$ReplaceText = ~s/sometext/sometext/gi;
			
		close(inputFile);
		
            }
            foreach (sort @$removed)
            {
                print "$date: Removed file $_\n";
            }
            #if (@$added || @$removed)
            #{
             #   print "\a\a\a\a"; # make a beep
            #}
        }
        $prev = $curr;

        sleep $interval;
    }
}
the above code will thrown me the errors when i placing the replace code as $ReplaceText = ~s/sometext/sometext/gi;

Kindly give solution for this issue and thanks in advance.

REgards,
Nagaraj
 
Old July 12th, 2011, 12:34 AM
Authorized User
 
Join Date: Aug 2009
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
Default

it's "=~" not "= ~".
The Following User Says Thank You to chorny For This Useful Post:
mnagaraj1983 (July 12th, 2011)
 
Old July 12th, 2011, 01:55 AM
Registered User
 
Join Date: Jul 2011
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks you so much, you save me lot of times.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to process multiple input xml files with a single xslt ? Divya XSLT 12 October 9th, 2018 03:01 AM
Multiple inputs: process all files in directory stolte XSLT 2 March 22nd, 2010 02:41 PM
process all xml files in folder lsantos2000 XSLT 4 November 8th, 2007 01:41 PM
How to Zip files within a folder Udi C# 2 January 25th, 2007 12:45 AM
FILES in FOLDER luma SQL Server DTS 0 June 9th, 2005 01:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.