I have a function that writes a file to a directory:
response.pipe(fs.createWriteStream(fullPath))
But before that I want to check if the path already exist, and if so, add a suffix, e.g. file_1.txt
(and if that exist, create file_2.txt
instead...etc):
// Check if the path already exist
let fullPath = "C:/test/file.txt"
let dir = "C:/test/"
let fileName = "file"
let fileExt = ".txt"
if (fs.existsSync(fullPath)) {
// I tried using a while loop but I end up making it too complicated
...
}
// Write file to disk
response.pipe(fs.createWriteStream(destinationPath))
How do I properly / efficiently do that?
The while
loop is the correct way.
// Check if the path already exist
let fullPath = "C:/test/file.txt"
let dir = "C:/test/"
let fileName = "file"
let fileExt = ".txt"
let num = 0;
while (fs.existsSync(fullPath)) {
fullPath = `${dir}${fileName}_${num++}${fileExt}`;
}
After this, fullPath
contains the first nonexistent file.
Note that there's a potential race condition. Some other process could create the file after your loop finishes.
If all of your file names are named the same thing + "_#".txt, I think the most (one of the most) efficient ways to check that would be something along the lines of:
Get all files from the directory
var files = [];
fs.readdir(dir, (err, files) => {
files.forEach(file => {
files.push(file);
});
})
You would then sort the array (could be expensive if a lot of files)... then last record would be the highest number which you can easily extract.
Another thing you could do is find the file which has the latest creation date using similar approach using the Stats class from FS.
I have the following markup: <li data-group="bottoms">Pants</li> <li data-group="bottoms">Pants</li> <li data-group="bottoms">Pants</li> <li data-group="tops"&...
I have the following markup: <li data-group="bottoms">Pants</li> <li data-group="bottoms">Pants</li> <li data-group="bottoms">Pants</li> <li data-group="tops"&...
I am writing an Android app that requires access to a MySql server. Following examples I have found, I have a php page that sits on the server and does the MySql stuff, and I try to access that from ...
I am writing an Android app that requires access to a MySql server. Following examples I have found, I have a php page that sits on the server and does the MySql stuff, and I try to access that from ...
I have a map like this for example const Map = new Map().set('123', [ [ 'foo', 'bar' ] ]).set('456', [ [ 'baz', 'qux' ], [ 'quux', 'corge' ] ]); /* The structure of the Map looks like this: Map {...
I have a map like this for example const Map = new Map().set('123', [ [ 'foo', 'bar' ] ]).set('456', [ [ 'baz', 'qux' ], [ 'quux', 'corge' ] ]); /* The structure of the Map looks like this: Map {...
I have a navbar which needs to change colour on scroll. This functionality was originally built with js, however now it has a 'Login' button which is built in elm to perform other functionality. I ...
I have a navbar which needs to change colour on scroll. This functionality was originally built with js, however now it has a 'Login' button which is built in elm to perform other functionality. I ...