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.

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:

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.

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

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