Skip to content

Instantly share code, notes, and snippets.

@jinwei233
Created September 16, 2011 12:47
Show Gist options
  • Save jinwei233/1222051 to your computer and use it in GitHub Desktop.
Save jinwei233/1222051 to your computer and use it in GitHub Desktop.
NodeJs 路径获取 格式转换
/**
获取当前路径 && 路径转换
*/
var cwd = process.cwd();
console.log("-----------------------");
console.log("cwd:",cwd);
console.log("-----------------------");
console.log("-----------------------");
console.log("__dirname:",__dirname);
console.log("-----------------------");
// set absolute path of our jar files
console.log("-----------------------");
var ori = __dirname+"/tools";
var toolPath = posix2dos(ori);
console.log("toolPath:",toolPath);
console.log("-----------------------");
function posix2dos(path){
/*
/cygdrive/d/att -> d:\\att
*/
var re = new RegExp(/^\/cygdrive\/(\w)/);
path = path.replace(re,function(match,drive){
//console.log(drive);
return drive+":";
}).replace(/\//g,'\\');
return path;
}
function dos2posix(path){
var re = new RegExp(/(\w):/);
path = path.replace(re,function(match,drive){
return "/cygdrive/"+drive;
}).replace(/\\/g,"/");
return path;
}
///cygdrive/d/f2e/att_js/test
///d:\f2e\att_js\tools\yuicompressor.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment