0

I have a string like C:\Users\shail.jet\Desktop\cca-lan_test_cases_path.txt.

I want to get only file name that is in this case is cca-lan_test_cases_path.txt.I have tried it with javascript split function but it did'nt work. Any help will be appreciated.

1
  • 1
    str.substring(str.lastIndexOf('\\') + 1) maybe? Commented Nov 13, 2014 at 8:56

2 Answers 2

0
 "C:\\Users\\shail.jet\\Desktop\\cca-lan_test_cases_path.txt".split("\\").pop();

Make sure you add escape backslashes in the file path string otherwise javascript will ignore it.

That will split by backslash and then use pop to get the last element in the array which will be the file name.

Sign up to request clarification or add additional context in comments.

Comments

0
var filename = fullPath.replace(/^.*[\\\/]/, '')

This will handle both \ OR / in paths

var filename = fullPath.replace(/^.*(\\|\/|\:)/, '');

should also protect from empty string

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.