@echo OFF @if not "%ECHO%"=="" echo %ECHO% @if not "%OS%"=="Windows_NT" goto DOSEXIT REM NT-CMD.cmd -- NT Command Script Demo and Reference REM v1.0 12-Aug-1998 JPV REM v1.1 13-Aug-1998 JPV Minor updates REM v1.2 29-Aug-2001 JPV Added date/time stuff REM See _Windows_NT_Shell_Scripting_ by Tim Hill, MacMillan, 1998 REM Set some debuging stuff, and check to see if we need to show help if "%DEBUG%"=="1" (set TRACE=echo) else (set TRACE=rem) if /i {%1}=={/?} (call :HELP %2) & (goto :HELPEXIT) REM Make sure the external file we need (reg.exe) is in the path set InPath= echo if exist %%~$PATH:1 set InPath=%%~$PATH:1 > _InPath.cmd call _InPath.cmd reg.exe del _InPath.cmd > NUL rem if not defined InPath echo. & echo %0 Fatal Error: Reg.exe not in Path! & goto :EOF REM Main Menu :Menu cls if defined TRACE %TRACE% [Main %0 %*] echo. echo %~n0 -- NT Command Processor (cmd.exe) Demo (C) 1998 JP Vossen echo ---------------------------------------------------------------- echo. echo 1. Set the CMD.exe Command Completion Character to [TAB] (Registry) echo. echo 2. Set a Legal Notice on Login, and do not display the last Logged on User echo. echo 3. Display a Command Redirection ^& Reserved Characters Reference echo. echo 4. Display a Multiple Commands Reference and Examples echo. echo 5. Advanced SET commands and examples echo. echo 6. State and Scope Commands echo. echo 7. Advanced IF and FOR Commands echo. echo 8. [Security] Automation Notes echo. echo 9. Show Date/Time echo. echo 0. EXIT echo. choice /c:1234567890 /n Choose one. REM Could have done this: if errorlevel 4 goto :Sub_%ERRORLEVEL% REM Remember, the errorlevel returned is the position of the menu option, not it's value! REM Thus, the errorlevel returned for a "9. Exit" is not necessarily 9... goto :Sub_%ERRORLEVEL% goto :EOF :Sub_1 cls if defined TRACE %TRACE% [Sub_1 %0 %*] echo Set the CMD.exe Command Completion Character to [TAB] echo ----------------------------------------------------- echo. REM Set the cmd.exe Command Completion Character to a [TAB], for the Current and .Default Users REM Requires reg.exe from the NT Resource Kit, which seems to have a bug that REM ignores the "/f" switch when not dealing with remote computers, and another REM Bug that disallows use of 32-bit numbers during the "update" command, so you have REM to delete then add to change it. echo y | reg delete "HKCU\Software\Microsoft\Command Processor\CompletionChar" reg add "HKCU\Software\Microsoft\Command Processor\CompletionChar=9" REG_DWORD echo y | reg delete "HKU\.Default\Software\Microsoft\Command Processor\CompletionChar" reg add "HKU\.Default\Software\Microsoft\Command Processor\CompletionChar=9" REG_DWORD echo. echo. echo HKCU\Software\Microsoft\Command Processor\CompletionChar should now be 0x9 echo. reg query "HKU\.Default\Software\Microsoft\Command Processor\CompletionChar" echo. pause goto :Menu :Sub_2 cls if defined TRACE %TRACE% [Sub_2 %0 %*] echo Set a Legal Notice on Login, and do not display the last Logged on User echo ----------------------------------------------------------------------- echo. REM Set a Logon Legal Notice reg update "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LegalNoticeCaption=My MsgBox Title" reg update "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LegalNoticeText=My Legal Notice" REM Disable Display of Last Logged on User reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DontDisplayLastUserName=1" REG_SZ echo. echo. echo Legal Notice and Display should be set: echo. reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LegalNoticeCaption" echo. reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LegalNoticeText" echo. reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DontDisplayLastUserName" echo. pause goto :Menu :Sub_3 cls if defined TRACE %TRACE% [Sub_3 %0 %*] echo. echo Command Redirection, ^& Reserved Characters echo ------------------------------------------ echo. echo (See also, Console Properties, Layout TAB, Screen Buffer Size) echo. echo ^> {file} Redirects STDOUT to the {file} specified, overwriting the file if echo it exists. echo. echo ^>^> {file} Redirects STDOUT to the {file} specified, appending to the file if echo it exists. echo. echo ^< {file} Redirects STDIN from the {files} specified. echo. echo 2^> {file} Redirects STDERR to the {file} specified, overwriting the file echo if it exists. echo. echo 2^>^&1 {file} Redirects STDOUT and STDERR to the {file} specified, overwriting echo the file if it exists. echo. echo. echo The following characters are reserved, and must be quoted ^(by ^^^) to be ECHOed: echo ^& ^| ^( ^) ^< ^> ^^ echo. pause goto :Menu :Sub_4 cls if defined TRACE %TRACE% [Sub_4 %0 %*] echo. echo Multiple Commands echo ----------------- echo. echo {cmd1} ^& {cmd1} Executes {cmd1} then {cmd2}. echo. echo {cmd1} ^&^& {cmd1} Executes {cmd1} then executes {cmd2} only if {cmd1} was echo successful. echo. echo {cmd1} ^|^| {cmd1} Executes {cmd1} then executes {cmd2} only if {cmd1} was echo NOT successful. echo. echo ^( ^) May be used to next complex commands, for multi-line echo commands and in "IF" ... "ELSE" structures. echo. echo Press any key for examples... pause > NUL cls echo. echo. echo e.g. echo 1 ^& echo 2 Echo 1, then Echo 2 echo 1 & echo 2 echo. echo e.g. Barf 2^> NUL ^&^& echo 2 If "Barf" works, then "Echo 2" Barf 2> NUL && echo 2 echo. echo. echo e.g. Barf 2^> NUL ^|^| echo 2 If "Barf" fails, then "Echo 2" Barf 2> NUL || echo 2 echo. echo. echo Note: since "Barf" does not exist (one hopes) it fails, generating an error echo to STDERR. Since we have redirected STDERR to NUL (the Bit Bucket), we echo don't see it and the display is cleaner. echo. pause goto :Menu :Sub_5 cls if defined TRACE %TRACE% [Sub_5 %0 %*] echo. echo Advanced SET Commands echo --------------------- echo. echo. echo ^<^<^< Mathmatical Evaluation ^(Seems to be Integer ONLY!^) ^>^>^> echo. echo set /a answer=2+2 set /a answer=2+2 echo answer: %answer% echo. echo. echo ^<^<^< Incrementation ^>^>^> echo. echo set /a counter += 1 set /a counter += 1 echo counter: %counter% echo. echo. echo ^<^<^< String Substitution ^>^>^> echo. echo set string=AAAbbbCCC set string=AAAbbbCCC echo string = %string% echo echo %%string:b=w%% echo string now = %string:b=w% echo. pause cls echo. echo ^<^<^< String Indexing ^(MID^$^) ^>^>^> echo. echo set string=1234567890 set string=1234567890 echo string = %string% echo echo %%string:~6,2%% echo string now = %string:~6,2% echo. echo. echo ^<^<^< Environment Variable Display ^>^>^> echo. echo set user set user echo. pause cls echo. echo ^<^<^< Parameter Qualification ^>^>^> echo. echo Parameters, such as %%0, %%1, %%2 ... %%9 can be qualified by the following: echo. echo f Expands to fully qualified path name echo d Expands to drive letter and colon only echo p Expands to path name only (no drive, filename, etc.) echo n Expands to file name only echo x Expands to leading period and file extension echo s Used with n or x, expands to short filename echo ^$var Returns the fully qualified path name of the file, if found in the path. echo. echo e.g. %%0 echo echo %0 echo. echo e.g. %%~d0 %%~p0 %%~n0 echo %~d0 %~p0 %~n0 echo. pause goto :Menu :Sub_6 cls if defined TRACE %TRACE% [Sub_6 %0 %*] echo. echo State and Scope Commands echo ------------------------ echo. echo PushD ^& PopD -- Push a Directory, or Pop a Directory on a stack. echo You pushd a directory, then change drives, directories, etc. Then echo you popd the directory, and you are back where you started. echo. echo Note: pushd {UNC Path} saves the current directory, then maps a drive to echo the UNC path and changes there. You can find out what drive was mapped by echo using the "CD" command. Popd returned to the saved directory, and unmaps the echo mapped drive. Drive mappings start from z: and work backwards, using the first echo available drive letter. (Very useful with the AT command.) echo. echo. echo SetLocal ^& EndLocal -- creates a copy of the environment, which may then be echo modified without affecting the parent enviroment. echo. echo. echo Note: both of the above commands may be nested. echo. pause goto :Menu :Sub_7 cls if defined TRACE %TRACE% [Sub_7 %0 %*] echo. echo Advanced IF Commands echo -------------------- echo. echo for %%x in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%x:\. echo Active Drive %%x: echo. echo. echo if [NOT] [/I] string1==string2 command (/i is case insensitve compare) echo. echo if [/I] value1 relop value2 command (reopl is a relative operator) echo EQU Equal to echo NQU Not Equal to echo LSS Less than echo LEQ Less than or Equal to echo GTR Greater than echo GEQ Greater than or Equal to echo. pause cls echo. echo Advanced IF Commands (continued) echo -------------------------------- echo. echo Multi-line IF echo. echo if not %%PROCESSOR_ARCHITECHTURE%%=="x86" ( echo if not %%PROCESSOR_ARCHITECHTURE%%=="MIPS" ( echo echo Intel or MIPS CPU required ^& goto :EOF echo ) echo ) echo if defined USERNAME ( echo echo A User is logged on ^& goto :loggedon echo ) else ( echo echo A User is not logged on ^& goto :notloggedon echo ) echo. pause cls echo. echo Advanced FOR Commands -- Iteration echo ---------------------------------- echo. echo FOR %%var IN (set) DO command File Iteration echo. echo e.g. for %%x in (c:\files\*.txt) do @type %%x echo. echo. echo FOR /D %%var IN (set) DO command Directory Iteration echo. echo e.g. for /d %%x in (c:\winnt) do @echo %%x echo. echo. echo FOR /L %var IN (start,step,end) DO command Numeric Iteration echo. echo e.g. for /l %%x in (1,1,10) do @echo %%x echo. echo. echo The following example echos all *.TTF files in any directory of c:\winnt: echo. echo for /d %%I in (c:\winnt\*) do @for %%J in ("%%i\*.ttf") do @echo %%J echo. pause cls echo. echo Advanced FOR Commands -- Text Parsing echo ------------------------------------- echo. echo FOR /F ["options"] %var IN (source) DO command echo. echo eol=c End of Line Character echo skip=nn Skip nn header lines echo delims=xxx xxx are the various delimiters echo tokens=ttt Specified which returned token to assign to variables echo. echo Note: If a [TAB] is a delimiter, you will need to enter "delims=^T", that is, echo "delims=" followed by the [TAB] key. echo. echo ;Sample Text file "Users.txt" echo Kirk,Admin,400 ; James Kirk echo Spock,Admin,400 ; Spock echo McCoy,Merdical,300 ; Lenard McCoy echo. echo for /f "eol=; toekns=1,3 delims=," %I in (users.txt) do @echo Username=%I Size=%J echo. echo. echo. pause goto :Menu :Sub_8 cls if defined TRACE %TRACE% [Sub_8 %0 %*] echo. echo [Security] Automation Notes echo --------------------------- echo. echo [RK] Denotes utilities from the Resource Kit echo. echo AuditPol [RK] = Set NT Server Auditing Policies echo Cacls, Xcacls [RK] = To View/modify NTFS permissions echo Drivers [RK] = Displays all installed Device Drivers echo DumpEL [RK] = Dumps formatted Event Logs to text echo Filever [RK] = Displays EXE ^& DLL version information echo GetMAC [RK] = Displays the MAC address of NIC(s) echo Local [RK], Global [RK], net groups = Show/manipulate groups/members echo Net Accounts = Set various account/password properties echo Net config {server} /hidden:yes = Hides a server from the Browse List echo Net send * "Message" = Send a message to all connected Users echo Net share = Shows all shares on a server echo PassProp [RK] = Set advanced password properties ^& Admin lockout echo RASList [RK] = Displays all RAS Servers echo RASUsers [RK] = Displays all User accounts with RAS Permission echo Sclist [RK] = Shows all services running on an NT computer echo ShowACLs [RK] = NTFS ONLY, Displays NTFS directory permissions echo SrvCheck [RK] = Displays information about non-hidden shares echo SrvInfo [RK] = Displays all kinds of server info, incl. drive, services, etc. echo UsrStat [RK] = Displays User stats such as last logon time echo. pause goto :Menu :Sub_9 cls if defined TRACE %TRACE% [Sub_9 %0 %*] echo. echo Date/Time Display echo ----------------- echo. for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set DayOfWeek=%%i set Month=%%j set Day=%%k set Year=%%l ) REM Needed for 4NT output rem if "%Month%" LSS "10" set Month=0%Month% rem if "%Day%" LSS "10" set Day=0%Day% for /F "tokens=1-4 delims=:" %%i in ('time /t') do ( set Hour=%%i set Minute=%%j set Second=%%k ) REM Needed for 4NT output rem if "%Hour%" LSS "10" set Hour=0%Hour% rem if "%Minute%" LSS "10" set Minute=0%Minute% rem if "%Second%" LSS "10" set Second=0%Second% echo. echo. echo DayOfWeek: %DayOfWeek% echo Month: %Month% echo Day: %Day% echo Year: %Year% echo. echo Hour: %Hour% (note: leading space but no leading zero, 12 hour :-( echo Minute: %Minute% rem echo Second: %Second% echo. echo. rem echo Date: %Year%-%Month%-%Day%-%Hour%:%Minute%.%Second echo Date: %Year%-%Month%-%Day% %Hour%:%Minute% echo. echo. pause goto :Menu :HELP if defined TRACE %TRACE% [Help %0 %*] echo. echo Usage: %0 echo. goto :EOF rem These must be the FINAL LINES in the script... :DOSEXIT echo $0 requires Windows NT... :Sub_10 if defined TRACE %TRACE% [Sub_9 %0 %*]