可以使用以下代码实现:

const fs = require('fs');
const path = require('path');

const prefix = 'prefix_'; // 前缀
const bundlePath = 'path/to/bundle'; // bundle 路径

function addPrefixToFiles(dirPath) {
  fs.readdir(dirPath, (err, files) => {
    if (err) {
      console.error(`Failed to read directory ${dirPath}: ${err}`);
      return;
    }
    files.forEach(file => {
      const filePath = path.join(dirPath, file);
      fs.stat(filePath, (err, stats) => {
        if (err) {
          console.error(`Failed to get stats for file ${filePath}: ${err}`);
          return;
        }
        if (stats.isDirectory()) {
          addPrefixToFiles(filePath);
        } else if (stats.isFile()) {
          const newFilePath = path.join(dirPath, prefix + file);
          fs.rename(filePath, newFilePath, err => {
            if (err) {
              console.error(`Failed to rename file ${filePath} to ${newFilePath}: ${err}`);
            } else {
              console.log(`Renamed file ${filePath} to ${newFilePath}`);
            }
          });
        }
      });
    });
  });
}

addPrefixToFiles(bundlePath);

这个脚本会递归遍历指定路径下的所有文件夹和文件,对于每个文件,如果是文件夹,则继续递归遍历;如果是文件,则给文件名添加前缀并重命名。

cocos 批量给bundle下文件夹以及子文件添加前缀

原文地址: https://www.cveoy.top/t/topic/b2AB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录