简体   繁体   中英

Convert relative path to absolute path on Windows?

I know of this question: Convert relative path to absolute using JavaScript
But it is about JavaScript that runs in a browser. My JS runs on Windows (WSH).
That means I don't have the following objects: window , document and console . I already figured a few things out:
Since you can use slash (/) instead of a backslash () in a path and you don't need to escape a slash, I'll try to work with / ... I also figured it would be best to remove the trailing slash if there is one.

So here are a couple of things I already figured out:

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)

There are a couple of of different relative pathes that have to be converted correctly. Just to make sure, here are some examples:

If the script was launched from C:\\folder1\\folder2\\folder3 the paths should be converted accordingly:

/ => 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

And yeah.. I'm kind of stuck here. I guess this requires some kind of parsing loop, but I just couldn't come up with the solution.
I hope you can help me out here. :/

Problem

  • how to do common file path operations in windows script host jscript

Solution

  • normalize the pathstep separator to forwardslash then convert that to an array with split()

Example

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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