如何使用节点路径将Windows路径转换为posix路径

时间:2018-12-16 04:16:48

标签: javascript node.js windows posix

我正在Windows上进行开发,但是需要知道如何将Windows路径(带有反斜杠string hex (string sHex) { string sReturn = ""; for (int i = 0; i < sHex.length (); ++i) { switch (sHex [i]) { case '0': sReturn.append ("0000 "); break; case '1': sReturn.append ("0001 "); break; case '2': sReturn.append ("0010 "); break; case '3': sReturn.append ("0011 "); break; case '4': sReturn.append ("0100 "); break; case '5': sReturn.append ("0101 "); break; case '6': sReturn.append ("0110 "); break; case '7': sReturn.append ("0111 "); break; case '8': sReturn.append ("1000 "); break; case '9': sReturn.append ("1001 "); break; case 'a': sReturn.append ("1010 "); break; case 'b': sReturn.append ("1011 "); break; case 'c': sReturn.append ("1100 "); break; case 'd': sReturn.append ("1101 "); break; case 'e': sReturn.append ("1110 "); break; case 'f': sReturn.append ("1111 "); break; } } return sReturn; } int main() { ifstream file; file.open("MachineCode.txt"); if (!file.is_open()) { cout << "ERROR"; } else { stringstream strStream; strStream << file.rdbuf(); string contents = strStream.str(); cout << hex(contents) << endl; } return 0; )转换为带有正斜杠(\的POSIX路径吗?

我的目标是将/转换为C:\repos\vue-t\tests\views\index\home.vue

所以我可以在导入到磁盘的文件中使用它

C:/repos/vue-t/tests/views/index/home.vue

我不想使用const appImport = ` import Vue from "vue" import App from '${path}' function createApp (data) { const app = new Vue({ data, render: h => h(App) }) return app }` //this string is then written to the disk as a file 字符串,而是希望使用.replace(/\\/g, '/')函数。

6 个答案:

答案 0 :(得分:14)

鉴于所有其他答案都依赖于安装(大型)第三方模块:这也可以使用Node的标准库path模块作为一个单行代码来完成,更具体地说,可以利用其专用的{ {1}}和path.posix命名空间的属性/功能:

path.win32

这将把您的路径转换为POSIX格式,而不管您是否已经在POSIX平台或win32上,而需要零依赖。

答案 1 :(得分:2)

Slash将Windows反斜杠路径转换为Unix路径

用法:

hashCode

答案 2 :(得分:1)

有一个名为upath的节点程序包会将Windows路径转换为unix。

$()

$(*betweenHeaders)

答案 3 :(得分:0)

模仿slashupath的单线(请参阅多线版本的有效性证明)

//
// one-liner
//
let convertPath = (windowsPath) => windowsPath.replace(/^\\\\\?\\/,"").replace(/\\/g,'\/').replace(/\/\/+/g,'\/')

//
// usage
//
convertPath("C:\\repos\\vue-t\\tests\\views\\index\\home.vue")
// >>> "C:/repos/vue-t/tests/views/index/home.vue"

//
// multi-liner (commented and compatible with really old javascript versions)
//
function convertPath(windowsPath) {
    // handle the edge-case of Window's long file names
    // See: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names
    windowsPath = windowsPath.replace(/^\\\\\?\\/,"");

    // convert the separators, valid since both \ and / can't be in a windows filename
    windowsPath = windowsPath.replace(/\\/g,'\/');

    // compress any // or /// to be just /, which is a safe oper under POSIX
    // and prevents accidental errors caused by manually doing path1+path2
    windowsPath = windowsPath.replace(/\/\/+/g,'\/');

    return windowsPath;
};

// dont want the C: to be inluded? here's a one-liner for that too
let convertPath = (windowsPath) => windowsPath.replace(/^\\\\\?\\/,"").replace(/(?:^C:)?\\/g,'\/').replace(/\/\/+/g,'\/')

答案 4 :(得分:0)

只需将默认库用作:

const {direname, resolve, basename}= require('path').posix;

import {posix} from 'path';
const {direname, resolve, basename}= posix;

答案 5 :(得分:-2)

const winPath = 'C:\\repos\\vue-t\\tests\\views\\index\\home.vue'
const posixPath = winPath.replace(/\\/g, '/').slice(2)
// Now posixPath = '/repos/vue-t/tests/views/index/home.vue'