Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Windows上是否将相对路径转换为绝对路径?_Javascript_Relative Path_Jscript_Absolute Path_Wsh - Fatal编程技术网

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

Javascript 在Windows上是否将相对路径转换为绝对路径?,javascript,relative-path,jscript,absolute-path,wsh,Javascript,Relative Path,Jscript,Absolute Path,Wsh,我知道这个问题: 但它是关于在浏览器中运行的JavaScript的。My JS在Windows(WSH)上运行。 这意味着我没有以下对象:窗口、文档和控制台。 我已经想出了一些办法: 由于您可以在路径中使用斜杠(/)而不是反斜杠(),并且不需要转义斜杠,因此我将尝试使用/。。。我还认为,如果后面有斜杠,最好删除它 以下是我已经想到的几件事: var currentDir = new ActiveXObject("WScript.Shell").CurrentDirectory.replace(/

我知道这个问题:
但它是关于在浏览器中运行的JavaScript的。My JS在Windows(WSH)上运行。
这意味着我没有以下对象:
窗口
文档
控制台
。 我已经想出了一些办法:
由于您可以在路径中使用斜杠(/)而不是反斜杠(),并且不需要转义斜杠,因此我将尝试使用/。。。我还认为,如果后面有斜杠,最好删除它

以下是我已经想到的几件事:

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/other folder/file.js/../other file.js
=>
D:/folder/other file.js

是的。。我被困在这里了。我想这需要某种解析循环,但我就是想不出解决方案。
我希望你能在这里帮助我/

问题
  • 如何在windows脚本主机jscript中执行公共文件路径操作
解决方案
  • 将pathstep分隔符规范化为正斜杠,然后使用
    split()
例子

//-标题:演示wsh jscript中的文件路径操作
//重排:rrmicro004
//dmid:“uu236zogguwfrosk”
//日期:“2019-04-1215:56:17”
//标记:文件、路径、,
//注:|
//*另见;;href=”https://stackoverflow.com/questions/33033388/convert-relative-path-to-absolute-path-on-windows"
//  
//regionbeg\:vars.init//
var str_currdir;
var lst_路径步骤;
//地区和地区://
//regionbeg\:vars.populate//
str_currdir=newActiveXObject(“WScript.Shell”).CurrentDirectory.replace(/\\\\/g,“/”)//带斜杠的当前目录
lst_pathsteps=str_currdir.split(“/”);
//地区和地区://
//regionbeg\:路径步骤.common.operations//
Echo(“total_pathsteps:+lst_pathsteps.length);//总步数
Echo(“根驱动器:+lst\u路径步骤[0]);//根驱动
Echo(“完整目录:+str\u currdir);//完整目录
Echo(“此路径步骤:+lst路径步骤[lst路径步骤.length-1]);//这是第一步
Echo(“父目录步骤:+lst路径步骤[lst路径步骤.length-2]);//父目录步骤
Echo(“祖父母目录步骤:+lst路径步骤[lst路径步骤.length-3]);//祖父母
WScript.Echo(“great1x祖父母目录步骤:+lst路径步骤[lst路径步骤.length-4]);//Great1x祖父母指导步骤
WScript.Echo(“great2x祖父母目录步骤:+lst路径步骤[lst路径步骤.length-5]);//Great2x祖父母指导步骤
//地区和地区://
]]>

这与JavaScript有什么关系??这不是一个复制品。考虑使用<代码>脚本。FraseSistMebObj/<代码>,因为它具有更多的处理路径的功能。
<?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>