admin管理员组文章数量:1355665
I am writing a small windows script in javascript/jscript for finding a match for a regexp with a string that i got by manipulating a file.
The file path can be provided relative or absolute. How to find whether a given path is absolute/relative and convert it to absolute for file manipulation?
I am writing a small windows script in javascript/jscript for finding a match for a regexp with a string that i got by manipulating a file.
The file path can be provided relative or absolute. How to find whether a given path is absolute/relative and convert it to absolute for file manipulation?
Share Improve this question asked Mar 9, 2010 at 4:46 SriramSriram 1,1902 gold badges15 silver badges27 bronze badges2 Answers
Reset to default 6How to find whether a given path is absolute/relative ...
From the MSDN article Naming Files, Paths, and Namespaces:
A file name is relative to the current directory if it does not begin with one of the following:
- A UNC name of any format, which always start with two backslash characters ("\\"). For more information, see the next section.
- A disk designator with a backslash, for example "C:\" or "d:\".
- A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.
So, strictly speaking, an absolute path is the one that begins with a single backslash (\
). You can check this condition as follows:
if (/^\\(?!\\)/.test(path)) {
// path is absolute
}
else {
// path isn't absolute
}
But often by an absolute path we actually mean a fully qualified path. In this is the case, you need to check all three conditions in order to distinguish between full and relative paths. For example, your code could look like this:
function pathIsAbsolute(path)
{
if ( /^[A-Za-z]:\\/.test(path) ) return true;
if ( path.indexOf("\\") == 0 ) return true;
return false;
}
or (using a single regex and a bit less readable):
function pathIsAbsolute(path)
{
return /^(?:[A-Za-z]:)?\\/.test(path);
}
... and convert it to absolute for file manipulation?
Use the FileSystemObject.GetAbsolutePathName
method:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var full_path = fso.GetAbsolutePathName(path);
To check whether the path is relative or absolute, look for a leading /
.
If it doesn't have one, you need to concatenate the path to a base path. Some programming environments have a "current working directory", but Javascript that lives in the browser doesn't, so you just need to pick a base path and stick to it.
function full_path(my_path) {
var base_path = "/home/Sriram/htdocs/media";
var path_regex = /^\/.*$/;
if(path_regex.test(my_path)) {
return my_path;
} else {
return base_path + my_path;
}
}
Paths can contain newlines, which the javascript regex .
won't match, so you might want to develop a more sophisticated regex to make sure all paths will work properly. However, I'd consider that outside the scope of this answer, and of my knowledge. :-)
本文标签:
版权声明:本文标题:regex - How to find whether a given path is absoluterelative and convert it to absolute for file manipulation? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743961219a2569062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论