Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > Perl
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 11th, 2011, 12:35 PM
Registered User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Reply With Quote
  #2 (permalink)  
Old July 12th, 2011, 12:34 AM
Authorized User
Points: 93, Level: 1
Points: 93, Level: 1 Points: 93, Level: 1 Points: 93, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2009
Location: Moldova
Posts: 23
Thanks: 0
Thanked 2 Times in 2 Posts
Default

it's "=~" not "= ~".
Reply With Quote
The Following User Says Thank You to chorny For This Useful Post:
mnagaraj1983 (July 12th, 2011)
  #3 (permalink)  
Old July 12th, 2011, 01:55 AM
Registered User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:42 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.