Posts tagged ‘shell’

Execute batch commands before or after compilation using Pre-build or Post-build events

Have you ever wanted to execute a set of commands before or after the compilation, like copying the contents of the directory to the target compilation directory, or copy output of the compilation to a specified folder. This feature is integrated inside visual studio with the name; “Pre-build or Post-build event command line”. You access it from the project properties, in the build events tab.

ProjectProperties

If you want to do something before the compilation you enter the shell commands to pre-build, and post-build is the operations for after the compilation. We can use the predefined macros without hard copying the directory names. The edit window will guide us to find the macros.

Editing

Let’s say we want to copy the contents of the output directory to the root d:\. What we do is to edit the post-build event command with the command :


copy /D $(TargetPath) c:\

When we build it. We just get the error, “The command “copy D:\DEV\_Projects\sampleProj\bin\sample.dll d:\” exited with code 1.” Trying to execute the code from command line you get a similar error ‘The system cannot find the file specified.’

copy D:\\DEV\\_Projects\\sampleProj\\bin\\sample.dll d:"
The system cannot find the file specified.


What we forget is the need for the quotations for the long directory and filenames.

copy /D \"$(TargetPath)\" "c:\"

As a result, we can do whatever bat file operations before or after the compilation.

Console2: Tabbed Command Prompt

Another open source tool appears on the web.
DownloadSquadLink Console: Tabbed command prompt for Windows.

This one is really useful to me. We are using console when we can’t do everything with mouse clicks. For example, we might want to compile a cs from the visual studio command prompt, we can’t do from the normal command prompt because the assemblies are not referenced(of course you can do, but it is not out of the box)

Console is an application that allows you to use the command prompt. Not only the command or cmd but also anything we would like. Here is the settings windows that we do the configurations.

settings.png

Using the settings window we can add new consoles. Actually we can directly edit the exml file. Here is my xml file configured for visual studio 2003 and 2005 command prompt, and windowspower shell

 
<tabs>
		<tab title="Console">
			<console shell="" init_dir=""/>
			<cursor style="0" r="255" g="255" b="255"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
		<tab title="cmd">
			<console shell="cmd.exe" init_dir=""/>
			<cursor style="0" r="255" g="255" b="255"/>
			<background type="2" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="65" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
		<tab title="Windows Power Shell" icon="C:\Program Files\Windows PowerShell\v1.0\powershell.exe">
			<console shell="powershell.exe" init_dir=""/>
			<cursor style="8" r="255" g="255" b="255"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
		<tab title="Visual Studio 2005 Command Prompt">
			<console shell="cmd.exe /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" init_dir=""/>
			<cursor style="11" r="255" g="255" b="255"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
		<tab title="Visual Studio .NET 2003 Command Prompt">
			<console shell="cmd.exe /k "C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat" init_dir=""/>
			<cursor style="4" r="255" g="255" b="255"/>
			<background type="0" r="0" g="0" b="0">
				<image file="" relative="0" extend="0" position="0">
					<tint opacity="0" r="0" g="0" b="0"/>
				</image>
			</background>
		</tab>
	</tabs>


Here is the result what we get for it.

powershell.pngaction1.png

Very useful and we have got the power of console…