簡體   English   中英

在Windows上將相對路徑轉換為絕對路徑?

[英]Convert relative path to absolute path on Windows?

我知道這個問題: 使用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

是的..我有點卡在這里。 我想這需要某種解析循環,但是我無法提出解決方案。
我希望你能在這里幫助我。 :/

問題

  • 如何在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM