简体   繁体   English

如何查找给定路径是绝对/相对路径还是将其转换为绝对路径以进行文件操作?

[英]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. 我正在javascript / jscript中编写一个小的windows脚本,用于查找regexp的匹配项,其中包含通过操作文件获得的字符串。

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? 如何查找给定路径是绝对/相对路径还是将其转换为绝对路径以进行文件操作?

How to find whether a given path is absolute/relative ... 如何查找给定路径是绝对的还是相对的...

From the MSDN article Naming Files, Paths, and Namespaces : 从MSDN文章命名文件,路径和命名空间

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 ("\\\\"). 任何格式的UNC名称,始终以两个反斜杠字符(“\\\\”)开头。 For more information, see the next section. 有关更多信息,请参阅下一节。
  • A disk designator with a backslash, for example "C:\\" or "d:\\". 带反斜杠的磁盘指示符,例如“C:\\”或“d:\\”。
  • A single backslash, for example, "\\directory" or "\\file.txt". 单个反斜杠,例如“\\ directory”或“\\ 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: 使用FileSystemObject.GetAbsolutePathName方法:

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. 有些编程环境有一个“当前工作目录”,但是浏览器中的Javascript没有,所以你只需要选择一个基本路径并坚持下去。

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 . 路径可以包含新行,javascript正则表达式. 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. 但是,我认为这不属于这个答案的范围,而是我的知识。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM