Posts Tagged ‘Shell Script’

I am developing a tool in C# with Visual Studio 2008. There are two C# files that are generated based on some data files, which are also used at runtime. There are multiple sets of the data files, each of them stored in in different folder. For different purpose, I need to use the data files from different folder. A data file, once defined, will never be changed or removed. When a data file needs to be changed, a new revision of it will be created and co-exist with the older ones. So if the number of files of two folders are different, we can safely say that the two sets of data files are different.

So, how can we automate the build process of the project?

First, I create an environment variable, XXX_DataFiles, indicating the folder of the data files. We can then use this variable in the project settings. When we want to use the data files from another folder, we can simply change the value of the environment variable, without having to change the project settings.

In the Build Event tab of the project properties dialog, I add the script something as follows:

echo OFF

IF NOT EXIST "$(SolutionDir)$(ProjectName)\A.cs" GOTO generate
IF NOT EXIST "$(SolutionDir)$(ProjectName)\B.cs" GOTO generate

for /F %%j in ('dir /A /B "$(TargetDir)Data\*.dat" ^| find /C /V ""') do set Count1=%%j

for /F %%j in ('dir /A /B "$(XXX_DataFiles)*.dat" ^| find /C /V ""') do set Count2=%%j

if %Count1% == %Count2% GOTO end


IF EXIST "$(TargetDir)Data" GOTO start_del

mkdir  "$(TargetDir)Data"
GOTO start_copy

del /Q "$(TargetDir)Data\*.*"

xcopy "$(XXX_DataFiles)*.dat" "$(TargetDir)Data" /Q

MyCodeGenerator "$(TargetDir)Data" "$(SolutionDir)$(ProjectName)"


First, if any of the generated C# files doesn’t exist, we go ahead to the generate. If both of them exist, we check whether the target folder and source folder of the data files have the same number of files. If the numbers are the same, we terminate the process. Otherwise, we create the target folder if it doesn’t exist yet and start copy the files. If the target folder exists, we delete all files inside the folder and copy data files from the source folder. Finally we invoke the MyCodeGenerator to generate the two C# files.

The script

for /F %%j in ('dir /A /B "$(TargetDir)Data\*.dat" ^| find /C /V ""') do set Count1=%%j

finds the number of .dat files in the “$(TargetDir)Data” folder and saves the number in the Count1 environment variable.


Read Full Post »