I want to delete an empty/non-empty directory from my local system and i have restriction that i need to do that only using javascript functions.I cannot refer to functions like 'FileSystemObject'
Please help!
Please Note:
I need solution for above issue as I am working on mobile app which support javascript only
This function should work. Removes files synchronously
Pass removeself = true to remove the empty directory.
const path = require('path');
const fs = require('fs');
const rmDir = function (dirPath, removeSelf) {
if (removeSelf === undefined)
removeSelf = true;
try {
var files = fs.readdirSync(dirPath);
} catch (e) {
// throw e
return;
}
if (files.length > 0)
for (let i = 0; i < files.length; i++) {
const filePath = path.join(dirPath, files[i]);
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath);
else
rmDir(filePath);
}
if (removeSelf)
fs.rmdirSync(dirPath);
};
Disclaimer: Not my code, copied from someone's gist(forgot the url).
Related
I am not able to get the image path in javascript, below is the code
for (let i = 0; i < 10; i++)
{
const themeButton = document.createElement('button')
if (i === 0)
{
themeButton.style.backgroundImage = "url('../assets/Pngs/ThemesIcon/NewTheme.png')"
//themeButton.style.backgroundImage = `url(https://i.postimg.cc/wMT2jLG7/Group-4807.png)`
}
else
{
themeButton.style.backgroundImage = "url('../assets/Pngs/ThemesIcon/NewTheme.png')"
}
themeButton.classList.add('colorBtn')
themeButton.style.backgroundRepeat = 'no-repeat'
themeButton.style.backgroundSize = '374px 180px'
container.appendChild(themeButton)
}
if i use url it is working for local path it is not working.
themeButton.style.backgroundImage = `url(https://i.postimg.cc/wMT2jLG7/Group-4807.png)`
Folder structure is:
App
Files
assets
Files/uiController.js --> i am accessing image
assets/Pngs/ThemesIcon/Theme1.png --> image folders
According to the folders structure, you provided.
You need backward by one folder with ../.
So the path will be
themeButton.style.backgroundImage = "url('../assets/Pngs/ThemesIcon/Theme1.png')"
I have a "clients.txt" file where I have a list of emails. I try to run a program for sending emails where I chose a number of emails to use from the file, in that case the number is 2. After I use the two emails I want to overwrite "clients.txt" without them. The problem is when I try to run the code just for one single time every thing is working! but if I make a loop something is wrong. Looking forward to see any help from you guys. Thanks! I add the code bellow. PS: Sorry for my bad english!
function readEmails(){
const fs = require('fs');
clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
let filtered = clients_list.filter(function (el) {
return el != null && el != '';
});
return filtered
}
function dump_array(arr, file){
let fs = require('fs');
let file = fs.createWriteStream(file);
file.on('error', function(err) { /* error handling */ });
arr.forEach(function(v) { file.write(v + '\n'); });
file.end();
}
while_var = 0;
while (while_var < 2){
while_var ++;
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}
const fs = require('fs');
function readEmails(){
const clients_list = fs.readFileSync('clients.txt', 'utf8').split('\n');
const filtered = clients_list
// clear false, 0 and undefined too
.filter(el => !!el)
// remove extra spaces and \r symbols
.map(el => el.trim());
return filtered;
}
function dump_array(arr, file){
// Here you need sync method.
fs.writeFileSync(file, arr.join('\n'));
// And here was 'already declared' error in orginal code
}
let while_var = 0;
while (while_var++ < 2){
let all_clients = readEmails();
let selected_clients = [];
if (all_clients.length > 0){
selected_clients = all_clients.splice(0,2);
dump_array(all_clients, 'clients.txt');
console.log(selected_clients);
}else{
console.log('No more clients')
}
}
The code listed below searches for files that contains a specified string under it's directory/subdirectories.
to activate it, you type node [jsfilename] [folder] [ext] [term]
i would like to change it so it will search without the base folder, i don't want to type ./ , just node [jsfilename] [ext] [term]
so it already know to search from it's location.
i know it has something to do with the process.argv but it need a hint what should i do.
PS:.
I already tried to change the last raw to :
searchFilesInDirectory(__dirname, process.argv[3], process.argv[2]);
it giving me noting...
const path = require('path');
const fs = require('fs');
function searchFilesInDirectory(dir, filter, ext) {
if (!fs.existsSync(dir)) {
console.log(`Welcome! to start, type node search [location] [ext] [word]`);
console.log(`For example: node search ./ .txt myterm`);
return;
}
const files = fs.readdirSync(dir);
const found = getFilesInDirectory(dir, ext);
let printed = false
found.forEach(file => {
const fileContent = fs.readFileSync(file);
const regex = new RegExp('\\b' + filter + '\\b');
if (regex.test(fileContent)) {
console.log(`Your word has found in file: ${file}`);
}
if (!printed && !regex.test(fileContent)) {
console.log(`Sorry, Noting found`);
printed = true;
}
});
}
function getFilesInDirectory(dir, ext) {
if (!fs.existsSync(dir)) {
console.log(`Specified directory: ${dir} does not exist`);
return;
}
let files = [];
fs.readdirSync(dir).forEach(file => {
const filePath = path.join(dir, file);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory()) {
const nestedFiles = getFilesInDirectory(filePath, ext);
files = files.concat(nestedFiles);
} else {
if (path.extname(file) === ext) {
files.push(filePath);
}
}
});
return files;
}
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
If I get what are you trying to achieve. You can do so by slightly changing your function call in the last line.
Change
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
to
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
Edit
As #Keith said in comments use process.cwd() to get the current working directory instead of __dirname
If you want it to work for both conditions then you need to do a conditional check...
if(process.argv.length === 5){
searchFilesInDirectory(process.argv[2], process.argv[4], process.argv[3]);
}else if(process.argv.length === 4){
searchFilesInDirectory(process.cwd(), process.argv[3], process.argv[2]);
}else{
throw new Error("Not enough arguments provided..");
}
I have a file , in javascript , that find all the directories that match the parameter.
And i got this error:
my code:
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}
var home_path=getUserHome();
var findit = require('findit'),
path = require('path'),
finder = findit(path.resolve(home_path));
var myArgs = process.argv.slice(2)[0];
var filter1 = new RegExp(myArgs);
//This listens for directories found
finder.on('directory', function (dir) {
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
(My code is a mess, i will clean it later)
How to fix that?
Why you didn't user the fs from Node, and look for dirs recursively? I think the error should be on the findit module...
That a look on https://nodejs.org/api/fs.html#fs_fs_readdir_path_callback or try instead the https://www.npmjs.com/package/recursive-readdir that also does it. I think that the things you use from the module findit, will be available there (like ways to ignore files)...
EDIT1: Example using recursive-readdir:
var recursive = require('recursive-readdir');
var filter1 = new RegExp(myArgs);
function ignoreFunc(file, stats) {
return !(stats.isDirectory() && filter1.test(path.basename(file)));
}
recursive('directory', [ignoreFunc] ,function (err, files) {
// Files is an array of filename (only the ones that matched the condition)
console.log(files);
});
#Moran, can you add a console.log directly in the callback of you "directory" event ?
finder.on('directory', function (dir) {
// Here
console.log(dir);
var directories = dir.split('\\');
var last= directories[directories.length-1].toLowerCase();
if(filter1.test(last)){
console.log('Directory: ' + dir );
}
});
To see what directory is problematic ? Then compare the rights applied on this folder and a directory that work, as "comverse" for example. It would help to find your error
I am new to Node.js and I am looking for guidance on how to compile together different .md files.
I downloaded this repository and would like to join/merge/compile-together all .md files. So I would like to get one single file with the content of each of the individual files so that I can have a new .md file with all the content inside.
This is what I was thinking about...
var docsdir = '../Docs/';
var filesArray = fs.readdir(docsdir, function(err,files){
if (err) console.error(err);
// missing how to check if file is a directory
return files;
}
var newFile;
for(var i = 0; i < files.length; i++){
fs.readFile(files[i], function read(err, data) {
if (err) {
throw err;
}
newFile = newFile + data;
});
}
fs.writeFile('wholeDocs', newFile)
THe directory structure is in this example:
+
|-Docs
|
|-folder1
| |
| |-file1.md
|
|
|-folder2
| |
| |-file3.md
etc...
This is offline processing so you can use fs.Sync() to do the job that easier than async function.
Note that async is a must if you write web app, async help avoid app blocking.
You also need to known about recursive.
You can learn from code below:
var fs = require('fs'), path = require('path');
function findAllFile(dir, ext) {
var ret = [];
function findOneLevel(dir) {
fs.readdirSync(dir).forEach(function(file) {
var name = path.join(dir, file);
var stat = fs.statSync(name);
if (stat.isDirectory()) {
findOneLevel(name);
} else if (stat.isFile() && path.extname(name).toLowerCase() === ext) {
ret.push(name);
}
});
}
findOneLevel(dir);
return ret;
}
function concatAllMd(dir, outFile) {
var contents = [];
findAllFile(dir, '.md').forEach(function(file) {
contents.push(fs.readFileSync(file, 'utf8'));
})
fs.writeFileSync(outFile, contents.join('\n'));
}
// use
concatAllMd('Docs', 'output.md');