For a file name with an absolute path, such as: D: \ Program Files \ Notepad ++ \ notepad1_cmd.exe
First, to avoid escape backslash problems, you can use a regular expression to replace \ or \ with #, such:
D: # Program files?notepad={{notepad={.exe
Then, use '#' as the separator to break the string into an array and obtain the following array:
D: ProgramFiles Notepad ++ notepad1_cmd.exe
Take the last file name of the array with a suffix: notepad1_0000.exe
Separate the filename with a suffix into an array using the separator '.' to obtain the following array:
Notepad ++ exe
Then retrieve the last one of the array to get the file suffix exe.
The Code is as follows (Win7 + IE9 passed the test ):
Copy codeThe Code is as follows:
// By MoreWindows (http://www.jb51.net/qq)
Function GetExtensionFileName (pathfilename)
{
Var reg =/(\ +)/g;
Var pfn = pathfilename. replace (reg ,"#");
Var arrpfn = pfn. split ("#");
Var fn = arrpfn [arrpfn. length-1];
Var arrfn = fn. split (".");
Return arrfn [arrfn. length-1];
}
// By MoreWindows (http://www.jb51.net/qq)
Function GetExtensionFileName (pathfilename)
{
Var reg =/(\ +)/g;
Var pfn = pathfilename. replace (reg ,"#");
Var arrpfn = pfn. split ("#");
Var fn = arrpfn [arrpfn. length-1];
Var arrfn = fn. split (".");
Return arrfn [arrfn. length-1];
}
Test code:
Copy codeThe Code is as follows:
Function Test ()
{
Var filePath = "D :\\ Program Files \ Notepad ++ \ notepad1_.exe ";
Alert (GetExtensionFileName (filePath ));
}
<Input type = "button" value = "Test" onclick = "Test ()"/>
Function Test ()
{
Var filePath = "D :\\ Program Files \ Notepad ++ \ notepad1_.exe ";
Alert (GetExtensionFileName (filePath ));
}
<Input type = "button" value = "Test" onclick = "Test ()"/>
Click the "Test" button to bring up the "exe" dialog box, indicating that GetExtensionFileName can correctly parse the file name with an absolute path and get the suffix.
It is estimated that this method can only be used on the Windows platform. I don't know what it will do in Linux?