Read JSON file in a directory and its sub-directories - node.js

I have a file named file.json in the /assets directory and some of its subdirectories as well. Is there a way that I can search the assets directory and read each of the file.json file using the fs.readFileSync method at once?

Not sure if you can do it synch, yet, you can use glob and stream-concat-files. Note the the concat by default is to a file but you can stream it to a a string buffer writeable stream.
glob('asset/**/file.json', function(err, files){concatFiles(files, 'concat.json', function(err){})});

Related

NodeJS - Find a given file by name and ext regardless of location, from inside a working directory (Closed)

How do I search for a specific file by name and extension in a working directory, regardless of the file's path from inside that working directory?
For example, if we are searching for foo.txt inside our first working directory, but it's located within a file path like the example below
workDir/folder2/is/foo/directory/foo.txt
Then say for further example, we want to then search for foo.txt inside our second working directory, but the file is located within a different file path like below
workDir/folder11/this/way/to/foo.txt
How would I find foo.txt regardless of the file's path, inside a working directory?
I am Using the below JavaScript code in my NodeJS project's controller.js file to try and achieve this.
NOTE: In real-time, every working directory this would be used on, would be named differently
controller.js
var fooPath = GetFooPath(data, path.join(workDir, "folder11/"));
if (fooPath == -1) {
console.log("Cannot find the foo file!");
return;
}
You could use globbing
glob("absolute/path/to/workDir/**/*.txt", options,
function (error, files) {
// files is an array of filenames.
// If the `nonull` option is set, and nothing
// was found, then files is ["**/*.js"]
// error is an error object or null.
})
NPM glob

shutil.make_archive not zipping to correct destination

As per the code below I am having issues with the zipping a directory using the python 3 shutil.make_archive function. The .testdir will be zipped but it is being zipped in /home/pi, instead of /home/pi/Backups.
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(zip_loc, 'zip', zip_dest)
Could anyone explain what I am doing wrong?
Reading the docs here I came up with:
zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(base_dir=zip_loc, root_dir=zip_loc, format='zip', base_name=zip_dest)
From the docs:
base_name is the name of the file to create, including the path, minus any format-specific extension.
root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.
base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.
root_dir and base_dir both default to the current directory.
Before to write the archive, move to the good directory :
old_path = os.getcwd()
os.chdir(path)
-> write the archive
After writing the archive move back to old directory :
os.chdir(old_path)

ParaView get file path

I am opening some VTU files from Directory X and there are other output files in that directory (for example log.txt) that I want to open via a plugin. If I do a os.getcwd() I end up in ParaViews installation directory. What I want is the directory of the VTU files I loaded BEFORE applying the plugin... So basically the start Point of the Pipline.
You could do something like this to get the reader
myreader = FindSource('MyReader')
then get the file name via the FileName attribute
myreader.FileName

node-archiver destination folder

I'm using node-archiver to zip some files and in the zip file I find the archived files under the directory where was zipped from.
So in the zip file, 'myfile.txt' is under this sort of directory:
'home/ubuntu/some_folder/another_folder/'
I want to have the files in the root directory of the zip file.
This is the code I suspect that is in charge with this:
archive.bulk(
{src: filePaths}
);
Its documentation is here although I don't see how to do it.
Any ideas?
Just add expand:true, flatten:true and indicate what name you want to use for destination folder (dest: 'destinationfoldername/')
archive.bulk(
{src: filePaths, dest:'destinationfoldername/', expand:true, flatten:true}
);
It workerd for me

What is the file extension of a file called "foo.tar.bz2"?

Considering the file named:
foo.tar.bz2
What is the file extension? Is it .tar.bz2 or .bz2? Is it well defined?
Edit: The question here is one of the definition of a "file extension", or where the separation is between the file's name and its extension: is it "foo|.tar.bz2" or "foo.tar|.bz2"
The standard file extension would be .tar.bz2, but .tbz2 should suffice as a shortened extension.
tar - is archive file
bz2 - is compressed with bzip
to unarchive and get all files you should type in command line unix:
tar jxf foo.tar.bz2
after that you will have files unarchived and extracted
extension is the last one .bz2

Resources