1

I want to creat directory if not exist and then save the file that I got from Front-end

This is my code and it only does the part of creating folders, but my main problem is saving the file in this path

let directory = await './some-dir/'+d.getFullYear()+'/'+("0" + (d.getMonth() + 1)).slice(-2)+'/'+(d.getDay().toString().padStart(2, "0"))+'/'+d.getHours()+'/'+d.getMinutes()+'/'+d.getSeconds()
    fs.mkdirSync(directory, { recursive: true })
4
  • 2
    what is the problem with saving the file in the path? the code you've give does create the directories. i think you forget to post part of your code Commented Jan 24, 2023 at 15:49
  • 1
    fs-extra has ensureDir. Commented Jan 24, 2023 at 16:10
  • Tangential, but a ~200-char line is... difficult to think about. And the spurious await is deceptive. Commented Jan 24, 2023 at 16:10
  • What is the actual problem? What's preventing the file from being saved? Commented Jan 24, 2023 at 16:12

3 Answers 3

2

If we want to create a file in a folder (both of them do not exist), we can use this way. Of course, in this example, the path of the file (folders) is first the number of the current year, then the number of the current month, and finally the current day For the file name, the time of file creation is considered as the name The value inside the file is also sent by the front-end request.body.values[0]

import fs from 'fs';
import path from 'path';

let d= new Date()
        const directory = await './some-dir/'+d.getFullYear()+'/'+("0" + (d.getMonth() + 1)).slice(-2)+'/'+(d.getDay().toString().padStart(2, "0"))
        let name= (d.getHours() + '-' + d.getMinutes() + '-' + d.getSeconds()).toString()+".txt"
        fs.mkdirSync(directory, { recursive: true })
        fs.writeFileSync(path.join(directory,name), request.body.values[0],"UTF8")

Sign up to request clarification or add additional context in comments.

Comments

1

The following code should help:

fs.access("./directory-name", function (error) {
    if (error) {
        let directory = './some-dir/' + d.getFullYear() + '/' + ("0" + (d.getMonth() + 1)).slice(-2) + '/' + (d.getDay().toString().padStart(2, "0")) + '/' + d.getHours() + '/' + d.getMinutes() + '/' + d.getSeconds()
        fs.mkdirSync(directory, { recursive: true })
    } else {
        // directory exists.
    }
})

1 Comment

From docs on code (internal): Using fsPromises.access() to check for the accessibility of a file before calling fsPromises.open() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.
0

The fs library has built-in support for making a directory only if it doesn't exist, so this can be done quite concisely. Also, it's not ideal to use the synchronous version fs library in an async function. Asychronoys code is more friendly to the javascript threading model (ie, it performs better).

const fs = require("fs/promises")

await fs.mkdir(directory, { recursive: true }) //  Calling fsPromises.mkdir() when path is a directory that exists results in a rejection only when recursive is false.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.