Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy file in node.js (including modified time)?

Tags:

node.js

copy

fs

I am able to copy a file in node.js using the following:

            var readStream = fs.createReadStream(fromFilePath);
            readStream.pipe(fs.createWriteStream(toFilePath));

The question is how to also copy/keep the modified time (mtime) like in a regular file copy command.

like image 800
Amit Apple Avatar asked Dec 04 '12 01:12

Amit Apple


People also ask

How do I copy files from one node js file to another?

copyFile() method is used to asynchronously copy a file from the source path to destination path. By default, Node. js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation.

Which of the following will copy a file in node JS?

With Node. js 8.5, a new File System feature is shipped by which you can copy the files using the core fs module.

How do I overwrite a node js file?

To overwrite a file using fs in Node. js, we can call writeFile or writeFileSync with the 'w' flag. to call writeFileSync with the file path , file content , and the 'w' flag to write the file even if it already exists. We can pass in the same arguments with writeFile to write the file asynchronously.

Which method is used to change the timestamp of the file in node JS?

utimesSync() method is used to synchronously change the modification and access timestamps of a file. The timestamps can be specified using a number, string, or Date object.

How to copy a file asynchronously in Node JS?

Last Updated : 16 Aug, 2021 The fs.copyFile () method is used to asynchronously copy a file from the source path to destination path. By default, Node.js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation.

How to move&copy files in NodeJS?

How To Move & Copy Files In NodeJS – Simple Examples 1 To copy a file in NodeJS – require ("fs").copyFileSync ("SOURCE", "TARGET") 2 To move a file in NodeJS require ("fs").renameSync ("SOURCE", "TARGET") More ...

How to modify the behavior of copy operation in Node JS?

By default, Node.js will overwrite the file if it already exists at the given destination. The optional mode parameter can be used to modify the behavior of the copy operation. Parameters: This method accepts three parameters as mentioned above and described below:

What is the use case for copying a file in Node JS?

Well, there are many use cases to this function. You might come across situations where you want to copy data from a file and maybe put it into use later in your code. However, copying a file in Node.js is not the same as reading a file and then writing its contents in another file.


1 Answers

There are methods in the fs module to access mtime:

var stat = fs.statSync(fromFilePath);
fs.utimesSync(toFilePath, stat.atime, stat.mtime)
like image 146
JohnnyHK Avatar answered Sep 22 '22 23:09

JohnnyHK