Tag Archives: batch

How to safely use your time while you build a project?

So your build takes a while… How do you make use of that time (e.g. check Stack Overflow) and not forget to get back to work when the build is done? Well, you can make a sound. I am not sure if there is a simple built-in option to Visual Studio allows to play sounds on events. It is quite possible there is, but I will use what I know. I found you can play a sounds in command line by typing echo ^G, but it didn’t seem to work for me in a VS project post-build step. I blogged about using batch scripts and C# code together in a single file and here’s a way to beep from a batch script:

Continue reading

Tagged , ,

Batch script to compile pixel shaders + mixing batch & C#

If you want to build a pixel shader for WPF there are some good examples and tools – some of which are:

  • NShader – that lets you edit HLSL code in VS with syntax highlighting
  • Shazzam – that helps edit and quickly test HLSL code + provides a few samples + generates WPF C#/VB wrapper classes automatically
  • Shader Effect BuildTask – that makes the shader build automatically when you build your project

The build task is a helpful thing, since it builds the effect every time you build your project, so your shader source code is treated the same way your C# source is but not really so helpful if you want to share your code with someone who does not necessarily have DirectX SDK or the build task installed – since it will break their build. That is why I usually include both the fx source code and the compiled shader in my VS projects – this also lets me dig up my old WPF projects and build them without the need for having DirectX SDK installed, since I write shaders less frequently than I set up new dev. machines.

Now, I do not want to remember the fxc.exe build commands or manually compile each shader, so I also include a batch script in the shaders folder that I can manually trigger when I modify and want to recompile a shader.

Since even minor changes in your shader files are error prone – I wanted to have a clear message in my script to so I know if my compiler worked without having to read all output. When I first wrote it I thought I could use colors – green for success and red for failure, but I found setting these on a per-line basis is not supported in regular command-line windows, so I just used full screen color changes “color 0A” for green and “color 0C” for red.

Today I wanted to play with shaders again and as I looked at that script I remembered I found some interesting code here (copied from here) one day, that allows you to have a single file with batch script and C# code. I found a few lines of C# code on StackOverflow that let you change the colors in the console window and here is what I have:

Note 1: This script requires DirectX SDK to be installed.

Note 2: This would probably be easier in PowerShell and I might port it one day, but it was just more interesting to me to do it this hacky way. + I don’t really know PowerShell or if it is available everywhere yet.

/* >NUL
@REM Open the containing folder in Windows Explorer and double click this file to compile shader
@CALL :COMPILE_FX HueEffect.fx HueEffect.ps
@DEL /q cecho.exe

using System;
using System.Linq;

class cecho
    static void Main(string[] args)
            Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[0], true);
            Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), args[1], true);
            Console.WriteLine(string.Join(" ", args.Skip(2)));
            Console.WriteLine("Usage: cecho background foreground text\r\nColors:");
            foreach (var name in Enum.GetNames(typeof(ConsoleColor)))
                Console.WriteLine(name + @": " + (int)Enum.Parse(typeof(ConsoleColor), name));

More batch code follows:

@SET WinDirNet=%WinDir%\Microsoft.NET\Framework
@IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
@IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
@IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
@%csc% /nologo /out:"cecho.exe" %1

@SET fxc="%DXSDK_DIR%Utilities\bin\x86\fxc.exe"
@SET FXCPARAMS=/nologo /T ps_2_0 /E main /Fo%2 %1
@%FXC% %fxcparams% 1> NUL
@IF [%ERRORLEVEL%] EQU [0] (cecho 0 10 %1 - COMPILATION SUCCESSFULL!) ELSE (cecho 0 12 %1 - ERROR! FIX SHADER!)

Tagged , , , ,

Clear all .svn bindings from a directory

dir /B /S ".svn" /AD >a.list & for /F "delims=" %a in (a.list) DO (attrib -H "%a" & rd /Q /S "%a") & del /Q a.list

Similarly – clear all bin/obj/resharper directories

dir /B /S "bin" /AD >a.list & for /F "delims=" %a in (a.list) DO (attrib -H "%a" & rd /Q /S "%a") & del /Q a.list
dir /B /S "obj" /AD >a.list & for /F "delims=" %a in (a.list) DO (attrib -H "%a" & rd /Q /S "%a") & del /Q a.list
dir /B /S "_ReSharper*" /AD >a.list & for /F "delims=" %a in (a.list) DO (attrib -H "%a" & rd /Q /S "%a") & del /Q a.list

Note to self – use double ampersands in batch files.

Batch script to clean a solution folder

dir /B /S ..\"bin" /AD >a.list & for /F "delims=" %%a in (a.list) DO (attrib -H "%%a" & rd /Q /S "%%a")
dir /B /S ..\"obj" /AD >b.list & for /F "delims=" %%a in (b.list) DO (attrib -H "%%a" & rd /Q /S "%%a")
dir /B /S ..\"_ReSharper*" /AD >c.list & for /F "delims=" %%a in (c.list) DO (attrib -H "%%a" & rd /Q /S "%%a")
dir /B /S ..\"Debug" /AD >d.list & for /F "delims=" %%a in (d.list) DO (attrib -H "%%a" & rd /Q /S "%%a")
dir /B /S ..\"x64" /AD >e.list & for /F "delims=" %%a in (e.list) DO (attrib -H "%%a" & rd /Q /S "%%a")


del a.list
del b.list
del c.list
del d.list
del e.list

Tagged ,