blog.jj5.net (2003 to 2005)

C# scripting...

Thu Feb 17 07:38:00 UTC+1100 2005

Categories:

WARNING: IF YOU RUN THIS SCRIPT AS IS IT MIGHT DELETE FILES FROM YOUR COMPUTER!

...so don't do that, OK?

For use by qualified personnel only.

Have a nice day.

--

I decided that it'd be useful for me to have a 'scripting' capability in C#.

So basically this afternoon I did this:

assoc .csript=cscriptfile
ftype cscriptfile=cmd /c u:\utility\cscript.cmd "%1" %*

You can see how I implemented cscript.cmd below [1], but it won't run 'out of the box' in your environment.

I didn't use assoc, or ftype though, so maybe I'm lying. I just went into the registry, but I think those commands will work.

Now in EditPlus I can just press Ctrl+1 to run a 'cscript' file. And in Explorer I can just double-click to run one.

The scripts take command line args, but the indexes are off by one.

And thanks to Scott for providing the perfect little project to try out my new “C# script“ technology.. :D

John.

[1] cscript.cmd

@call F:\file\proj\net.jj5\utility\command-line\dev\0.10\cm-deploy-beta.cmd >nul 2>&1
rem @echo OFF

rem ///////////////////////////////////////////////////////////////////////
rem // cscript.cmd
rem // ====================================================================
rem //
rem // Compiles and runs a C# source file.
rem //

rem set DEBUG=1

goto :INIT


rem ///////////////////////////////////////////////////////////////////////
rem // HELP procedure
rem // Display brief help message
rem //

:HELP

 if defined TRACE %TRACE% [proc %0 %*]
 
 echo This script does not have help information.
 echo.

goto :EOF


rem ///////////////////////////////////////////////////////////////////////
rem // MAIN procedure
rem //

:MAIN

 if defined TRACE %TRACE% [proc %0 %*]

 color 0B

 if not exist "%APPDATA%\cscript" mkdir "%APPDATA%\cscript"

 set _ScriptPath=%APPDATA%\cscript\%~nx1
 set _ExePath=%_ScriptPath%.exe
 set _LogPath=%_ExePath%.log

 copy "%~f1" "%_ScriptPath%" >nul 2>&1
 if errorlevel 1 set ERR=%errorlevel% && goto :ERROR

 rem echo Script Path: %_ScriptPath%
 rem echo Exe Path: %_ExePath%

 del /s /q "%_ExePath%" >nul 2>&1

 %SYSTEMROOT%\Microsoft.NET\Framework\v1.1.4322\csc.exe /out:"%_ExePath%" /target:exe /debug+ /nologo "%_ScriptPath%" > "%_LogPath%" 2>&1
 if errorlevel 1 set ERR=%errorlevel% && goto :ERROR
 rem if errorlevel 1 exit %errorlevel%

 shift
 "%_ExePath%" %*

 del /s /q "%_ScriptPath%*" >nul 2>&1

 color 02
 exit 0

goto :EOF


rem ///////////////////////////////////////////////////////////////////////
rem // INIT procedure
rem //

:INIT

@if not "%ECHO%"=="" echo %ECHO%
@if not "%OS%"=="Windows_NT" goto DOSEXIT

rem Set local scope and call MAIN procedure
 
setlocal & pushd & set RET=

 set SCRIPTNAME=%~n0
 set SCRIPTPATH=%~f0
 
 if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem)
 
 call u:\utility\ntscriptlib-0.10\ntscriptlib.bat :INIT %SCRIPTPATH%

 if /i {%1}=={/help} (call :HELP %2) & (goto :HELPEXIT)
 if /i {%1}=={/?} (call :HELP %2) & (goto: :HELPEXIT)

 call :MAIN %*

 :HELPEXIT

 popd & endlocal & set RET=%RET%

goto :EOF


rem ///////////////////////////////////////////////////////////////////////
rem // ERROR procedure
rem // Abnormal exit
rem //

:ERROR

 if defined TRACE %TRACE% [proc %0 %*]
 
 rem color 04
 
 echo.
 echo WARNING: Script failed with errorlevel: %ERR%
 echo.
 echo   Current directory is:
 echo     %CD%
 echo   Command-line was:
 echo     %CMDCMDLINE%
 echo.

 notepad "%_LogPath%"

 del /s /q "%_ScriptPath%*" >nul 2>&1

 color 02
 exit 1

goto :EOF


rem ///////////////////////////////////////////////////////////////////////
rem // DOSEXIT
rem // ====================================================================
rem //
rem // These must be the final lines in the script.
rem //

:DOSEXIT

 echo This script requires Windows NT or later.

rem ///////////////////////////////////////////////////////////////////////


Copyright © 2003-2005 John Elliot