Grunt-Contrib-Copy, how to copy contents of a directory keeping the same folder structure without overwriting existing files/folders in dest folder?

John Doe

Source Structure ``` folder

|----Sub-Folder-1
|    |-a.js
|    |-b.js
|----Sub-Folder-2
|    |-c.js
|-d.js
|-e.js
```

Destination structure before running copy task ``` folder

|----Sub-Folder-1
|    |-a.js
|-e.js
```

I need the destination folder to be exactly the same as src folder but I don't want to overwrite the existing files, like a.js and e.js in the above example already exist so they should not be touched, other files/folders should be created, so I want to recursively check inside 'folder' if a file exists or not and copy it if it doesn't exist. I have been using following filter for not overwriting single files filter: function (filepath) { return !(grunt.file.exists('dest')); } but 'folder consists several subdirectories and files so it is not feasible to write for every file. Please help in writing custom grunt task which can do this .

RobC

This can be achieved by adding custom logic inside the filter function of the grunt-contrib-copy target to carry out the following:

  1. Utilize the nodejs path module to help ascertain what the resultant path will be.
  2. Determine whether a file already exists at its destination path by utilizing grunt.file.exists.

The following gists demonstrates how to achieve your requirement cross-platform:

Gruntfile.js

module.exports = function (grunt) {

  'use strict';

  var path = require('path'); // Load additional built-in node module. 

  grunt.loadNpmTasks('grunt-contrib-copy');

  grunt.initConfig({
    copy: {
      non_existing: {
        expand: true,
        cwd: 'src/', //       <-- Define as necessary.
        src: [ '**/*.js' ],
        dest: 'dist/', //     <-- Define as necessary.

        // Copy file only when it does not exist.
        filter: function (filePath) {

          // For cross-platform. When run on Windows any forward slash(s)
          // defined in the `cwd` config path are replaced with backslash(s).
          var srcDir = path.normalize(grunt.config('copy.non_existing.cwd'));

          // Combine the `dest` config path value with the
          // `filepath` value excluding the cwd` config path part.
          var destPath = path.join(
            grunt.config('copy.non_existing.dest'),
            filePath.replace(srcDir, '')
          );

          // Returns false when the file exists.
          return !(grunt.file.exists(destPath));
        }
      }
    }
  });

  grunt.registerTask('default', [ 'copy:non_existing' ]);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I copy folder's contents and tree-structure into a remote directory with same structure

From Java

Visual Studio: How to "Copy to Output Directory" without copying the folder structure?

From Dev

how to copy any folder structure without subfolders

From Dev

how to copy a directory (folder and its contents) to another directory?

From Dev

How to copy a folder by overwriting an existing folder and delete all the old content in Linux?

From Dev

grunt-contrib-copy - ignore folder when copying

From Dev

Copy Folder Contents to Parent Directory in Rake (on Windows)

From Dev

Batch File to Loop Through Folders and Copy Folder & Contents to Directory with Same Folder Name

From Dev

Copy folder contents in Xcode

From Dev

Unix/Mac OS X: Use file list to copy files and folder keeping the directory structure

From Dev

net::scp copy/overwriting folder

From Dev

net::scp copy/overwriting folder

From Dev

How can Powershell copy an entire folder structure but exclude one folder and its contents

From Dev

Swift: Copy folder/directory without files

From Dev

How to copy only folder contents with WinSCP script

From Dev

Copy Multiple Directories into One Directory Keeping the root folder using Robocopy

From Dev

NodeJS - How to copy one folder to another, overwriting only files that differ?

From Dev

How to extract archives with same folder structure so that folder contents are merged?

From Dev

Copy contents of subfolder to another folder

From Dev

Wix- How to copy a directory to install folder

From Dev

How to COPY files of current directory to folder in Dockerfile

From Dev

how to copy directory with wildcard to another folder

From Dev

How can I recursively copy files without overwriting existing permissions?

From Dev

How can I recursively copy files without overwriting existing permissions?

From Dev

How do I copy a file without overwriting an existing file?

From Dev

Copy a folder overwriting ONLY smaller files in destination

From Dev

how to copy a folder into another folder

From Dev

How do I copy a folder keeping owners and permissions intact?

From Dev

How to move some specific folder keeping a predefined directory structure?

Related Related

  1. 1

    How can I copy folder's contents and tree-structure into a remote directory with same structure

  2. 2

    Visual Studio: How to "Copy to Output Directory" without copying the folder structure?

  3. 3

    how to copy any folder structure without subfolders

  4. 4

    how to copy a directory (folder and its contents) to another directory?

  5. 5

    How to copy a folder by overwriting an existing folder and delete all the old content in Linux?

  6. 6

    grunt-contrib-copy - ignore folder when copying

  7. 7

    Copy Folder Contents to Parent Directory in Rake (on Windows)

  8. 8

    Batch File to Loop Through Folders and Copy Folder & Contents to Directory with Same Folder Name

  9. 9

    Copy folder contents in Xcode

  10. 10

    Unix/Mac OS X: Use file list to copy files and folder keeping the directory structure

  11. 11

    net::scp copy/overwriting folder

  12. 12

    net::scp copy/overwriting folder

  13. 13

    How can Powershell copy an entire folder structure but exclude one folder and its contents

  14. 14

    Swift: Copy folder/directory without files

  15. 15

    How to copy only folder contents with WinSCP script

  16. 16

    Copy Multiple Directories into One Directory Keeping the root folder using Robocopy

  17. 17

    NodeJS - How to copy one folder to another, overwriting only files that differ?

  18. 18

    How to extract archives with same folder structure so that folder contents are merged?

  19. 19

    Copy contents of subfolder to another folder

  20. 20

    Wix- How to copy a directory to install folder

  21. 21

    How to COPY files of current directory to folder in Dockerfile

  22. 22

    how to copy directory with wildcard to another folder

  23. 23

    How can I recursively copy files without overwriting existing permissions?

  24. 24

    How can I recursively copy files without overwriting existing permissions?

  25. 25

    How do I copy a file without overwriting an existing file?

  26. 26

    Copy a folder overwriting ONLY smaller files in destination

  27. 27

    how to copy a folder into another folder

  28. 28

    How do I copy a folder keeping owners and permissions intact?

  29. 29

    How to move some specific folder keeping a predefined directory structure?

HotTag

Archive