Mohamed Salemyan
Mohamed Salemyan

Reputation: 691

Copy folder content included in C# project to output directory without parent folder

I have included my project dependencies files to C# project from directory in my project with name "Dependencies" that include every file that project need to this project sub directory (because i don't want the project seems crowded and add needed file to "Dependencies" sub directory) but i don't want the sub directory copied to output path. I want only content of sub directory with all structure copied to output path. I don't want use post built Copy command because i have unneeded files and folders that i don't want copied to output directory. Is there any way to do this?Project Structure

Upvotes: 1

Views: 2250

Answers (1)

Lars Thomas Bredland
Lars Thomas Bredland

Reputation: 310

I often use robocopy "jobs" for this, which allows to save a job description file which tells robocopy what to include and where to copy. So my Post-Build Event would be along the lines of

robocopy $(ProjectDir)\dependencies $(ProjectDir)$(OutDir) /JOB:$(ProjectDir)\dependencies\publish 

The job file is then called publish.rcj and also placed in the dep. directory

Another way of doing it is to create a setupproject that gathers the resources you need, but that is even more work :-)

Here goes. ROBOCOPY

robocopy /? 

gives you the help you need to figure it out :) Here is one to get you started:

robocopy c:\source c:\target /S /E /XD systems /XF publish.rcj /NP /NJS /NJH /NFL /NDL /SAVE:publish.rcj

Explanation of some settings:

  • /SAVE:publish.rcj tells it to save the job description to file (named) publish.rcj
  • /XD systems tells it to ignore folder named systems from copying (you can list many)
  • /XF publish.rcj tells it to not copy files named publish.rcj (you can list many)
  • /S /E tells it to copy all subfolders even empty ones
    • /NP /NJH /NJS /NFL /NDL is just to turn off listing of files and folders that are copied (play with it to see)

Now a "trick" (can't seem to make this work correctly from command line): Open the job file (publish.rcj) and substitute:

/SD:c:\Source With /NOSD

and

/DD:c:\Target With /NODD
  • the NOSD (no source directory) and NODD (no destination dir.) tells the job to always get Source and target from input - the $(ProjectDir) stuff from the post build event

Now you can call the job like this:

robocopy c:\my_new_source c:\my_new_target /job:publish

And it will use all the paramteres specified in the job description file.

Upvotes: 2

Related Questions