質問

どのように伝えJavaScriptの何パスセパレータ用OSでのスクリプトはrunnning?

役に立ちましたか?

解決

Afairにいつでも利用してパスセパレータでもWindows上でしか利用できません。

見積もりから http://bytes.com/forum/thread23123.html:

その状況に要約できる なっているわけではなく:

  • すべてのDOSサービス-インフラに変えてDOS2.0およびすべてのWindows Apiのどちらかを受け入れ前 スラッシュまたはバックスラッシュ.いています。

  • の標準コマンドはシェル(CMDまたはコマンド)を受け入れ前 スラッシュ.ものです。/tmp"の例 与前の投稿失敗します。

他のヒント

使用 path モジュール node.js を返しますプラットフォーム固有のファイルセパレーターが不要になる。

path.sep  // on *nix evaluates to a string equal to "/"

編集:しSebasのコメントは以下の利用を追加する必要がありますので、この上部のjsファイル:

const path = require('path')

正しい答え

あり すべてのOSのaccept CD../CD..\CD..にかかわらず通過する分離機。その一方で、読みのパスです。かご存知だろうかがえ、"windowsパス ' '\ 許可されます。

当然の'Duh!' 質問

るとどうなるか、試してみなさいよ、例えば、インストール先ディレクトリ %PROGRAM_FILES% (x86)\Notepad++.以下の例です。

var fs = require('fs');                             // file system module
var targetDir = 'C:\Program Files (x86)\Notepad++'; // target installer dir

// read all files in the directory
fs.readdir(targetDir, function(err, files) {

    if(!err){
        for(var i = 0; i < files.length; ++i){
            var currFile = files[i];

            console.log(currFile); 
            // ex output: 'C:\Program Files (x86)\Notepad++\notepad++.exe'

            // attempt to print the parent directory of currFile
            var fileDir = getDir(currFile);

            console.log(fileDir);  
            // output is empty string, ''...what!?
        }
    }
});

function getDir(filePath){
    if(filePath !== '' && filePath != null){

       // this will fail on Windows, and work on Others
       return filePath.substring(0, filePath.lastIndexOf('/') + 1);
    }
}

何が起きたのか!?

targetDir はに設定されている部分文字列の指数 0, は、 0 (indexOf('/')-1C:\Program Files\Notepad\Notepad++.exeにおけるアオコの発生などによる空の文字列が返されます。

を解---

このコードは以下のポスト: いる現在のオペレーティングシステムとNode.js

myGlobals = { isWin: false, isOsX:false, isNix:false };

サーバ側で検出はOS.

// this var could likely a global or available to all parts of your app
if(/^win/.test(process.platform))     { myGlobals.isWin=true; }
else if(process.platform === 'darwin'){ myGlobals.isOsX=true; }
else if(process.platform === 'linux') { myGlobals.isNix=true; }

ブラウザ側で検出OS

var appVer = navigator.appVersion;
if      (appVer.indexOf("Win")!=-1)   myGlobals.isWin = true;
else if (appVer.indexOf("Mac")!=-1)   myGlobals.isOsX = true;
else if (appVer.indexOf("X11")!=-1)   myGlobals.isNix = true;
else if (appVer.indexOf("Linux")!=-1) myGlobals.isNix = true;

ヘルパー関数のセパレータ

function getPathSeparator(){
    if(myGlobals.isWin){
        return '\\';
    }
    else if(myGlobals.isOsx  || myGlobals.isNix){
        return '/';
    }

    // default to *nix system.
    return '/';
}

// modifying our getDir method from above...

ヘルパー関数の親ディレクトリ(クロスプラットフォーム)

function getDir(filePath){
    if(filePath !== '' && filePath != null){
       // this will fail on Windows, and work on Others
       return filePath.substring(0, filePath.lastIndexOf(getPathSeparator()) + 1);
    }
}

getDir() ないかを知る。

でも本当のスリックタイヤ、チェックの両方の場合には、ユーザの入力パス経由でコマンドライン等

// in the body of getDir() ...
var sepIndex = filePath.lastIndexOf('/');
if(sepIndex == -1){
    sepIndex = filePath.lastIndexOf('\\');
}

// include the trailing separator
return filePath.substring(0, sepIndex+1);

も利用できます'path'モジュールやパスです。月として、上述したい場合は負荷モジュールに簡単なのです。個人的には、だと思っているのに十分なだけをチェックからの情報のプロセスで用意されています。

var path = require('path');
var fileSep = path.sep;    // returns '\\' on windows, '/' on *nix

ヴィア-フェラータのシーズン全!

登録したの答えが正しいの例外の解析のパスによるファイルの入力インターネットexplorer(試IE8いことを知らないその他のバージョン).この場合、パスの入力要素の値入力します。値のフォーム"C:\fakepath\<filename>".注意バックスラッシュ。

では"/"を使用し、すべてのOSと分かっています。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top