batch esempio

Upload: robert-schumann

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Batch Esempio

    1/12

    @echo off

    rem *************** start of 'main'

    set DEBUG=0if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem)

    rem Note that right now there is a bug in tracerpt.exe cause of which you mightwant to use tracefmt.exe instead.rem set the value 1 if you want to use tracefmt.exe insteadset USE_TRACE_FMT=1%TRACE% The value of variable USE_TRACE_FMT is %USE_TRACE_FMT%

    rem define variables for each of the switches we supportset SWHELP=hset SWMODE=modeset SWTRACELOG=tracelogset SWMOF=mofset SWOUTPUT=oset VALID=0

    rem define variables for parameters to be passed to tracerptset TRACEDIR=%WINDIR%\system32\msdtc\traceset TRACEFILE1=%TRACEDIR%\dtctrace.log

    set TRACEFILE2=%TRACEDIR%\tracetx.logset MOFFILE=%TRACEDIR%\msdtctr.mofset ERRORFILE=%TRACEDIR%\errortrace.txtset OUTPUTFILE=%TRACEDIR%\trace

    rem Parse command line and setup variablesset CMDLINE=%*%TRACE% About to call PARSECMDLINE with the argument %CMDLINE%call :PARSECMDLINE 0

    rem Validate the command line%TRACE% About to call the procedure VALIDATEcall :VALIDATE

    rem if Vaidation fails, we give upif "%VALID%"=="0" (

    %TRACE% Parameter validation failed, exiting ...goto :EOF

    )

    rem depending on the value of the mode, set the tracelogfilecall :MYFINDSWITCH %SWMODE%if not "%RET%"=="0" (

    if "%RETV%"=="1" set TRACEFILE=%TRACEFILE1%if "%RETV%"=="2" set TRACEFILE=%TRACEFILE2%

    )

    rem if the tracelog switch was used, set the tracelogfilecall :MYFINDSWITCH %SWTRACELOG%if not "%RET%"=="0" (

    set TRACEFILE=%RETV%)

    rem if the mof switch was used, set the moffilecall :MYFINDSWITCH %SWMOF%if not "%RET%"=="0" (

  • 7/30/2019 Batch Esempio

    2/12

    set MOFFILE=%RETV%)

    rem if the output switch was used, set the output filecall :MYFINDSWITCH %SWOUTPUT%if not "%RET%"=="0" (

    set OUTPUTFILE=%RETV%)

    %TRACE% TRACEFILE=%TRACEFILE%%TRACE% MOFFILE=%MOFFILE%%TRACE% OUTPUTFILE=%OUTPUTFILE%

    rem if the specified tracelogfile does not exist, display an error message and give upif not exist %TRACEFILE% (

    echo The tracelogfile %TRACEFILE% does not exist. exiting ...call :HELPgoto :EOF

    )

    rem if the specified moffile does not exist, display an error message and give upif not exist %MOFFILE% (

    echo The moffile %MOFFILE% does not exist. exiting ...call :HELPgoto :EOF

    )

    rem set a variable for output file with extensionset OUTPUTFILEWITHEXT=%OUTPUTFILE%.csv%TRACE% The value of variable OUTPUTFILEWITHEXT=%OUTPUTFILEWITHEXT%

    rem if the specified outputfile exists, ask if the user is ok with it being over-written.rem if the user wants to continue, delete the old output file, else give up.

    if exist %OUTPUTFILEWITHEXT% (echo The file %OUTPUTFILEWITHEXT% already exists. You may press Control-C to terminate the batch file. Continuing the batch file will overwrite this file.

    Pausedel %OUTPUTFILEWITHEXT% 1>nul 2>nul

    )

    rem if the old error file exists, delete itif exist %ERRORFILE% (

    del %ERRORFILE% 1>nul 2>nul%TRACE% Deleted the file %ERRORFILE%

    )

    rem call the utility with the right arguments%TRACE% About to call the utility tracerpt.exe ...

    if "%USE_TRACE_FMT%"=="0" (goto :USE_TRACEPRT_UTILITY) else (goto :USE_TRACEFMT_UTILITY)

    :USE_TRACEPRT_UTILITY%TRACE% Entered the USE_TRACEPRT_UTILITY block, about to call traceprttracerpt %TRACEFILE% -o %OUTPUTFILE% -mof %MOFFILE% > %ERRORFILE% 2>&1

  • 7/30/2019 Batch Esempio

    3/12

    rem if output file does not exist, display an error message and give upif not exist %OUTPUTFILEWITHEXT% (

    %TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...

    call :DISPLAY_ERROR_MESSAGEgoto :EOF

    )notepad %OUTPUTFILEWITHEXT%goto :EOF

    :USE_TRACEFMT_UTILITY%TRACE% Entered the USE_TRACEFMT_UTILITY block, about to call tracefmttracefmt %TRACEFILE% -o %OUTPUTFILEWITHEXT% -tmf %MOFFILE% -nosummary > %ERRORFILE% 2>&1rem if output file does not exist, display an error message and give upif not exist %OUTPUTFILEWITHEXT% (

    %TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...

    call :DISPLAY_ERROR_MESSAGEgoto :EOF

    )notepad %OUTPUTFILEWITHEXT%goto :EOF

    goto :EOFrem *************** end of 'main'

    rem *************** Procedures begin here ****************************

    rem *************** start of procedure VALIDATE:VALIDATE

    set ARG=1set SWHELPFOUND=0set SWMODEFOUND=0set SWTRACELOGFOUND=0set SWMOFFOUND=0set SWOUTPUTFOUND=0set OUTNAMENAME=0

    rem If no arguments are used at all, don't perform any other validation, just display help and give upif %CMDARGCOUNT% EQU 0 if %CMDSWCOUNT% EQU 0 (call :HELP) & (goto :EOF)

    rem If not arguments are given, display help

    if %CMDARGCOUNT% GTR 0 goto ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES

    rem If the switch SWHELP is used anywhere, don't perform any other validation, just display help and give upcall :MYFINDSWITCH %SWHELP%if not "%RET%"=="0" (call :HELP) & (goto :EOF)

    :SWLOOP

    if %ARG% GTR %CMDSWCOUNT% goto :SWLOOPEND

  • 7/30/2019 Batch Esempio

    4/12

    call :GETSWITCH %ARG%set MYSWITCH=%RET:~1%

    rem make sure no switch is used twiceif /i "%MYSWITCH%"=="%SWHELP%" (if "%SWHELPFOUND%"=="1" (goto ERROR_USED

    _SAME_SWITCH_TWICE) else (set SWHELPFOUND=1))if /i "%MYSWITCH%"=="%SWMODE%" (if "%SWMODEFOUND%"=="1" (goto ERROR_USED

    _SAME_SWITCH_TWICE) else (set SWMODEFOUND=1))if /i "%MYSWITCH%"=="%SWTRACELOG%" (if "%SWTRACELOGFOUND%"=="1" (goto ER

    ROR_USED_SAME_SWITCH_TWICE) else (set SWTRACELOGFOUND=1))if /i "%MYSWITCH%"=="%SWMOF%" (if "%SWMOFFOUND%"=="1" (goto ERROR_USED_S

    AME_SWITCH_TWICE) else (set SWMOFFOUND=1))if /i "%MYSWITCH%"=="%SWOUTPUT%" (if "%SWOUTPUTFOUND%"=="1" (goto ERROR_

    USED_SAME_SWITCH_TWICE) else (set SWOUTPUTFOUND=1))

    rem make sure that the switches mode and tracelog are not used simultaneously

    if "%SWMODEFOUND%"=="1" if "%SWTRACELOGFOUND%"=="1" goto ERROR_USED_BOTH_MODE_AND_TRACELOG

    rem make sure that there is no switch outside our listif /i not "%MYSWITCH%"=="%SWHELP%" (

    if /i not "%MYSWITCH%"=="%SWMODE%" (if /i not "%MYSWITCH%"=="%SWTRACELOG%" (

    if /i not "%MYSWITCH%"=="%SWMOF%" (if /i not "%MYSWITCH%"=="%SWOUTPUT%" ((echo Invalid Switch "%RET%") &

    (call :HELP) & (goto :EOF) )))))set /a ARG+=1

    goto :SWLOOP:SWLOOPEND

    rem make sure that either the switch "-mode" or "-tracelog" was usedif "%SWMODEFOUND%"=="0" if "%SWTRACELOGFOUND%"=="0" (echo Invalid Usage : neither "-%SWMODE%" nor "-%SWTRACELOG%" was specified) & (call :HELP) & (goto :EOF)

    rem make sure that the value of the mode entered is valid

    call :MYFINDSWITCH %SWMODE%if not "%RET%"=="0" if not "%RETV%"=="1" if not "%RETV%"=="2" goto ERROR_INVALID_MODE

    rem make sure that the value of the outputfile entered does not have any extensioncall :MYFINDSWITCH %SWOUTPUT%for /f "tokens=1* delims=." %%I in ("%RETV%") do (set OUTPUTEXT=%%J)if not "%OUTPUTEXT%"=="" goto ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION

    rem if we have come this far, everything went well, set the valid flagset VALID=1goto :EOF

    :ERROR_USED_SAME_SWITCH_TWICE(echo Invalid Usage : use the switch %RET% multiple times) & (call :HELP) & (goto :EOF)

    :ERROR_USED_BOTH_MODE_AND_TRACELOG(echo Invalid Usage : cannot use both "-%SWMODE%" and "-%SWTRACELOG%" at the same time) & (call :HELP) & (goto :EOF)

    :ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES

  • 7/30/2019 Batch Esempio

    5/12

    call :GETARG 1echo Invalid Usage : "%RET%" used without any switchcall :HELPgoto :EOF

    :ERROR_INVALID_MODE(echo Invalid Usage : Valid values for %SWMODE% are 1 and 2) & (call :HELP) & (goto :EOF)

    :ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION(echo Invalid Usage : Output filename should not have any extension) & (call :HELP) & (goto :EOF)

    rem *************** end of procedure VALIDATE

    rem *************** start of procedure HELP:HELPecho Usageecho "msdtcvtr { -MODE {1 | 2} | -tracelog tracelogfilename } [options]"echo "All switches can be prefixed with either '-' or '/'"echo Parameters:echo "-MODE 1 to view background tracing"echo "-MODE 2 to view tracing generated by ui"echo "-tracelog binary Trace log file name"

    echo Options:echo "-h OR -? Display Help"echo "-o Output Filename without extension"echo "-mof Mof Filename"goto :EOFrem *************** end of procedure HELP

    rem *************** start of procedure DISPLAY_ERROR_MESSAGE:DISPLAY_ERROR_MESSAGEecho Failed to convert the binary trace data to text format.echo Following reasons can cause this to happen:echo 1) The utility TraceFmt.exe is missing

    echo 2) The file %TRACEFILE% is either missing or corruptedecho 3) The file %MOFFILE% is either missing or corruptedecho The exact error message can be found in the file '%ERRORFILE%'goto :EOFrem *************** end of procedure DISPLAY_ERROR_MESSAGE

    rem /////////////////////////////////////////////////////////////////////////rem INIT procedurerem Must be called in local state before other procs are usedrem:INIT%TRACE% [proc %0 %*]

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem VARDEL procedurerem Delete multiple variables by prefixremrem Arguments: %1=variable name prefixrem:VARDEL

  • 7/30/2019 Batch Esempio

    6/12

    %TRACE% [proc %0 %*]for /f "tokens=1 delims==" %%I in ('set %1 2^>nul') do set %%I=

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem PARSECMDLINE procedurerem Parse a command line into switches and argsremrem Arguments: CMDLINE=command text to parserem %1=0 for new parse (def) or 1 to append to existingremrem Returns: CMDARG_n=arguments, CMDSW_n=switchesrem CMDARGCOUNT=arg count, CMDSWCOUNT=switch countrem RET=total number of args processedrem:PARSECMDLINE%TRACE% [proc %0 %*]

    if not {%1}=={1} ((call :VARDEL CMDARG_)(call :VARDEL CMDSW_)(set /a CMDARGCOUNT=0)(set /a CMDSWCOUNT=0)

    )set /a RET=0

    call :PARSECMDLINE1 %CMDLINE% 1>nulset _MTPLIB_T1=set _LASTARGSWITCH=0set _LASTARGSWITCHNAME=0

    goto :EOF:PARSECMDLINE1

    if {%1}=={} goto :EOFset _MTPLIB_T1=%1set _MTPLIB_T1=%_MTPLIB_T1:"=%set /a RET+=1shift /1if "%_MTPLIB_T1:~0,1%"=="/" goto :PARSECMDLINESWif "%_MTPLIB_T1:~0,1%"=="-" goto :PARSECMDLINESW

    if "%_LASTARGSWITCH%"=="1" (set CMDSW_%CMDSWCOUNT%=%_LASTARGSWITCHNAME%:%_MTPLIB_T1%set _LASTARGSWITCH=0goto :PARSECMDLINE1

    )set /a CMDARGCOUNT+=1set CMDARG_%CMDARGCOUNT%=%_MTPLIB_T1%set _LASTARGSWITCH=0goto :PARSECMDLINE1:PARSECMDLINESWset /a CMDSWCOUNT+=1set CMDSW_%CMDSWCOUNT%=%_MTPLIB_T1%set _LASTARGSWITCH=1

    set _LASTARGSWITCHNAME=%_MTPLIB_T1%goto :PARSECMDLINE1

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem GETARG procedurerem Get a parsed argument by indexremrem Arguments: %1=argument index (1st arg has index 1)rem

  • 7/30/2019 Batch Esempio

    7/12

    rem Returns: RET=argument text or empty if no argumentrem:GETARG%TRACE% [proc %0 %*]

    set RET=if %1 GTR %CMDARGCOUNT% goto :EOFif %1 EQU 0 goto :EOFif not defined CMDARG_%1 goto :EOFset RET=%%CMDARG_%1%%call :RESOLVE

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem GETSWITCH procedurerem Get a switch argument by indexremrem Arguments: %1=switch index (1st switch has index 1)remrem Returns: RET=switch text or empty if nonerem RETV=switch value (after colon char) or emptyrem:GETSWITCH%TRACE% [proc %0 %*]

    (set RET=) & (set RETV=)

    if %1 GTR %CMDSWCOUNT% goto :EOFif %1 EQU 0 goto :EOFif not defined CMDSW_%1 goto :EOFset RET=%%CMDSW_%1%%call :RESOLVEfor /f "tokens=1* delims=:" %%I in ("%RET%") do (set RET=%%I) & (set RET

    V=%%J)goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem FINDSWITCH procedurerem Finds the index of the named switchrem

    rem Arguments: %1=switch namerem %2=search start index (def: 1)remrem Returns: RET=index (0 if not found)rem RETV=switch value (text after colon)rem:FINDSWITCH%TRACE% [proc %0 %*]

    if {%2}=={} (set /a _MTPLIB_T4=1) else (set /a _MTPLIB_T4=%2):FINDSWITCHLOOP

    call :GETSWITCH %_MTPLIB_T4%if "%RET%"=="" (set RET=0) & (goto :FINDSWITCHEND)if /i "%RET%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :FINDSWITCHEN

    D)set /a _MTPLIB_T4+=1

    goto :FINDSWITCHLOOP:FINDSWITCHENDset _MTPLIB_T4=

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem MYFINDSWITCH procedurerem Finds the index of the named switch

  • 7/30/2019 Batch Esempio

    8/12

    remrem Arguments: %1=switch name without the leading / or -rem %2=search start index (def: 1)remrem Returns: RET=index (0 if not found)rem RETV=switch value (text after colon)rem:MYFINDSWITCH%TRACE% [proc %0 %*]

    if {%2}=={} (set /a _MTPLIB_T4=1) else (set /a _MTPLIB_T4=%2):MYFINDSWITCHLOOP

    call :GETSWITCH %_MTPLIB_T4%if "%RET%"=="" (set RET=0) & (goto :MYFINDSWITCHEND)if /i "%RET:~1%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :MYFINDSWI

    TCHEND)set /a _MTPLIB_T4+=1

    goto :MYFINDSWITCHLOOP:MYFINDSWITCHENDset _MTPLIB_T4=

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem REGSETM and REGSETU proceduresrem Set registry values from variables

    remrem Arguments: %1=reg context (usually script name)rem %2=variable to save (or prefix to save set of vars)rem:REGSETM%TRACE% [proc %0 %*]

    for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKLM %1 %%I "%%J"goto :EOF:REGSETU%TRACE% [proc %0 %*]

    for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKCU %1 %%I "%%J"

    goto :EOF:REGSET1set _MTPLIB_T10=%4set _MTPLIB_T10=%_MTPLIB_T10:\=\\%reg add %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nulreg update %1\Software\MTPScriptContexts\%2\%3=%_MTPLIB_T10% >nul

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem REGGETM and REGGETU proceduresrem Get registry value or values to variablesremrem Arguments: %1=reg context (usually script name)

    rem %2=variable to restore (def: restore entire context)remrem Returns: RET=value of last variable loadedremrem WARNING: The "delims" value in the FOR commands below is a TABrem character, followed by a space. If this file is edited byrem an editor which converts tabs to spaces, this procedurerem will break!!!!!rem:REGGETM

  • 7/30/2019 Batch Esempio

    9/12

    %TRACE% [proc %0 %*]for /f "delims= tokens=2*" %%I in ('reg query HKLM\Software\MTPScriptCo

    ntexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"goto :EOF:REGGETU%TRACE% [proc %0 %*]

    for /f "delims= tokens=2*" %%I in ('reg query HKCU\Software\MTPScriptContexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"goto :EOF:REGGETM1

    set _MTPLIB_T10=%2set _MTPLIB_T10=%_MTPLIB_T10:\\=\%set _MTPLIB_T10=%_MTPLIB_T10:"=%set %1=%_MTPLIB_T10%set RET=%_MTPLIB_T10%

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem REGDELM and REGDELU proceduresrem Delete registry valuesremrem Arguments: %1=reg context (usually script name)rem %2=variable to delete (def: delete entire context)rem

    :REGDELM%TRACE% [proc %0 %*]call :GETTEMPNAMEecho y >%RET%reg delete HKLM\Software\MTPScriptContexts\%1\%2 nuldel %RET%

    goto :EOF:REGDELU%TRACE% [proc %0 %*]

    call :GETTEMPNAMEecho y >%RET%reg delete HKCU\Software\MTPScriptContexts\%1\%2 nuldel %RET%

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem SRAND procedurerem Seed the random number generatorremrem Arguments: %1=new seed valuerem:SRAND%TRACE% [proc %0 %*]

    set /a _MTPLIB_NEXTRAND=%1goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem RAND procedurerem Get next random number (0 to 32767)remrem Returns: RET=next random numberrem:RAND%TRACE% [proc %0 %*]

    if not defined _MTPLIB_NEXTRAND set /a _MTPLIB_NEXTRAND=1

  • 7/30/2019 Batch Esempio

    10/12

    set /a _MTPLIB_NEXTRAND=_MTPLIB_NEXTRAND * 214013 + 2531011set /a RET=_MTPLIB_NEXTRAND ^>^> 16 ^& 0x7FFF

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem RESOLVE procedurerem Fully resolve all indirect variable references in RET variableremrem Arguments: RET=value to resolveremrem Returns: RET=as passed in, with references resolvedrem:RESOLVE%TRACE% [proc %0 %*]

    :RESOLVELOOPif "%RET%"=="" goto :EOFset RET1=%RET%for /f "tokens=*" %%I in ('echo %RET%') do set RET=%%I

    if not "%RET%"=="%RET1%" goto :RESOLVELOOPgoto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem GETINPUTLINE procedurerem Get a single line of keyboard input

    remrem Returns: RET=Entered linerem:GETINPUTLINE%TRACE% [proc %0 %*]

    call :GETTEMPNAMEset _MTPLIB_T1=%RET%copy con "%_MTPLIB_T1%" >nulfor /f "tokens=*" %%I in ('type "%_MTPLIB_T1%"') do set RET=%%Iif exist "%_MTPLIB_T1%" del "%_MTPLIB_T1%"set _MTPLIB_T1=

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem GETSYNCFILE procedurerem Get a sync file name (file will not exist)remrem Returns: RET=Name of sync file to userem:GETSYNCFILE%TRACE% [proc %0 %*]

    call :GETTEMPNAMEgoto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem SETSYNCFILE procedure

    rem Flag sync event (creates the file)remrem Arguments: %1=sync filename to flagrem:SETSYNCFILE%TRACE% [proc %0 %*]

    echo . >%1goto :EOF

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

  • 7/30/2019 Batch Esempio

    11/12

    rem DELSYNCFILE procedurerem Delete sync fileremrem Arguments: %1=sync filenamerem:DELSYNCFILE%TRACE% [proc %0 %*]

    if exist %1 del %1goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem WAITSYNCFILErem Wait for sync file to flagremrem Arguments: %1=sync filenamerem %2=timeout in seconds (def: 60)remrem Returns: RET=Timeout remaining, or 0 if timeoutrem:WAITSYNCFILE%TRACE% [proc %0 %*]

    if {%2}=={} (set /a RET=60) else (set /a RET=%2)if exist %1 goto :EOF:WAITSYNCFILELOOP

    sleep 1set /a RET-=1if %RET% GTR 0 if not exist %1 goto :WAITSYNCFILELOOP

    goto :EOF

    rem /////////////////////////////////////////////////////////////////////////rem GETTEMPNAME procedurerem Create a temporary file nameremrem Returns: RET=Temporary file namerem:GETTEMPNAME%TRACE% [proc %0 %*]

    if not defined _MTPLIB_NEXTTEMP set /a _MTPLIB_NEXTTEMP=1if defined TEMP ((set RET=%TEMP%)

    ) else if defined TMP ((set RET=%TMP%)

    ) else (set RET=%SystemRoot%):GETTEMPNAMELOOP

    set /a _MTPLIB_NEXTTEMP=_MTPLIB_NEXTTEMP * 214013 + 2531011set /a _MTPLIB_T1=_MTPLIB_NEXTTEMP ^>^> 16 ^& 0x7FFFset RET=%RET%\~SH%_MTPLIB_T1%.tmp

    if exist "%RET%" goto :GETTEMPNAMELOOPset _MTPLIB_T1=

    goto :EOF

    rem These must be the FINAL LINES in the script...:DOSEXITecho This script requires Windows NT

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

  • 7/30/2019 Batch Esempio

    12/12