How to copy any files using Visual Studio’s build events?

I’m writing this post to have a quick reference for how to copy any files recursively using Visual Studio’s build events. VS provides an easy way to automate any steps such as copy/paste of any type of files by it’s pre/post-build events. To access this option, go to the properties of your project and then select the Build Events option where you have access to two command line input fields to specify your pre/post events.

Recently, I had a scenario where I had to copy the script files of one project to another project in the same solution. Both the projects are ASP.NET MVC web applications. To copy all the files and folders under Scripts folder, I just needed the command-line statement below in the post build event input field. Note that I’m using built-in macros so that the path is not hardcoded based on my systems. This helps to make this reusable if you work within a team as these macros are converted to actual folder path automatically.

This is what I all need to paste all the javascript files with the same folder structure in the destination folder.

start xcopy /E /Y /R "$(ProjectDir)Scripts" "$(SolutionDir)MainWebAppScripts"

If in case you only want to copy the files with specific extension like .js, .ts, .jpeg, .jpg, etc. you can just use the wilcard character ‘*’.{extension} pattern like this:

start xcopy /E /Y /R "$(ProjectDir)Scripts*.js" "$(SolutionDir)MainWebAppScripts"
start xcopy /E /Y /R "$(ProjectDir)Scripts*.ts" "$(SolutionDir)MainWebAppScripts"

Just today I had a scenario where I need to copy javascript files to a different folder but without retaining the folder structure. I tried a lot to play with the arguments exposed by xcopy utility but had no luck. Also, most of the results that I found on web were assuming that nested folder structure is to be maintained on copy. All I needed was to copy all the files with ‘.js’ extension recursively inside Scripts folder and dump those files in a different Scripts folder at root level i.e. no nested folder structure.

So, below is the code that worked which still uses the macro and loop through all the files that matches the extension provided in code below. Note the use of double ‘%%’ for the range variable ‘f’ in the code below. The code was failing with a single ‘%’ for range variable. If you want to copy all the files without performing a match, just replace all the extensions with ‘(*)’ – just a wild character that will match all the files.

for /r "$(ProjectDir)Scripts" %%f in (*.js *.ts *.js.map) do @xcopy "%%f" "$(SolutionDir)MainWebAppScripts"

Below is the help information for ‘xcopy’ command that explains all the arguments that can be used:

C:>xcopy /?
Copies files and directory trees.

XCOPY source [destination] [/A | /M] [/D[:date]] [/P] [/S [/E]] [/V] [/W]
                           [/C] [/I] [/Q] [/F] [/L] [/G] [/H] [/R] [/T] [/U]
                           [/K] [/N] [/O] [/X] [/Y] [/-Y] [/Z] [/B] [/J]
                           [/EXCLUDE:file1[+file2][+file3]...]

  source       Specifies the file(s) to copy.
  destination  Specifies the location and/or name of new files.
  /A           Copies only files with the archive attribute set,
               doesn't change the attribute.
  /M           Copies only files with the archive attribute set,
               turns off the archive attribute.
  /D:m-d-y     Copies files changed on or after the specified date.
               If no date is given, copies only those files whose
               source time is newer than the destination time.
  /EXCLUDE:file1[+file2][+file3]...
               Specifies a list of files containing strings.  Each string
               should be in a separate line in the files.  When any of the
               strings match any part of the absolute path of the file to be
               copied, that file will be excluded from being copied.  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.
  /P           Prompts you before creating each destination file.
  /S           Copies directories and subdirectories except empty ones.
  /E           Copies directories and subdirectories, including empty ones.
               Same as /S /E. May be used to modify /T.
  /V           Verifies the size of each new file.
  /W           Prompts you to press a key before copying.
  /C           Continues copying even if errors occur.
  /I           If destination does not exist and copying more than one file,
               assumes that destination must be a directory.
  /Q           Does not display file names while copying.
  /F           Displays full source and destination file names while copying.
  /L           Displays files that would be copied.
  /G           Allows the copying of encrypted files to destination that does
               not support encryption.
  /H           Copies hidden and system files also.
  /R           Overwrites read-only files.
  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.
  /U           Copies only files that already exist in destination.
  /K           Copies attributes. Normal Xcopy will reset read-only attributes.
  /N           Copies using the generated short names.
  /O           Copies file ownership and ACL information.
  /X           Copies file audit settings (implies /O).
  /Y           Suppresses prompting to confirm you want to overwrite an
               existing destination file.
  /-Y          Causes prompting to confirm you want to overwrite an
               existing destination file.
  /Z           Copies networked files in restartable mode.
  /B           Copies the Symbolic Link itself versus the target of the link.
  /J           Copies using unbuffered I/O. Recommended for very large files.

The switch /Y may be preset in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.

VS build events can be used to do many other things than just simple copy and paste. But this is a common need in a solution that a developer can face. As it’s just a command line utility, the statements can be run using command prompt or a bat file as well but it may need some more code.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger