use strict; use warnings; my @dirs; while (my $line = ) { my ($indent, $entry, $isDir) = $line =~ m!^(\s*)([^/]*)(/|\\)?!; next unless defined $entry and length $entry; my $depth = length ($indent); if ($depth < @dirs) { splice @dirs, $depth, $#dirs; } if ($isDir) { push @dirs, $entry; next; } print join ('/', @dirs, $entry); } __DATA__ base/ dir1/ file1.txt a.png dir2/ b.txt q.txt dir3/ a.png f.txt