in reply to Robocopy log parsing

You are making life hard for yourself. Why not just use the robocopy option to include full pathnames in the logfile?

/FP :: include Full Pathname of files in the output.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Robocopy log parsing
by bit5nip3r (Initiate) on May 29, 2012 at 09:55 UTC

    Don't have that option. I'm not the creator of these logs files.

      I think you want something like that:

      use warnings; use strict; my $filename = $ARGV[0]; #$filename = 'robocopy_log.txt'; open my $hin, '<', $filename or die "Cannot open file /$filename/"; my @fcont = <$hin>; my ( $dir, $txt, $nbytes, $file_found, $sav_dir, $sav_file ); foreach my $line (@fcont) { if ( $line =~ /New File|New Dir/ ) { ($dir) = $line =~ /New Dir\s+(?:[0-9]+|[0-9]+?\.[0-9]+? +\s[a-z])\s(.*)/; ($file_found) = $line =~ /New File\s+(?:[0-9]+|[0-9]+?\.[0-9]+ +?\s[a-z])\s(.*?)\s+?/; ( $txt, $nbytes ) = $line =~ /(New File|New Dir)\s+([0-9]+|[0- +9]+?\.[0-9]+?\s[a-z])\s.*/; $sav_dir = $dir if $dir; $sav_file = $file_found if $file_found; $sav_dir = !defined $sav_dir ? q{} : $sav_dir; $sav_file = !defined $sav_file ? q{} : $sav_file; print join( "\t\t", $txt, $sav_dir, $nbytes, $sav_file ) . "\n +"; } } close $hin;

      Also look at this example, how to avoid $1, $2, ... matching parameters, you can directly assing the result to a list of variables

      You also can try to put the three assignments in ony line, this is left as an exercise ;-)

        Thanks alot for this. Works very nicely. There is one thing though. This is tyhe result:

        New Dir + 16 x:\xxxxxx\xxx\ New File x:\xxxxxx\xxx\ + 126862 xxxxxxxxxxxxxxxxxxxxxxx.xxx New File x:\xxxxxx\xxx\ + 48640 xxxxxxxxxxxxxxxxxxxxxxxxx.xxx New File x:\xxxxxx\xxx\ + 96494 xxxxxxxxxxxxxxxxxxxxxx.xxx New File x:\xxxxxx\xxx\ + 105466 xxxxxxxxxxxxx.xxx New File x:\xxxxxx\xxx\ + 653655 xxxxxxxxxx.xxx New File x:\xxxxxx\xxx\ + 542396 123456778910xxxxx.xxx New Dir + 6 y:\yyyyyyyyy\yyyyyyyyyyy\yyyy +yyy\123456778910xxxxx.xxx New File y:\yyyyyyyyy\yyyyyyyyyyy\yyyyyyy\ + 41984 yyyyyyyyyyyyyyyyyy.yyy New File y:\yyyyyyyyy\yyyyyyyyyyy\yyyyyyy\ + 10007 yyyyyyyyyyyyyyy.yyy New File y:\yyyyyyyyy\yyyyyyyyyyy\yyyyyyy\ + 1.3 m yyyyyy.yyy

        when it encounters the next New Dir entry, it adds the last filename from the previous one. as exampled above. Any suggestions?

        Thanks. I made a few tweaks, and have also wraped a MySQL import process around it. So now it parses the log file and directly imports into MySQL.

        use warnings; use strict; use DBI; # Set datasource my $dsn = "DBI:mysql:host=<hostname>;database=<dbname>" . ";port=<portnumber>"; # Connect to database my $dbh = DBI->connect($dsn,"userid","password"); print "Status: Connected to MySQL.\n"; my $filename = $ARGV[0]; open my $hin, '<', $filename or die "Cannot open file /$filename/"; my @fcont = <$hin>; my ($dir, $txt, $nbytes, $file_found, $sav_dir, $sav_file); foreach my $line (@fcont) { if ($line =~ /New File|New Dir/) { chomp($line); ($dir) = $line =~ /New Dir\s+(?:[0-9]+|[0-9]+?\.[0-9]+? +\s[a-z])\s(.*)/; ($file_found) = $line =~ /New File\s+(?:[0-9]+|[0-9]+?\.[0-9]+ +?\s[a-z])\s+(.*?$)/; ( $txt, $nbytes ) = $line =~ /(New File|New Dir)\s+([0-9]+|[0- +9]+?\.[0-9]+?\s[a-z])\s+.*?/; $sav_dir = $dir if $dir; $sav_file = $file_found if $file_found; $sav_dir = !defined $sav_dir ? q{} : $sav_dir; $sav_file = !defined $sav_file ? q{} : $sav_file; # print join("|", $ARGV[0],$txt,$nbytes,$sav_dir,$file_found). + "\n"; my $insert = $dbh->prepare('INSERT INTO <tablename> (File,Type,Size,Dir,Name) VALUES (?,?,?,?,?)') or die "Prepare failed: " . $ +dbh->errstr(); $insert->execute($ARGV[0],$txt,$nbytes,$sav_dir,$file_found) + or die "Execute failed: " . $dbh->errstr(); } } close $hin; print "Status: Processing of $ARGV[0] complete.\n"; # Disconnect from the database $dbh->disconnect (); print "Status: Disconnected from MySQL.";