if ($filepath =~ /([^\/\\]+)$/) { $file = $1; } else { $file = $filepath; } #### $filepath = 'c:\windows\foo\bar\quux\baz.txt';