😅Thay đổi tên ảnh hàng loạt bằng ảnh khác, Changename (ok)
Last updated
Last updated
Chú ý để thực hiện hãy copy file step-1 giống với step-2
F:\Thien Hoa Account\Changename\process.js
// get name images in folder step-1 👇🏽
const fs = require('fs');
const path = require("path");
// Recursive function to get files
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir)
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
// If it is a directory, recursively call the getFiles function with the directory path and the files array
getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
const filesInTheFolder = getFiles('step-1');
var strTmp = filesInTheFolder.toString().replace(/step-1\//g, "");
var imageNew = strTmp.split(',');
var lengthOld = imageNew.length;
var imageOld = [];
imageNew.forEach(function(img, index) {
if (img.substr(-4) == '.jpg') {
imageOld.push(`${index + 1}${img.substr(-4)}`);
}
if (img.substr(-5) == '.webp') {
imageOld.push(`${index + 1}${img.substr(-5)}`);
}
if (img.substr(-5) == '.jpeg') {
imageOld.push(`${index + 1}${img.substr(-5)}`);
}
if (img.substr(-4) == '.png') {
imageOld.push(`${index + 1}${img.substr(-4)}`);
}
if (img.substr(-4) == '.gif') {
imageOld.push(`${index + 1}${img.substr(-4)}`);
}
});
var arrs2 = [];
const filesInTheFolder2 = getFiles('step-2');
var strTmp2 = filesInTheFolder2.toString().replace(/step-2\//g, "");
var imageNew2 = strTmp2.split(',');
console.log("Result 1 😀");
imageNew2.forEach(function(item, index) {
if (index < lengthOld) {
const pathToFile = path.join(__dirname + "/step-2", item);
const newPathToFile = path.join(__dirname + "/step-3", imageOld[index]);
fs.rename(pathToFile, newPathToFile, function(err) {
if (err) {
throw err
} else {
console.log(index);
}
});
}
});
setTimeout(function() {
console.log("Result 2 😀");
const filesInTheFolder3 = getFiles('step-3');
var strTmp3 = filesInTheFolder3.toString().replace(/step-3\//g, "");
var imageNew3 = strTmp3.split(',');
imageNew3.forEach(function(item, index) {
const pathToFile2 = path.join(__dirname + "/step-3", item)
const newPathToFile2 = path.join(__dirname + "/step-4", imageNew[index])
fs.rename(pathToFile2, newPathToFile2, function(err) {
if (err) {
throw err
} else {
console.log(index);
}
});
});
}, 3000);
F:\Thien Hoa Account\Changename
F:\Thien Hoa Account\Node\getnames.js
const fs = require('fs')
// Recursive function to get files
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir)
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
// If it is a directory, recursively call the getFiles function with the directory path and the files array
getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
const filesInTheFolder = getFiles('F:/Thien Hoa Account/Getname');
var writeStream = fs.createWriteStream("JournalDEV.html");
writeStream.write(filesInTheFolder.toString());
writeStream.end();
F:\Thien Hoa Account\Node\replace.js
const fs = require("fs")
const path = require("path")
var imageOld = ["1.jpg","2.jpg"];
var imageNew = ["1.png","2.png"];
imageOld.forEach(function(item, index){
const pathToFile = path.join(__dirname + "/images", item)
const newPathToFile = path.join(__dirname + "/imagesnew", imageNew[index])
fs.rename(pathToFile, newPathToFile, function(err) {
if (err) {
throw err
} else {
console.log(index)
}
})
});
Bạn có thể thay đổi tên files trong folder thành tên thay thês
F:\Thien Hoa Account\Changename\test.js
const fs = require('fs');
const path = require("path");
function getFiles(dir, files = []) {
// Get an array of all files and directories in the passed directory using fs.readdirSync
const fileList = fs.readdirSync(dir)
// Create the full path of the file/directory by concatenating the passed directory and file/directory name
for (const file of fileList) {
const name = `${dir}/${file}`
// Check if the current file/directory is a directory using fs.statSync
if (fs.statSync(name).isDirectory()) {
// If it is a directory, recursively call the getFiles function with the directory path and the files array
getFiles(name, files)
} else {
// If it is a file, push the full path to the files array
files.push(name)
}
}
return files
}
const imageOld = getFiles('files');
const imageNew = getFiles('files');
for (var i = 0; i < imageNew.length; i++) {
imageNew[i] = imageNew[i].replace('thien-hoa', 'bac-viet');
}
imageOld.forEach((img, index) => {
fs.rename(img, imageNew[index], (err) => {
if (err) throw err;
console.log('Rename complete!');
});
});