#!/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 '$newdir'"; print "newdir now: '$newdir'\n\n"; #### 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'