use File::Spec; my $goners = 3; # how many? my $str = 'C:\fldr1\fld2\fldr3\fldr4\fldr5\test.txt@@\main\Msometest\test12.1x\1'; $str =~ s/\@/\@.*//; my @dirs = File::Spec->splitdir($str); splice @dirs, 0, $goners; print File::Spec->catdir(@dirs);