18

I try to copy a font with nodejs v14.17.0 to %localappdata%\Microsoft\Windows\Fonts , when try with prompt I don't have problem

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

but when try with nodejs, I've this issue

[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'
}

This is my code

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 ");
});

How to solve it ?

2
  • When you copy the file using prompt, did you run the prompt as administrator ? Jun 26, 2021 at 16:51
  • 1
    @ĐăngKhoaĐinh I use Hyper as prompt but with current user (no administrator )
    – salsan
    Jun 26, 2021 at 17:17

2 Answers 2

10

In according with official documentation fs.copyFile

  • src <string> | <Buffer> | <URL> source filename to copy
  • dest <string> | <Buffer> | <URL> destination filename of the copy operation

In dest is required destination filename and not only destination directory.

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

In this case dest = 'locallAppdata+'\\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 ");
 });
1
  • Permission error on a malformed destination path is kind of evil, especially when it's legitimate to think that the destination folder should be enough (as the file could keep it's old name). Anyways, thanks !
    – Akheloes
    Jan 31 at 15:51
0

This is how to recursively copy files from one directory to another

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);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.