my @destArray = split(/\//, $destPath); #splits path on the forward slash. while(scalar(@destArray)) { my $cmd = "cd \"" . $destArray[0] . "\""; print "command: $cmd\n"; sendCommand($session, $cmd);#"cd \"" . $destArray[0] . "\""); if ($commandResponse =~ m/Operation failed/) { print "Debug: last cd command failed, so now create the rest of the tree.\n"; last; } shift (@destArray); # otherwise remove the directory from the list and continue }