Copy large number of files but exclude certain sub folders on Windows

I want to backup my work folder which is mainly contained within a folder structure such as:

www
    - project 1
        - assets
        - node_modules
    - project 1
        - assets
        - node_modules
    - project 1
        - assets
        - node_modules

I need to copy all of the projects but exclude the node_modules folder within each project. Is there an easy way to do this in bulk? As I have large quantities of projects to go through.

Running on Windows 8.1


I need to copy all of the projects but exclude the node_modules folder

You can use xcopy together with the exclude option:

xcopy www backup /exclude:except.txt
  • except.txt contains the list of files of directories to be excluded.

except.txt:

node_modules

/EXCLUDE:file1[+file2][+file3]...

  • The files can each contain one or more full or partial pathnames to be excluded.
  • When any of these match any part of the absolute path of a SOURCE file, then that file will be excluded.
  • For example, specifying a string like \obj\ or .obj will exclude all files underneath the directory obj or all files with the .obj extension respectively.

Source xcopy - Copy files and/or directory trees to another folder.


Further reading

  • An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.

Thanks to @DavidPostill for his answer. It worked a treat. I did however also managed to get robocopy working. For those whom it might help, the command was:

robocopy SOURCE DEST /mir /xd node_modules

Source being the folder to copy, and dest being the destination. And right at the end I have node_modules as the folder I want to exclude.


In the contextual menu of the "file explorer" I added a powershell script that allows me to duplicate any folder without node_modules :

enter image description here

Here's how to do it :

Create your PowerShell script file script.ps1 :

$Source=$args[0]
$Position = $Source.lastindexofany("\")
$Destination = $Source.substring(0, $Position + 1)
$FolderName = $Source.substring($Position + 1)
$Destination = "$($Destination)$($FolderName)-DUPLICATE_WITHOUT_NODE_MODULES"
robocopy $Source $Destination /mir /xd node_modules

Create a registry file : (ex: Add_Script_To_ContextMenu.reg)

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\DupFolderWithoutNodeModules]
@="Duplicate folder without node_modules"

[HKEY_CLASSES_ROOT\Directory\shell\DupFolderWithoutNodeModules\command]
@="C:\\\\Windows\\\\system32\\\\WindowsPowerShell\\\\v1.0\\\\powershell.exe -File \"C:\\Path of_your_script\\script.ps1\" \"%L\""

Replace C:\\Path of_your_script by yours. Do not forget to double \ by \\.

Then, double clic on the .reg file !

And it's done.