How to delete all txt files within directory NodeJs - node.js

I need to delete only the txt files within a directory (multiple levels). I'd hazard a guess that it's possible with fs-extra...
https://github.com/jprichardson/node-fs-extra
But I can't find a solution without specifying the individual file name.
I was hoping to solve it with the following...
fse.remove('./desktop/directory/*.txt')
but unfortunately the asterisk wouldn't select all... as I then could have done something like the following...
fse.remove('./desktop/directory/sub1/*.txt')
fse.remove('./desktop/directory/sub1/sub2/*.txt')
fse.remove('./desktop/directory/sub1/sub2/sub3/*.txt')
fse.remove('./desktop/directory/sub1/sub2/sub3/sub4/*.txt')
Not the cleanest I know... But it's all I've got.
Any help or suggestions on this would be appreciated. Thanks.

If you are using rimraf, You can try this.
const rimraf = require('rimraf');
rimraf.sync('**/*.txt');
rimraf accept glob as the first parameter.
If you want to use it asynchronously, you can even write
rimraf('**/*.txt', options, () => {
console.log('deleted')
})
Though fs-extra uses rimraf internally to delete the file.

What about this?
fse.remove('./desktop/directory/**/*.txt')
Adding ** means to include all sub-directories

Related

Reading whole directory with Spark except one file

