[英]Can't access files on network drive using FS npm module nodejs
I have a network drive mapped to X:\\ and for some reason I cannot access the files in it using the FS module.我有一个映射到 X:\\ 的网络驱动器,由于某种原因,我无法使用 FS 模块访问其中的文件。 I tried using fs.existsSync(file) and it returns that no files can be found but when I type out the path fs.existsSync('X:\\path\\to\\file') it works fine.我尝试使用 fs.existsSync(file) 并返回找不到文件但是当我输入路径 fs.existsSync('X:\\path\\to\\file') 它工作正常。 Both file and the string I hardcoded are exactly the same, so why is fs returning different results for both?我硬编码的文件和字符串完全相同,那么为什么 fs 为两者返回不同的结果? I have to use the "file" variable because I am looping through an array of file paths to create them in my network drive if they do not exist.我必须使用“文件”变量,因为如果它们不存在,我将循环遍历文件路径数组以在我的网络驱动器中创建它们。 I have also accounted for the backslashes, and replaced them all with double backslashes so the string is not being escaped.我还考虑了反斜杠,并将它们全部替换为双反斜杠,这样字符串就不会被转义。
//This does not work
let modifiedfilePathToSiger = path.join('X:\\', filePathToSiger.slice(17));
let mps = JSON.stringify(modifiedfilePathToSiger)
//Remove double quotes from mps
let file = mps.replace(/['"]+/g, '') //Ex: file here = X:\\Data\\Aerial Photo.docx
let found = fs.existsSync(file);
console.log(found) //Returns false
//This does work
let found = fs.existsSync('X:\\Data\\Aerial Photo.docx');
console.log(found) //Returns true
One other thing to note, if I type out the whole hardcoded path into fs.existsSync() it works fine and returns true, but if I copy and paste the path that is logged from the console, it returns false, albeit the exact same path... not 1 character is different.另一件事要注意,如果我将整个硬编码路径输入 fs.existsSync() 它工作正常并返回 true,但如果我复制并粘贴从控制台记录的路径,它返回 false,尽管完全相同路径...不是 1 个字符是不同的。
Looks like when I log file variable it returns X:\\Data\\Aerial Photo.docx but when I log the hardcoded string X:\\Data\\Aerial Photo.docx it returns X:\\Data\\Aerial Photo.docx看起来当我记录文件变量时它返回 X:\\Data\\Aerial Photo.docx 但是当我记录硬编码字符串 X:\\Data\\Aerial Photo.docx 时它返回 X:\\Data\\Aerial Photo.docx
Make use of .split
and path.win32
then path.join
利用.split
和path.win32
然后path.join
From Node REPL从节点 REPL
> path = require('path').win32
<ref *1> {
resolve: [Function: resolve],
normalize: [Function: normalize],
isAbsolute: [Function: isAbsolute],
join: [Function: join],
relative: [Function: relative],
toNamespacedPath: [Function: toNamespacedPath],
dirname: [Function: dirname],
basename: [Function: basename],
extname: [Function: extname],
format: [Function: bound _format],
parse: [Function: parse],
sep: '\\',
delimiter: ';',
win32: [Circular *1],
posix: <ref *2> {
resolve: [Function: resolve],
normalize: [Function: normalize],
isAbsolute: [Function: isAbsolute],
join: [Function: join],
relative: [Function: relative],
toNamespacedPath: [Function: toNamespacedPath],
dirname: [Function: dirname],
basename: [Function: basename],
extname: [Function: extname],
format: [Function: bound _format],
parse: [Function: parse],
sep: '/',
delimiter: ':',
win32: [Circular *1],
posix: [Circular *2],
_makeLong: [Function: toNamespacedPath]
},
_makeLong: [Function: toNamespacedPath]
}
> strArr = file.split('\\')
[ 'X:', 'Data', 'Aerial Photo.docx' ]
> path.join(strArr[0],strArr[1],strArr[2])
'X:\\Data\\Aerial Photo.docx'
>
it will take care of the slashes respective to environment它将处理与环境相关的斜线
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.