Node readdirSync and getting paths dynamically - node.js

I have a directory whose contents shrink and grow. I am trying to run a program that will list all the directories.
Is there a way for fs.readdirSync to be fed a path like ./src/images/*/*/* and it would list all the paths within that wildcard structure?
The only alternative I can see is creating some complex nested loops.

Related

Can Pathlib iterdir be used to recursively iterate through a directory structure like os.walk?

I know that glob exists for this purpose and one can also use os.walk to get a list of all subdirectories and files inside a folder and use this to create a tree of all files and folders that exist inside a root directory. However, can one also use the Pathlib iterdir function to recursively iterate through all subdirectories inside a folder and store the paths to all files and the filenames like we can do so easily with os.walk?
I am asking this question since as per my knowledge, the Pathlib actually presents better way to do things already done by os and os.path.

Flag to Create Missing Directories During fs.promises.writeFile

As I review these file system flags, I'm I correct in concluding that there is no flag you can pass to fs.promises.writeFile that will automatically create all missing directories leading up to a filename? If not, which flag does this?
I don't like solutions that check for the existence of the folders first before attempting writeFile, because after the folders are created that check happens every time you write to a file in that folder.
In my program, after the folders are created once, it should always be there, so it seems more efficient to only create the folders if there is an exception. However, I'm hoping there is a flag that avoids all this micro-management.
If a flag for auto-creating the folders doesn't exist for writeFile, then I'd like to attempt writeFile first, and then (only if there is an exception) create the folders recursively.
fs.promises.writeFile() does not automatically create the directory structure for you. That must exist first.
If you want to automatically create the path because you received an error indicative of a path problem, you can use fs.promises.mkdir() and pass the recursive flag.
And you could, of course, create your own wrapper function that calls fs.promises.writeFile() and if it gets whatever error you get when the path doesn't exist (you'd have to test to see exactly what that error is), then call fs.promises.mkdir() and then repeat the fs.promises.writeFile(). It could all be wrapped in your own utility function.

MATLAB, Octave: working with folder names that have space in them

In MATLAB, actually Octave, I would like to find a list of all subfolders in the current folder so I use this:
subFolder = dir;
This gives the list of all subfolders in the current folder. This returns a structure whose one element is the name. Assume I have two subfolders with names subfolder 1A and subfolder 1B.
Now I want to go to these folders. Then I do this:
cd subFolder(1).name
But I get this error:
error: subFolder(1).name: No such file or directory
If I do this:
cd "subfolder 1A"
everything works fine. What is the solution?
The space in the folder name is a red herring in this case. It's not the source of the problem. The actual issue is that you need to call the cd function using function syntax instead of command syntax (i.e. use parentheses; related question here):
cd(subFolder(1).name);
When you use the command syntax, subFolder(1).name is itself being treated as the string argument to cd (i.e. it's looking for a folder called 'subFolder(1).name'). With the function syntax, the string contained within the structure array field is used as the argument.
To make your code a little more robust, you could also use the 'folder' field returned by dir:
cd(fullfile(subFolder(1).folder, subFolder(1).name));
This will go to the desired folder regardless of the directory you are currently in, since it specifies an absolute path instead of a partial path (which is relative to the current directory).

Better way of inserting directories in Haskell modules

I am writing a (actually my first) largish program in Haskell and am trying to break up my program into modules, so that it might be easier to maintain. In Haskell, the module names seem to be dependent upon the directory structure. This is perfectly fine till I have to embed another layer of directory structure for the program. Ill give a very simple example below:
Let us say that we are starting out with a Haskell program with the following directory structure.
Names in [square brackets] represent directories,
names in {curly braces} represent modules,
names in (normal brackets) represent file names associated within the modules
[src]
- (Main.hs) {Main}
- (PRBS.hs) {PRBS}
- [Hardware]
- (DataDef.hs) {Hardware.DataDef}
- (ShiftRegister.hs) {Hardware.ShiftRegister}
This is all fine. I can import everything I want wherever I want it. However, now say I want to create another level of abstraction, like so:
[src]
- (Main.hs) {Main}
- [Firmware]
- (PRBS.hs) {Firmware.PRBS}
- [Hardware]
- (DataDef.hs) {Firmware.Hardware.DataDef}
- (ShiftRegister.hs) {Firmware.Hardware.ShiftRegister}
Notice now that the names of all modules in Hardware have changed. I now have to change the module names within each file, and in all the other files where the files are imported. The three files I have shown in just an example. If the directory structure ends up having hundreds of files with tens of embedded directories, then it might become impossible to maintain code. Furthermore, if at any point, I want to copy a directory (and its subdirectories at a particular point in the current directory system), I need to figure out all the previous directories that come before it, and manually change the module names within each file.
Since Haskell is used in several large projects, I am very certain that I am missing something here. Any help in getting me out of this quagmire will be greatly appreciated!
If you have perl and bash and find and all that, you can do something like this:
find Firmware/Hardware/ -name '*.hs' -exec perl -i.bak -pe \
's/^module (Hardware\.\S+)/module Firmware.$1/' \
{} \;
diff Firmware/Hardware/DataRef.hs{,.bak}

Implementing FUSE

I want to implement a file system using FUSE. When the contents of the directory is requested, only the types of files in that directory will be reported as subdirectories. For example, if there are ugur.PDF, guler.JPG and devatate.PNG files in the directory, the files themselves will not be reported, but types (PDF, JPG and PNG) will be reported instead. I tried to implement this file system. My problem is i can not imagine that how can i report without changing ls-l command? how does ls-l command work? (I have a readdir function and it loads file_type and file_name to buffer. I tried to change but i couldn't achive)
How does ls -l work? That seems to be the crux of the issue. It calls fstat(2). That system call fills the stat structure. When you implement a fuse provider, you are providing the data that ends up in the stat structure. If you want to change the directory structure, you return the necessary fabricated information for the various items.
I can think of two approaches to this:
1. Use a database like SQLite
you can store the files, path and file types in the database. Then when the user gets into some directory, you can say do some query like select file_types where path="" and populate as directories using filler()
2. Recursively traverse original path
you can create a list of all file types in the current directory, then use filler() to post them as directories. Then, when user enters any one directory, you can again do a check or something to see if cur_path is in last_path(orig directory), and you can select those file types and display them

Resources