asyncfunctioncopyFilesWithNumberedNames(sourceDir, targetDir) { try { // Create target directory if it does not exist await fs.mkdir(targetDir, { recursive: true });
// Get all files from the source directory const filesToCopy = awaitgetAllFiles(sourceDir);
// Copy each file with a new numbered name let fileNumber = 1; for (const file of filesToCopy) { const newFilename = `${fileNumber}${path.extname(file)}`; const targetPath = path.join(targetDir, newFilename); await fs.copyFile(file, targetPath); console.log(`Copied ${file} to ${targetPath}`); fileNumber++; }
console.log('All files have been copied successfully.'); } catch (err) { console.error('An error occurred:', err); } }
const sourceDirectory = 'xxx'; // Replace with your source directory path const targetDirectory = 'xxx'; // Replace with your target directory path