Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that is trying to re-create a directory structure in a new place. I have a sub that gets passed 3 things (1) the old directory root (2) the new directory root and (3) the particular directory in question. For some reason, I cannot seem to match the old root, to replace it with the new one and create the directory. Here is the code:
sub newDir { my $dir = shift; my $oldroot = shift; my $newroot = shift; my $newdir = $dir; # replace old root dir with new one and create new dir $newdir = $dir; $newdir =~ s/$oldroot/$newroot/; mkdir $newdir; }
What I am trying to do is to start with:
dir = c:\top\dir\file
oldroot = c:\top
newroot = c:\newtop
and create the directory
newdir = c:\newtop\dir\file

Am I missing something with the substitution? I have tried a number of opstions for s///, but cannot seem to get a match on $oldroot????? Thanks, Mike

Replies are listed 'Best First'.
Re: replace part of directory
by Util (Priest) on Aug 22, 2009 at 20:12 UTC

    When you put a variable in the first section of a s/// substitution, the variable's contents are used as a pattern, not as a simple string. So, c:\top is understood as 5 characters, with the middle character being a TAB ("\t"). You can fix this by using quotemeta.

    I would also add a start-of-string anchor to be safer.

    You may need something like make_path from File::Path instead of mkdir when creating multiple levels of directory all at once.

    Working, tested exmaple code:

    #!/usr/bin/perl use strict; use warnings; my $dir = 'c:\top\dir\file'; my $oldroot = 'c:\top'; my $newroot = 'c:\newtop'; my $newdir = $dir; print "Oldroot will be used as the pattern; It is '$oldroot'\n"; print "newdir was: '$newdir'\n"; $newdir =~ s/$oldroot/$newroot/ or warn "Can't find pattern '$oldroot' in dir string '$newdir'"; print "newdir now: '$newdir'\n\n"; my $oldroot_regex = quotemeta $oldroot; #$oldroot_regex = '^' . $oldroot_regex; # I would do this to be safe. print "Escaped pattern is '$oldroot_regex'\n"; print "newdir was: '$newdir'\n"; $newdir =~ s/$oldroot_regex/$newroot/ or warn "Can't find pattern '$oldroot_regex' in dir string '$newdi +r'"; print "newdir now: '$newdir'\n\n";
    Output:
    Oldroot will be used as the pattern; It is 'c:\top' newdir was: 'c:\top\dir\file' Can't find pattern 'c:\top' in dir string 'c:\top\dir\file' at pm_ +790602_03.pl line 13. newdir now: 'c:\top\dir\file' Escaped pattern is 'c\:\\top' newdir was: 'c:\top\dir\file' newdir now: 'c:\newtop\dir\file'
    By the way, you will save yourself some future headaches if you just standardize on POSIX paths (forward slashes instead of backslashes), which *do* work fine in Perl on Win32. I use $path =~ tr{\\}{/} to fix up user input.

Re: replace part of directory
by roboticus (Chancellor) on Aug 22, 2009 at 18:49 UTC

    I think you're taking the wrong approach. You might try something like:

    use File::Find; my $oldroot = shift; my $newroot = shift; find(\&wanted, $oldroot); sub wanted { # Chop off $oldroot prefix, glom on $newroot prefix print $newroot, substr($File::Find::name,length($oldroot), "\n"; }

    Note: Untested code, you'll likely need to fix it.

    ...roboticus