windows Node.js复制文件EPERM:不允许操作

5w9g7ksd  于 2023-08-07  发布在  Windows
关注(0)|答案(2)|浏览(169)

我尝试复制字体与nodejs v14.17.0%localappdata%\Microsoft\Windows\Fonts,当尝试与提示我没有问题

copy /B "Haloha Free Trial.ttf" /V %localappdata%\Microsoft\Windows\Fonts\
    1 file(s) copied.

字符串
但是当尝试使用nodejs时,我遇到了这个问题

[Error: EPERM: operation not permitted, copyfile 'D:\dev\test\javascript\font\Haloha Free Trial.ttf' -> 'C:\Users\omen\AppData\Local\Microsoft\Windows\Fonts'] {
  errno: -4048,
  code: 'EPERM',
  syscall: 'copyfile',
  path: 'D:\\dev\\test\\javascript\\font\\Haloha Free Trial.ttf',
  dest: 'C:\\Users\\omen\\AppData\\Local\\Microsoft\\Windows\\Fonts'
}


这是我的代码

let options = process.argv.slice(2);
console.log(options[0]);

console.log(process.env.LOCALAPPDATA);
const locallAppdata = process.env.LOCALAPPDATA;

const fs = require('fs');

fs.copyFile( options[0], locallAppdata+'\\Microsoft\\Windows\\Fonts\\', (err) =>{
    if(err) throw err;
    console.log( argv[0] + " was copied ");
});


如何解决?

whlutmcx

whlutmcx1#

根据官方文档fs.copyFile

  • SRC||源 文件名复制
  • 目的地||复制操作的目标文件名

dest中,不仅需要目标目录,还需要目标文件名**。

fs.copyFile(src, dest[, mode], callback)

字符串
在这种情况下,dest='localAppdata +'\Microsoft\Windows\Fonts\'+options[0]

let dest = locallAppdata+'\\Microsoft\\Windows\\Fonts\\' + options[0];
let src =  options[0];
    
 fs.copyFile( src, dest, (err) =>{
     if(err) throw err;
             console.log( options[0] + " was copied ");
 });

qnakjoqk

qnakjoqk2#

将文件从一个目录递归复制到另一个目录

import { copyFileSync, mkdirSync, readdirSync, statSync } from 'fs';
import { join } from 'path';

const SOURCE_PATH = `libs/models/src/lib/template/sample`;
const TARGET_PATH = SOURCE_PATH.replace('sample', 'product');
const DIRS = readdirSync(SOURCE_PATH);

function isFile(target: string) {
  return statSync(target).isFile();
}

function copyFile(source: string, target: string) {
  copyFileSync(source, target);
  return;
}

function copyFiles(source: string, target: string) {
  const dirs = readdirSync(source);
  for (const d of dirs) {
    if (isFile(join(source, d))) {
      copyFile(join(source, d), join(target, d));
    } else {
      mkdirSync(join(target, d));
      copyFiles(join(source, d), join(target, d));
    }
  }
}

copyFiles(SOURCE_PATH, TARGET_PATH);

字符串

相关问题