3

I'm tidying my projects. And I found the way to remove the object folder with adding: %TEMP%

In my projects. But I want somehow to make this global setting or to auto delete my obj dirs after a build. Is there a way to do that?

blez
  • 4,939
  • 5
  • 50
  • 82

2 Answers2

1

I personally like having a specific Output folder in my project where I put all the compiled files.

I have the following command line in the Post-build events.

copy "$(TargetPath)" "$(SolutionDir)\Output\$(TargetFileName)"

This will copy the compiled file to the Output directory inside the Solution. You would need to add this to all the projects in your solution.

If you have any dependencies that also needs to be copied you could add something like this as well.

copy "$(ProjectDir)Dependencies\Language.xml" "$(SolutionDir)\Output\Extensions\Language.xml"

[EDIT]

You can try the following to have the file copied first, and then once that is done delete the object folder.

copy "$(TargetPath)" "$(SolutionDir)\Output\$(TargetFileName)"
rd /s /q "$(ProjectDir)\obj"

[EDIT2] Updated with screenshots to illustrate. :)

This is how my object folder normally would look like after compiling the project. enter image description here

This is how it looks after compiling it with the above command. As you can see the folder is re-created after the event by Visual Studio, but the folder is empty. enter image description here

You might want to double check that you are running Visual Studio with elevated permissions. To do so, simply right click on the Visual Studio and choose "Run as Administrator".

eandersson
  • 25,781
  • 8
  • 89
  • 110
  • That's interesting, but when you use .dlls and other files you must copy them too. Also I want to get rid of obj. – blez Oct 04 '11 at 16:50
  • You could probably just add a script that removes all the files in a similar manner once you've copied the files you need. You could take a look at this one for example, simply change it to *.obj: http://stackoverflow.com/questions/768282/how-to-delete-files-in-visual-studio-pre-build-event-command-line – eandersson Oct 04 '11 at 16:52
  • I tried: rd /s /q "$(SolutionDir)obj", but it doesn't remove the obj folder. – blez Oct 04 '11 at 17:02
  • I updated with a command to delete the obj folder as well. It will still be re-created, but should be empty. – eandersson Oct 04 '11 at 17:05
  • It doesn't delete the obj folder. – blez Oct 04 '11 at 17:37
  • It works for me, but my guess is that it's recreated again. Have you checked the contents of the folder, or are you simply looking at the folder itself? You also need to make sure that you use `ProjectDir`, and not `SolutionDir`. – eandersson Oct 04 '11 at 17:51
  • @blez Feel free to update your question if this isn't working, or if you need a different solution. :) – eandersson Oct 05 '11 at 20:24
  • Keep in mind that Visual Studio will re-create some of the content regardless, so I don't think you'll be able to delete the folder completely without a 3rd party program, but the contents *should* be cleared with that command. – eandersson Oct 06 '11 at 19:53
  • Feel free to add some reasoning to the down vote. :) P.S if the folder isn't being cleared make sure that you are running Visual Studio with elevated permissions. See the end of my answer. – eandersson Oct 17 '11 at 07:13
1

Are you using source control?
This comment sounds like you don't:

While archiving, those are unneeded megabytes.

("Archiving" sounds a bit like copying the whole project folder regularly to something like backup_yyyymmdd)

If you're not using source control, you should definitively consider starting to use it.

Apart from the general advantages (like, having a change history with dates and comments...), it has an out-of-the-box solution for your problem with the obj folders:

Every good source control software out there supports ignoring certain files or folders which you can define (ignoring means: they can never be committed to the source repository, you don't even see them in the list of changed files, not even when they were changed).

For example, in Mercurial (which I use) the ignore settings are saved in a file named .hgignore in the main folder (Git has the same, it's just called .gitignore).

My default .hgignore file for all Visual Studio projects looks like this:

syntax: glob
bin
obj
*.suo
*.user

The first line belongs to Mercurial's ignore syntax, the rest are the settings what to ignore.
You can see that the bin and obj folders are ignored...and they are ignored no matter in which subfolder they are!

So I don't have to care about where the obj folders actually are, and I don't have to delete them manually every time I build my solution. They are simply non-existent in my source control history.


Plus, I have a variation of Fuji's answer about putting everything in one single output folder:

I like to do this as well, but I prefer changing the output folders in Visual Studio's project settings instead of using post-build events.

The default output folders are:

  • bin\Debug\
  • bin\Release\

I change them to:

  • ..\build\Debug\
  • ..\build\Release\

This compiles everything into subfolders of a build folder which is at the same level like the .sln file (which means: all projects in the solution directly compile into the same folder).
It also reduces compile time because Visual Studio won't have to copy all the dependencies after compiling (because everything already is in the same folder).

(I do it mainly because of the compile time, because I ignore the bin and obj folders anyway in Mercurial as described above, so I don't care where they actually are)

Christian Specht
  • 35,843
  • 15
  • 128
  • 182