I have the following directory containing these CSV files:
/data/one.csv
/data/two.csv
/data/three.csv
/data/four.csv
If I want to read everything, I can simply do:
/data/*.csv
but I can not seem to read everything, except four.csv.
This:
/data/*[^four]*.csv
seemed to work but I think that if the list of files would be bigger, than this way of reading would probably be wrong (because of double wildcards).
Is there a good way to do this? I also am aware that:
/data/{one,two,three,^four}.csv
would solve this specific case, but I need the except method for future needs.
Thank you very much!
I am not 100% sure that this method will work, but you can try. You can use Bash/Python or whatever script to scan all the csv files in the folder, but not with the names four.csv.
The input for spark will be (assuming you have files: one.csv, two.csv, three.csv, four.csv, five.csv ,...up to n.csv files)
PathToFiles=[/data/one.csv, /data/two.csv, /data/three.csv, /data/five.csv, ..., /data/n.csv]
Then you can use (the code is in python)
filesRDD = spark.sparkContext.wholeTextFiles(",".join(PathToFiles))
I have wrote similar code in java, in my impression, it works and you can try.

Node : What is the right way to delete all the files from a directory?

So I was trying to delete all my files inside a folder using node.
I came across 2 methods .
Method 1
Delete the folder using rmkdir. But if I plan on adding the images on the same folder then I use mkdir and creates the same folder again and appends the files to it.
Example: I have an Add Files and Delete ALL button. When I click deleteAll , the folder gets deleted. And when I click add then the folder gets created and the file gets added to that folder
Method 2
Using readdir , I loop through the files and stores in an array and then delete only the files instead of the folder.
Which is the best way to do it ? If its not among these then please advice me a better solution.
The rm function of ShellJS will do the trick. It works as a one-liner, and it works cross-platform, and is well tested and documented. It even supports recursive deletes.
Basically, something such as:
const { rm } = require('shelljs');
rm('-rf', '/tmp/*');
(Sample code taken from ShellJS' documentation.)

ElectronJS - adding folder to favorites [macOS]

I am wondering if there is a way to add a folder to Favorites using Node?
I've found this issue, but the solution does not work anymore.
Basically the file that I'm interested in is located in:
/Users/USER_NAME/Library/Application\ Support/com.apple.sharedfilelist
The thing is I do not know how to modify it...
You can use try os module. Very easy to use.
Here is an example:
const home = require("os").homedir(); // This will get your OS based Home directory
const dirToSave = `${home}/Desktop/output.csv`; // Now you can add file or folder to any tree directory of home directory.
fs.writeFile(`${dirToSave}`, {csvData}, function(err, stat) {
if (err) throw err;
console.log("file saved");
});
There is a file that you need to edit
You can edit the sidebar on Mac using the com.apple.sidebarlists.plist preference file. The items will be in the favoriteitems dictionary.
The items you see are all set to being AlwaysVisible. You need to edit that file to add your own file.
I will give some links that might help you.
About the com.apple.sidebarlists.plist file, http://www.thexlab.com/faqs/finder.html
Finding the com.apple.finder.plist, https://discussions.apple.com/thread/4122582
Another post, https://apple.stackexchange.com/questions/139305/how-can-i-add-new-folders-to-the-favorites-in-the-finder-sidebar
About editing a file in Electron
There are tutorials about how to edit files with Electron, you can start from this example,
https://ourcodeworld.com/articles/read/106/how-to-choose-read-save-delete-or-create-a-file-with-electron-framework
What I've done is I wrote a python script based on the resource that #Royson posted in this question's comments (click).
It's available under a gist:
https://gist.github.com/Ancinek/6d6e34791c5a8674275560ae118848c7
Currently possible to add/remove one file at a time.
I will be creating a binary for this so it can be run without the need to installing the pyobjc on the user's machine - will post the update soon after I find out how to actually do this.
Hope this helps somebody!

Can't find mongoose module NodeJS require

for some reason I can't get my require to work, it should just find both models fine but the path for the file just won't work.
app
models
user.js
match.js
server.js
Seems like a simple fix but cannot seem to do it myself right now. I'm using (var User = require('/app/models/user.js');)
Thanks guys.
Actually you also can write it as,
var User = require('./app/models/user')
without even putting .js at the end, as it is added by default.. Just a tip to know :)
and here ./ means the current directory.
Hope it helps :)

Add a Timestamp to the End of Filenames with Grunt

During my Grunt tasks, add a unique string to the end of my filenames. I have tried grunt-contrib-copy and grunt-filerev. Neither have been able to do what I need them to...
Currently my LESS files are automatically compiled on 'save' in Sublime Text 3 (so this does not yet occur in my grunt tasks). Then, I open my terminal and run 'grunt', which concatenates (combines) my JS files. After this is done, then grunt should rename 'dist/css/main.css' and 'dist/js/main.js' with a "version" at the end of the filename.
I have tried:
grunt-contrib-copy ('clean:expired' deletes the concatenated JS before grunt-contrib-copy' can rename the file)
grunt-filerev ('This only worked on the CSS files for some reason, and it inserted the version number BEFORE the '.css'. Not sure why it didn't work on the JS files.')
Here's my Gruntfile.js
So, to be clear, I am not asking for "code review" I simply need to know how I can incorporate a "rename" process so that when the tasks are complete, I will have 'dist/css/main.css12345 & dist/js/main.js12345' with no 'dist/css/main.css' or 'dist/js/main.js' left in their respective directories.
Thanks in advance for any help!
UPDATE: After experimenting with this, I ended up using grunt-contrib-rename and it works great! I beleieve the same results can be achieved via grunt-contrib-copy, in fact I know it does the same thing. So either will work. As far as support for regex, not sure if both support it, so may be something else worth looking into before choosing one of these plugins :)
Your rename:dist looks like it should do what you want, you just need to move clean:dist to be the first task that runs (so it deletes things from the prior build rather than the current build). The order of tasks is defined by the array on this last line:
grunt.registerTask('default', ['jshint:dev', 'concat:dist', 'less:dist', 'csslint:dist', 'uglify:dist', 'cssmin:dist', 'clean:dist', 'rename:dist']);
That said, I'm not sure why you want this behavior. The more common thing to do is to insert a hash of the file into the filename before the file extension.
The difference between a hash and a timestamp is that the hash value will always be the same so long as the file contents don't change - so if you only change one file, the compiled output for just that file will be different and thus browsers only need to re-downloaded that one file while using cached versions of every other file.
The difference between putting this number before the file extension and after the extension is that a lot of tools (like your IDE) have behavior that changes based on the extension.
For this more standard goal, there are tons of ways to accomplish it but one of the more common is to combine grunt-filerev with grunt-usemin which will create properly named files and also update your HTML file(s) to reference these new file names
I'm not sure to understand completely what end you want, but if you add a var timestamp = new Date().getTime(); at the beginning of your gruntfile and concatenate to your dest param that should do the job.
dest: 'dist/js/main.min.js' + timestamp
Is it what your looking for?

Resources