【问题标题】:Get javascript filepath in Windows Script Host在 Windows Script Host 中获取 javascript 文件路径
【发布时间】:2020-05-03 17:22:12
【问题描述】:

使用与 Windows Script Host 一起运行的 .js 文件时,是否可以获得正在运行的 .js 文件的路径(与当前工作目录不同)?换句话说,我想要类似于 PowerShell 的 $PSScriptRoot 变量的东西。有没有办法得到这个?

我想要这个的原因是因为执行这些脚本时的工作目录并不总是与 .js 文件本身的位置相同,但我希望我的代码引用相对于 .js 文件位置的东西.

【问题讨论】:

标签: javascript windows directory path windows-scripting


【解决方案1】:
You can get this with Node Js 

    const path = require('path'); // you can install this with npm

    // __dirname = C:\dev\project
    // __filename = C:\dev\project\index.js
    const dir= path.basename(__dirname);
    const archive= path.basename(__filename);
    const fullpath = path.join(dir, archive);

    console.log('Dir:', dir); // Dir: test
    console.log('Archive:', archive); // Archive: index.js
    console.log('Dir + Archive:', fullpath); // Dir + Archive: project\index.js

【讨论】:

  • 是否可以将 node js 与 Windows Script Host 一起使用?我不确定是不是。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-30
相关资源
最近更新 更多