【问题标题】:Using nodejs to copy a folder and add a suffix to its name when folder with matching name already exists in the destination当目标中已存在具有匹配名称的文件夹时,使用nodejs复制文件夹并为其名称添加后缀
【发布时间】:2018-01-25 17:11:24
【问题描述】:

我正在调用Feathers JS API 来复制包含一些文件的文件夹。假设我的文件夹名称是 'Website1'

Linux 的正常行为是,它将新文件夹名称附加为 'Website1 副本' 并进一步作为 'Website1 另一个副本''Website1 3rd复制',等等。

这可以通过ShellJS 实现吗?

我的代码:

function after_clone_website(hook) {
  return new Promise((resolve, reject) => {
    let sourceProjectName = hook.params.query.sourceProjectName;
    let destinationProjectName = sourceProjectName + '_copy';

    let userDetailId = hook.params.query.userDetailId;

    let response = '';

    response = shell.cp('-Rf', config.path + userDetailId + '/' + 
        sourceProjectName, config.path + userDetailId + '/' +
        destinationProjectName);

    hook.result = response;
    resolve(hook)

  });
}

【问题讨论】:

    标签: node.js directory shelljs


    【解决方案1】:

    ShellJS 不包含模拟 Linux 相同行为的内置逻辑。 IE。这是附加的; ...复制...另一个副本...第三个副本...第四个副本,当目标路径中的文件夹已经存在且与被复制的源文件夹同名时,复制到文件夹名称。

    解决办法:

    您可以使用带有-e 选项的ShellJS test() 方法来检查路径的每个潜在变化是否存在。如果确实存在,则运行您自己的自定义逻辑以确定 cp() 方法中正确的目标路径值应该是什么。

    自定义逻辑应该是什么?

    以下要点包括一个自定义的copyDirAndRenameIfExists() 函数,它接受两个参数;源文件夹的路径,以及目标文件夹的路径(非常类似于 ShellJS cp() 函数的工作方式)。

    var path = require('path'),
        shell = require('shelljs');
    
    /**
     * Copies a given source folder to a given destination folder.
     *
     * To avoid potentially overwriting an exiting destination folder, (i.e. in the
     * scenario whereby a folder already exists in the destination folder with the
     * same name as the source folder), the source folder will be renamed following
     * normal Linux behaviour. I.e. One of the following values will be appended as
     * appropriate: `copy`, `another copy`, `3rd copy`, `4th copy`, etc.
     *
     * @param {String} srcDir - Path to the source directory to copy.
     * @param {String} destDir - Path to the destination directory.
     * @returns {Object} Object returned from invoking the shelljs `cp()` method.
     */
    function copyDirAndRenameIfExists(srcDir, destDir) {
        var dirName = path.basename(srcDir),
            newDirName = '',
            hasCopied = false,
            counter = 0,
            response = {};
    
        /**
         * Helper function suffixes cardinal number with relevent ordinal
         * number suffix. I.e. st, nd, rd, th
         * @param {Number} number - The number to suffix.
         * @returns {String} A number with its ordinal number suffix.
         */
        function addOrdinalSuffix(number) {
            var j = number % 10,
                k = number % 100;
    
            if (j === 1 && k !== 11) {
                return number + 'st';
            }
            if (j === 2 && k !== 12) {
                return number + 'nd';
            }
            if (j === 3 && k !== 13) {
                return number + 'rd';
            }
            return number + 'th';
        }
    
        /**
         * Helper function to get the appropriate folder name suffix.
         * @param {Number} num - The current loop counter.
         * @returns {String} The appropriate folder name suffix.
         */
        function getSuffix(number) {
            if (number === 1) {
                return ' copy';
            }
            if (number === 2) {
                return ' another copy';
            }
            return ' ' + addOrdinalSuffix(number) + ' copy';
        }
    
        /**
         * Helper function copies the source folder recursively to the destination
         * folder if the source directory does not already exist at the destination.
         */
        function copyDir(srcDir, destDir) {
            if (!shell.test('-e', destDir)) {
                response = shell.cp('-R', srcDir, destDir);
                hasCopied = true;
            }
        }
    
        // Continuously invokes the copyDir() function
        // until the source folder has been copied.
        do {
            if (counter === 0) {
                copyDir(srcDir, path.join(destDir, dirName));
            } else {
                newDirName = dirName + getSuffix(counter);
                copyDir(srcDir, path.join(destDir, newDirName));
            }
            counter += 1;
    
        } while (!hasCopied);
    
        return response;
    }
    

    实施

    1. 将提供的要点中的copyDirAndRenameIfExists() 函数(如上)添加到您现有的节点程序中。

    2. 1234563 IE。添加以下内容:
        var path = require('path');
    
    1. 最后通过更改问题中提供的代码行来调用自定义 copyDirAndRenameIfExists() 函数:

      response = shell.cp('-Rf', config.path + userDetailId + '/' +
          sourceProjectName, config.path + userDetailId + '/' + destinationProjectName);
      

      改为:

      response = copyDirAndRenameIfExists(
        path.join(config.path, userDetailId, sourceProjectName),
        path.join(config.path, userDetailId, destinationProjectName)
      };
      

    【讨论】:

    • 哇,这是完美的解决方案。非常感谢@RobC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2018-05-13
    • 1970-01-01
    • 2018-09-06
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多