Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

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


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

Hmm, you say it exists as a directory, does the path contain any unicode codepoints ? What do you get with this program

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use Win32; use Win32::Unicode qw/ statW /; my $path = 'Z:/123/xyz/xyz/xyz_xyz_xyz_/01_abc'; dd( $path ); dd({ -ansi, Win32::GetANSIPathName( $path ) }); dd({ -full, scalar Win32::GetFullPathName( $path ) }); dd({ -short, Win32::GetShortPathName( $path ) }); dd( statW( $path ) ); __END__
  • Comment on Re^15: search and replace strings in different files in a directory
  • Download Code

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

    Dear Anonymous Monk

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

    The initial purpose of the file (before it becam a rather huge project for a novice like me) was to do the following:

    read the path in the text-file provided by user

    go to each directory specified by the path name

    Look for any xml-files and open each one individually

    in each xml-file replace certain strings

    create a backup of the xml(s)

    close the file(s)

    close the directory

    Hope that helps (it did for me to get an initial idea what I was going to do step by step)

    Looking forward to your reply. Thanks for your help

    Kind regards

    C.

      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) )

        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.

Log In?
Username:
Password:

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

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

    No recent polls found