Showing posts with label batch files. Show all posts
Showing posts with label batch files. Show all posts

June 29, 2010

Run Batch File in C#

Quick method to create and run a batch file, the contents of which are passed as a string.
private void RunBatchFile(
  string batFileContents, 
  int waitToFinishSecs)
{
    string batFile = Path.GetTempFileName();
    batFile = batFile.Replace(".tmp", ".bat");
    File.WriteAllText(batFile, batFileContents);
    string dir = System.IO.Path.GetDirectoryName(batFile);
    string file = System.IO.Path.GetFileName(batFile);

    Process process = new Process();
    process.StartInfo.FileName = file;
    process.StartInfo.WorkingDirectory = dir;
    bool res = false; // Dont do anything with this yet
    try
    {
        process.Start();
        res = process.WaitForExit(waitToFinishSecs*1000);
        File.Delete(batFile);
    }
    catch (Win32Exception winex)
    {
        Debug.WriteLine(winex.ToString());
        throw;
    }    
}

October 7, 2008

Batch File To Clean A Build

Batch file to clean a development environment Add /f to the 'del ...' statements to force deletion of readonly files as well
cd "c:\SomeDevPath"
del /q /s *.exe
del /q /s *.dll
del /q /s *.pdb
del /q /s *.cache
del /q /s *.resources
del /q /s *.proj.user
pause

May 11, 2005

Batch Files

Batch file help
Batch file Command Line Parameters
Run Batch File in C#
Batch File To Clean A Build
To put all output from a command in a file (erases existing file):
command > filename  
To APPEND output to a file (great for log files):
command >> filename  
To use output from one command as input for another:
command1 | command2 
Want a certain batch file to clear the screen after running?:
echo @cls >> batfile.bat
Want to view a text file, a page at a time?:
type filename.txt  more
Use call to invoke another batch file:
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
Echo to just display a line of text in the output:
ECHO ========== Started ==========
PAUSE to make the batchfile pause until a key is pressed:
ECHO ========== Finished ==========
PAUSE
Here is an example build script:
ECHO ========== Started ==========
CALL "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
F:
cd F:\SourceControl\XXX
ECHO ========== CLEAN ==========
del /F /S /Q win32_vs90
del /F /S /Q win32_vs100
del /F /S /Q obj\d32
del /F /S /Q obj\r32
del /F /S /Q *.suo
del /F /S /Q *.ncb
del /F /S /Q *.user
msbuild /t:clean
ECHO ========== Syncing to change list 285519 ==========
p4 sync //depot/Main/XXX/...@285519
p4 -s -c RBovillTestRelated sync //depot/Main/UnitTestData/...@285519
ECHO ========== Building XXX ==========
CALL bin\VSXXX.bat -rebuild -debug -nopause
ECHO ========== Finished ==========
PAUSE