use strict; use warnings; use File::Spec (); foreach ( '/rootdir/sub1/sub2/sub3/sub4/', '/rootdir/sub1/sub2/sub3/file', '/rootdir/', '/rootfile', '/', 'C:\\', 'reldir/sub1/', 'reldir/file', 'reldir/', 'relfile', ) { my $path = $_; print("$path\n"); my ($vol, $dirs) = File::Spec->splitpath($path, 0); $dirs = File::Spec->canonpath($dirs); my @dirs = File::Spec->splitdir($dirs); pop(@dirs); $dirs = File::Spec->catdir(@dirs); $path = File::Spec->catpath($vol, $dirs, ''); print(" -> $path\n"); }