在Windows上将相对路径转换为绝对路径?

时间:2015-10-09 08:24:08

标签: javascript relative-path jscript absolute-path wsh

我知道这个问题:Convert relative path to absolute using JavaScript
但它是关于在浏览器中运行的JavaScript。我的JS在Windows(WSH)上运行 这意味着我没有以下对象:windowdocumentconsole。 我已经想出了一些事情:
既然你可以在路径中使用斜杠(/)而不是反斜杠()而你不需要转义斜杠,我会尝试使用/ ...我也认为最好删除尾部如果有的话会削减。

所以这里有一些我已经想到的事情:

var currentDir = new ActiveXObject("WScript.Shell").CurrentDirectory.replace(/\\/g, '/'); //current directory with slashes
var root = currentDir.substring(0,2) //e.g. C: or D: (without trailing slash)

有几个不同的相对路径需要正确转换。为了确保,这里有一些例子:

如果脚本是从C:\folder1\folder2\folder3启动的,则应相应地转换路径:

/ => C:
/test => C:/test
\test => C:/test
\test\ => C:/test
.. => C:/folder1/folder2
C:\folder1\folder2\folder3\..\folder3-1\test.js => C:/folder1/folder2/folder3-1/test.js
../../test.js => C:/folder1/test.js
D:\ => D:
. => C:/folder1/folder2/folder3
./test => C:/folder1/folder2/folder3/test
.\..\.. => C:/folder1
D:/folder/another folder/file.js/../../other file.js => D:/folder/other file.js

是的..我有点被困在这里。我想这需要某种解析循环,但我无法想出解决方案 我希望你能在这里帮助我。 :/

1 个答案:

答案 0 :(得分:0)

问题

  • 如何在Windows脚本宿主jscript中执行常见的文件路径操作

解决方案

  • 将pathstep分隔符标准化为正斜杠,然后使用split()将其转换为数组

示例

<?xml version="1.0"?>
<package>
<job id="default" filename="filepath_operations.wsf">
<script language="jscript">
<![CDATA[
    //  <beg-block>
    //  - caption: demo filepath operations in wsh jscript
    //    rectype: rrmicro004
    //    dmid:    "uu236zogguwfrosk"
    //    date:    "2019-04-12 15:56:17"
    //    tags:    file,path,
    //    notes:  |
    //      * seealso ;; href="https://stackoverflow.com/questions/33033388/convert-relative-path-to-absolute-path-on-windows"
    //  <end-block>

    // regionbeg_::       vars.init//
    var str_currdir;
    var lst_pathsteps;
    // regionend_://

    // regionbeg_::       vars.populate//
    str_currdir = new ActiveXObject("WScript.Shell").CurrentDirectory.replace(/\\/g, '/'); //current directory with slashes
    lst_pathsteps = str_currdir.split("/");
    // regionend_://

    // regionbeg_::       pathstep.common.operations//
    WScript.Echo("total_pathsteps: "        + lst_pathsteps.length);                    // total_pathsteps
    WScript.Echo("root_drive: "             + lst_pathsteps[0]);                        // root_drive
    WScript.Echo("full_directory: "         + str_currdir);                             // full_directory
    WScript.Echo("this_dir_step: "          + lst_pathsteps[lst_pathsteps.length-1]);   // this_dir_step
    WScript.Echo("parent_dir_step: "        + lst_pathsteps[lst_pathsteps.length-2]);   // parent_dir_step
    WScript.Echo("grandparent_dir_step: "   + lst_pathsteps[lst_pathsteps.length-3]);   // grandparent_dir_step
    WScript.Echo("great1xgrandparent_dir_step: " + lst_pathsteps[lst_pathsteps.length-4]);   // great1xgrandparent_dir_step
    WScript.Echo("great2xgrandparent_dir_step: " + lst_pathsteps[lst_pathsteps.length-5]);   // great2xgrandparent_dir_step
    // regionend_://
]]>
</script>
</job>
</package>