Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^17: search and replace strings in different files in a directory

by Anonymous Monk
on Sep 03, 2014 at 08:14 UTC ( [id://1099363]=note: print w/replies, xml ) Need Help??


in reply to Re^16: search and replace strings in different files in a directory
in thread search and replace strings in different files in a directory

There might be some umlauts in some of the path names. Would that trigger the alarm?

No alarm, but you might not be able to open the files without help of GetShortPathName or openW :) although you probably might open "da-MötleyCrüe", it depends

If you make sub ddiagnostic out of what I posted, you can use  ddiagnostic( $path ); to see if this is an issue

This is one of the reasons for the subs subs subs subs. You can write/debug using simple ascii test filenames ... figure out the program logic get it 99% there, then throw in some unicode filenames and only change a few subs

Or maybe even change no subs, maybe :)( Re^3: Trouble with File::Find::Rule (win32 unicode overload file test operators) )

  • Comment on Re^17: search and replace strings in different files in a directory
  • Download Code

Replies are listed 'Best First'.
Re^18: search and replace strings in different files in a directory
by PitifulProgrammer (Acolyte) on Sep 03, 2014 at 12:11 UTC

    Dear Anonymous Monk

    Thanks a mil for your help.

    I used your code to check and found out that umlauts were the culprits

    I created a test folder with test file and ran the code. To my surprise I got new error messages (cf. list below), which have not yet appeared... As you can imagine, the source files remained untouched and no back-up files were created

    Use of uninitialized value $dst in rename at C:/strawberry/perl/site/l +ib/Path/Tiny.pm line 1004. Path::Tiny::move(Path::Tiny=ARRAY(0x267ea6c), undef) called at hor +nbach_entities_20140902.pl line 68 main::Replace(Path::Tiny=ARRAY(0x267ea6c), "T:/2014/F.../C.../a.xm +l-2014-09-03.bak") called at script.pl line 59 main::RetrieveAndBackupXML("t:/F.../C.../\") called at script.pl l +ine 29 main::Main() called at script.pl line 18 Use of uninitialized value $dst in concatenation (.) or string at C:/s +trawberry/perl/site/lib/Path/Tiny.pm line 1004. Path::Tiny::move(Path::Tiny=ARRAY(0x267ea6c), undef) called at scr +ipt.pl line 68 main::Replace(Path::Tiny=ARRAY(0x267ea6c), "T://F.../C.../a.xml-20 +14-09-03.bak") called at hornbach_entities_20140902.pl line 59 main::RetrieveAndBackupXML("t:\\2014\\F...\\C...\\") called at hor +nbach_entities_20140902.pl line 29 main::Main() called at hornbach_entities_20140902.pl line 18 Error rename on 'T:/2014/F.../C...//a.xml' -> ''': at C:/strawberry/p +erl/site/lib/Path/Tiny.pm line 1490. Path::Tiny::Error::throw("Path::Tiny::Error", "rename", "T::/2014/ +F.../C.../a.xml' -> ''", "") called at C:/strawberry/perl/site/lib/Pa +th/Tiny.pm line 126 Path::Tiny::_throw(Path::Tiny=ARRAY(0x267ea6c), "rename", "T:/2014 +/F.../C.../a.xml' -> ''") called at C:/strawberry/perl/site/lib/Path/ +Tiny.pm line 1004 Path::Tiny::move(Path::Tiny=ARRAY(0x267ea6c), undef) called at scr +ipt.pl line 68 main::Replace(Path::Tiny=ARRAY(0x267ea6c)...[...]

    I seem unable to figure what the exact meaning of the error is, I reckon it must be something in the xml-files,

    I am really sorry for all these questions, however it is a good way to learn things.

    Thanks a mil for your support

    Kind regards

    C.

      It says you're giving move undef, I don't know why you're doing that, I can't see your code

        Dear Anonymous Monk(s)

        Of course, sorry I forgot to post the most recent version of the code.

        use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; use File::BOM; use Carp::Always; use Data::Dump qw/ dd /; use Win32; use Win32::Unicode qw/ statW /; Main( @ARGV ); exit( 0 ); sub Main { #my( $infile_paths ) = @_; my( $infile_paths ) = 'C:\dev\test_paths.txt';; my @paths = GetPaths( $infile_paths ); #print "The following paths were in the file:\n"; #say for @paths; for my $path ( @paths ){ RetrieveAndBackupXML( $path ); } #return @paths; } ## end sub Main sub GetPaths { use File::BOM; ## my @paths = path( shift )->lines_utf8; my @paths = path( shift )->lines( { binmode => ":via(File::BOM)" } + ); s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub RetrieveAndBackupXML { #~ my( $directory ) = @_; ## same as shift @_ ## same as shift my $directory = shift; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #sets cu +rrent date and time to be added to backup file my $bak = "$date.bak"; #date added to the .bak file my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); #sub Replace using it's 2 para +meters as defined below } } ## end sub Main sub Replace { my( $in, $bak ) = shift; path( $in )->move( $bak ); #Creates a copy of the original file my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { #s{&}{&amp;}g; ## will match more than what you want fix it s{&amp;amp;}{&amp;}g; s{\s>\s}{&gt;}g; s{\s<\s}{&lt;}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace

        In case you spot an errors in terms of comments, please do comment on the comment(s) in the code, the more feedback the better, since I might be using that code a lot and might also have to explain to colleagues.

        Thanks a mil for your help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1099363]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-23 21:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found