Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for adshelp.bat

(view source code of adshelp.bat as plain text)

  1. @ECHO OFF
  2. :: Use DISABLEDELAYEDEXPANSION to enable writing exclamation marks
  3. IF "%OS%"=="Windows_NT" SETLOCAL DISABLEDELAYEDEXPANSION
  4.  
  5. :: Version number for this batch file
  6. SET MyVer=2.11
  7.  
  8. :: Display "about"
  9. ECHO.
  10. ECHO ADSHelp.bat,  Version %MyVer% for Windows Server 2003
  11. ECHO Generate an HTML help file for the Directory Service command line tools
  12. ECHO.
  13. ECHO Written by Rob van der Woude
  14. ECHO http://www.robvanderwoude.com
  15. ECHO.
  16. ECHO Text find and replace script from TechNet Script Center Hey, Scripting Guy!'s
  17. ECHO article "How Can I Find and Replace Text in a Text File?"
  18. ECHO http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx
  19.  
  20. ECHO.
  21.  
  22. :: For MS-DOS and Windows 9x the script ends here
  23. IF NOT "%OS%"=="Windows_NT" EXIT
  24.  
  25. :: Create temporary script
  26. CALL :CreateVBScript "%Temp%\replace.vbs"
  27.  
  28. ECHO Writing HTML header . . .
  29.  
  30. >  adshelp.htm ECHO ^<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"^>
  31. >> adshelp.htm ECHO ^<html^>
  32. >> adshelp.htm ECHO ^<head^>
  33.  
  34. :: Read Windows version using VER command
  35. FOR /F "tokens=1 delims=[" %%A IN ('VER') DO SET Ver=%%A
  36. FOR /F "tokens=1* delims= " %%A IN ('ECHO.%Ver%') DO SET Ver=%%B
  37.  
  38. :: Read latest Service Pack from registry
  39. CALL :GetSP
  40.  
  41. >> adshelp.htm ECHO ^<title^>Help for Directory Service command line tools^</title^>
  42. >> adshelp.htm ECHO ^<meta name="generator" content="ADSHelp.bat, Version %MyVer%, by Rob van der Woude"^>
  43. >> adshelp.htm ECHO ^</head^>
  44. >> adshelp.htm ECHO.
  45. >> adshelp.htm ECHO ^<body^>
  46. >> adshelp.htm ECHO.
  47. >> adshelp.htm ECHO ^<div align="center"^>
  48. >> adshelp.htm ECHO ^<h2^>Help for^</h2^>
  49. >> adshelp.htm ECHO ^<h1^>Directory Service command line tools^</h1^>
  50. >> adshelp.htm ECHO ^<h2^>%Ver%%SP%^</h2^>
  51. FOR /F "tokens=* delims=" %%A IN ('VER') DO SET Ver=%%A
  52. >> adshelp.htm ECHO ^<h3^>%Ver%^</h3^>
  53. >> adshelp.htm ECHO ^</div^>
  54. >> adshelp.htm ECHO.
  55. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  56. >> adshelp.htm ECHO.
  57.  
  58. ECHO Creating command index table . . .
  59. SET FirstCell=1
  60. >> adshelp.htm ECHO ^<table class="CommandIndexTable"^>
  61. SET Lines=1
  62. SET DSCmd=
  63. FOR %%A IN (DSADD DSGET DSMOD DSMOVE DSQUERY DSRM) DO (
  64. 	FOR /F "tokens=* delims=" %%B IN ('%%A /? 2^>^&1 ^| FIND /V "/?"') DO (
  65. 		CALL :ListExec "%%~A" "%%~B"
  66. rem 2>NUL
  67. 	)
  68. )
  69. >> adshelp.htm ECHO ^</table^>
  70. >> adshelp.htm ECHO.
  71. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  72. >> adshelp.htm ECHO.
  73. >> adshelp.htm ECHO ^<div align="center"^>^<a href="#"^>Back to the top of this page^</a^>^</div^>
  74. >> adshelp.htm ECHO.
  75. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  76. >> adshelp.htm ECHO.
  77.  
  78. ECHO Creating help for individual commands . . .
  79. FOR %%A IN (DSADD DSGET DSMOD DSMOVE DSQUERY DSRM) DO CALL :ListHelp %%A
  80.  
  81. ECHO Closing HTML file . . .
  82.  
  83. >> adshelp.htm ECHO.
  84. >> adshelp.htm ECHO ^<div align="center"^>
  85. >> adshelp.htm ECHO.
  86. >> adshelp.htm ECHO ^<p^>More detailed descriptions on Microsoft's Windows Server 2003 Directory Service command line tools can be found
  87. >> adshelp.htm ECHO ^<a href="http://support.microsoft.com/kb/322684" target="_blank"^>here^</a^>^</p^>
  88. >> adshelp.htm ECHO.
  89. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  90. >> adshelp.htm ECHO.
  91. >> adshelp.htm ECHO ^<p^>This HTML help file was generated by:^<br^>
  92. >> adshelp.htm ECHO ^<strong^>ADSHelp.bat^</strong^>, Version %MyVer%
  93. >> adshelp.htm ECHO for Windows 2003 Server^<br^>
  94. >> adshelp.htm ECHO Written by Rob van der Woude^<br^>
  95. >> adshelp.htm ECHO ^<a href="http://www.robvanderwoude.com"^>http://www.robvanderwoude.com^</a^>^<br^>
  96. >> adshelp.htm ECHO ^&nbsp;^<br^>
  97. >> adshelp.htm ECHO Text find and replace script by TechNet Script Center's
  98. >> adshelp.htm ECHO ^<a href="http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx" target="_blank"^>Hey, Scripting Guy!^</a^>^</p^>
  99. >> adshelp.htm ECHO.
  100. >> adshelp.htm ECHO ^</div^>
  101. >> adshelp.htm ECHO.
  102. >> adshelp.htm ECHO ^</body^>
  103. >> adshelp.htm ECHO ^</html^>
  104.  
  105. ECHO.
  106. ECHO Now starting display of "adshelp.htm" . . .
  107. START "ADSHelp" adshelp.htm
  108.  
  109. :: End of main batch program
  110. ENDLOCAL
  111. GOTO:EOF
  112.  
  113.  
  114. :: Subroutines
  115.  
  116.  
  117. :AddLink
  118. FOR /F "tokens=1 delims=-" %%a IN (%1) DO SET DsDetailCmd=%%a
  119. SET DsDetailCmd=%DsDetailCmd:~0,-1%
  120. SET LoCase=%DsDetailCmd%
  121. CALL :UpCase
  122. SET DsDetailCmd=%UpCase%
  123. SET DsDetailCmd=%DsDetailCmd: =_%
  124. SET DsDetailCmd=%DsDetailCmd:^*=Gen%
  125. START /B /WAIT CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" "%LoCase% - " "<a href="""#%DsDetailCmd%""">%DsDetailCmd%</a> - "
  126. GOTO:EOF
  127.  
  128.  
  129. :CreateVBScript
  130. >  "%~1" ECHO ' Text find and replace script from TechNet Script Center's
  131. >> "%~1" ECHO ' "Hey, Scripting Guy!"'s article "How Can I Find and Replace Text in a Text File?"
  132. >> "%~1" ECHO ' http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx
  133. >> "%~1" ECHO '
  134. >> "%~1" ECHO ' Usage:  CSCRIPT REPLACE.VBS "d:\path\textfile.txt" "oldtext" "newtext"
  135. >> "%~1" ECHO.
  136. >> "%~1" ECHO Const ForReading = 1
  137. >> "%~1" ECHO Const ForWriting = 2
  138. >> "%~1" ECHO.
  139. >> "%~1" ECHO strFileName = Wscript.Arguments^(0^)
  140. >> "%~1" ECHO strOldText  = Wscript.Arguments^(1^)
  141. >> "%~1" ECHO strNewText  = Wscript.Arguments^(2^)
  142. >> "%~1" ECHO.
  143. >> "%~1" ECHO Set objFSO  = CreateObject^( "Scripting.FileSystemObject" ^)
  144. >> "%~1" ECHO Set objFile = objFSO.OpenTextFile^( strFileName, ForReading ^)
  145. >> "%~1" ECHO.
  146. >> "%~1" ECHO strText     = objFile.ReadAll
  147. >> "%~1" ECHO objFile.Close
  148. >> "%~1" ECHO strNewText  = Replace^( strText, strOldText, strNewText ^)
  149. >> "%~1" ECHO.
  150. >> "%~1" ECHO Set objFile = objFSO.OpenTextFile^( strFileName, ForWriting ^)
  151. >> "%~1" ECHO objFile.WriteLine strNewText
  152. >> "%~1" ECHO objFile.Close
  153. GOTO:EOF
  154.  
  155.  
  156. :ListExec
  157. IF /I NOT "%~1"=="%DSCmd%" (
  158. 	SET Descr=
  159. 	SET DSCmd=%~1
  160. 	SET Lines=1
  161. )
  162. IF %Lines%==0 GOTO:EOF
  163. IF "%~2"=="" (
  164. 	SET Lines=0
  165. 	GOTO:EOF
  166. )
  167. FOR /F "tokens=1 delims=." %%C IN ('ECHO.%~2') DO IF DEFINED Descr (SET Descr=%Descr% %%C) ELSE (SET Descr=%%C)
  168. ECHO.%2 | FIND "." >NUL
  169. IF NOT ERRORLEVEL 1 (
  170. 	FOR /F "tokens=1* delims=:" %%C IN ('ECHO.%Descr%') DO IF NOT "%%D"=="" SET Descr=%%D
  171. 	SET Lines=0
  172. )
  173. IF %Lines%==0 (
  174. 	>> adshelp.htm ECHO ^<tr^>
  175. 	>> adshelp.htm ECHO     ^<td^>^<a href="#%DSCmd%"^>%DSCmd%^</a^>^</td^>
  176. 	>> adshelp.htm ECHO     ^<td^>^&nbsp;^</td^>
  177. 	>> adshelp.htm ECHO     ^<td^>^%Descr%^</td^>
  178. 	>> adshelp.htm ECHO ^</tr^>
  179. )
  180. GOTO:EOF
  181.  
  182.  
  183. :ListHelp
  184. SET DsHelpCmd=%~1
  185. ECHO  . . . %DsHelpCmd%
  186. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  187. >> adshelp.htm ECHO.
  188. >> adshelp.htm ECHO ^<h2 id="%DsHelpCmd%"^>%DsHelpCmd%^</h2^>
  189. >> adshelp.htm ECHO.
  190. >> adshelp.htm ECHO ^<pre^>
  191.  
  192. :: Write help screen to temporary file
  193. IF EXIST "%Temp%\adshelp.tmp" DEL "%Temp%\adshelp.tmp"
  194. %DsHelpCmd% /? > "%Temp%\adshelp.tmp" 2>NUL
  195. :: "Escape" special characters in temporary file using temporary VBScript
  196. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" "&" "&amp;"
  197. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" "<" "&lt;"
  198. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" ">" "&gt;"
  199. :: Insert hyperlinks:
  200. FOR /F "tokens=* delims=" %%A IN ('TYPE "%Temp%\adshelp.tmp" ^| FINDSTR /R /B /I /C:"%DsHelpCmd% [a-z][a-z][a-z]* - [a-z]"') DO (
  201. 	CALL :AddLink "%%~A"
  202. )
  203. :: Append "escaped" text
  204. >> adshelp.htm TYPE "%Temp%\adshelp.tmp"
  205.  
  206. >> adshelp.htm ECHO ^</pre^>
  207. >> adshelp.htm ECHO.
  208. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  209. >> adshelp.htm ECHO.
  210. IF /I NOT "%~1"=="DSGET" IF /I NOT "%~1"=="DSMOVE" IF /I NOT "%~1"=="DSRM" (
  211. 	FOR /F "tokens=1* delims=-" %%B IN ('%DsHelpCmd% OU /? 2^>NUL ^| FINDSTR /R /I /B /C:"%~1 [^ -][^ -]* /\? - "') DO (
  212. 		CALL :ListDetails "%%~B" "%%~C"
  213. 	)
  214. )
  215. IF /I "%~1"=="DSGET" (
  216. 	FOR /F "tokens=1* delims=-" %%B IN ('%DsHelpCmd% OU /? 2^>NUL ^| FINDSTR /R /I /B /C:"%~1 [^/ -][^\? -]* - "') DO (
  217. 		CALL :ListDetails "%%B~" "%%~C"
  218. 	)
  219. )
  220. >> adshelp.htm ECHO.
  221. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  222. >> adshelp.htm ECHO.
  223. >> adshelp.htm ECHO ^<div align="center"^>^<a href="#"^>Back to the top of this page^</a^>^</div^>
  224. >> adshelp.htm ECHO.
  225. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  226. >> adshelp.htm ECHO.
  227. GOTO:EOF
  228.  
  229.  
  230. :ListDetails
  231. >> adshelp.htm ECHO ^<table class="ListDetailsTable"^>
  232. >> adshelp.htm ECHO ^<tr^>
  233. FOR /F "tokens=1,2" %%D IN ('ECHO.%~1') DO SET Detail=%%D %%E
  234. ECHO        %Detail% . . .
  235. :: Convert Detail string to uppercase
  236. SET LoCase=%Detail%
  237. CALL :UpCase
  238. SET Detail=%UpCase%
  239. SET URL=%Detail: =_%
  240. FOR /F "tokens=1,2" %%D IN ('ECHO.%Detail%') DO IF "%%E"=="*" (SET URL=%Detail:^*=GEN%)
  241. >> adshelp.htm ECHO     ^<th^>^<a id="%URL%"^>%Detail%^</a^>^</th^>
  242. >> adshelp.htm ECHO ^</tr^>
  243. >> adshelp.htm ECHO ^<tr^>
  244. SET Descr=%~2
  245. ECHO.%1 | FIND /I "dsquery *" >NUL
  246. IF ERRORLEVEL 1 (
  247. 	>> adshelp.htm ECHO     ^<td^>^%Descr%^</td^>
  248. ) ELSE (
  249. 	CALL :DsQueryGen
  250. )
  251. >> adshelp.htm ECHO ^</tr^>
  252. >> adshelp.htm ECHO ^</table^>
  253. >> adshelp.htm ECHO.
  254. >> adshelp.htm ECHO.%Detail%
  255. >> adshelp.htm ECHO.
  256. >> adshelp.htm ECHO ^<pre^>
  257.  
  258. :: Write help screen to temporary file
  259. IF EXIST "%Temp%\adshelp.tmp" DEL "%Temp%\adshelp.tmp"
  260. %Detail% /? > "%Temp%\adshelp.tmp" 2>&1
  261. :: "Escape" special characters in temporary file using temporary VBScript
  262. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" "&" "&amp;"
  263. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" "<" "&lt;"
  264. CSCRIPT //NoLogo "%Temp%\replace.vbs" "%Temp%\adshelp.tmp" ">" "&gt;"
  265. :: Append "escaped" text
  266. >> adshelp.htm TYPE "%Temp%\adshelp.tmp"
  267. >> adshelp.htm ECHO ^</pre^>
  268. >> adshelp.htm ECHO.
  269. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  270. >> adshelp.htm ECHO.
  271. >> adshelp.htm ECHO ^<div align="center"^>^<a href="#%DsHelpCmd%"^>Back to %DsHelpCmd%^</a^>^</div^>
  272. >> adshelp.htm ECHO.
  273. >> adshelp.htm ECHO ^<p^>^&nbsp;^</p^>
  274. >> adshelp.htm ECHO.
  275. GOTO:EOF
  276.  
  277.  
  278. :DsQueryGen
  279. :: DSQUERY * description is displayed on 2 lines, so we need to add an extra line to the description
  280. FOR /F "tokens=1* delims=[]" %%E IN ('DSQUERY OU /? 2^>NUL ^| FIND /N /V "X@#$X" ^| FIND /I "dsquery * /?"') DO SET LineNum=%%E
  281. SET /A NextLineNum = %LineNum% + 1
  282. FOR /F "tokens=1* delims=[]" %%E IN ('DSQUERY OU /? 2^>NUL ^| FIND /N /V "X@#$X" ^| FIND "[%NextLineNum%]"') DO SET NextLine=%%F
  283. SET Descr=%Descr% %NextLine%
  284. >> adshelp.htm ECHO     ^<td^>^%Descr%^</td^>
  285. GOTO:EOF
  286.  
  287.  
  288. :GetSP
  289. SET SP=
  290. :: Export registry tree to temporary file
  291. START /WAIT REGEDIT.EXE /E "%Temp%.\%~n0.dat" "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
  292. IF NOT EXIST "%Temp%.\%~n0.dat" GOTO:EOF
  293. :: Read value of "CSDVersion" from temporary file
  294. FOR /F "tokens=2 delims==" %%A IN ('TYPE "%Temp%.\%~n0.dat" ^| FIND /I "CSDVersion"') DO SET SP=%%~A
  295. :: Check if value is valid
  296. ECHO.%SP% | FIND /I "Service Pack" >NUL
  297. IF ERRORLEVEL 1 SET SP=
  298. DEL "%Temp%.\%~n0.dat"
  299. :: Use a shorter notation
  300. IF DEFINED SP SET SP=%SP:Service Pack=SP%
  301. GOTO:EOF
  302.  
  303.  
  304. :UpCase
  305. SET UpCase=%LoCase%
  306. SET Detail=%UpCase:a=A%
  307. SET Detail=%UpCase:b=B%
  308. SET Detail=%UpCase:c=C%
  309. SET Detail=%UpCase:d=D%
  310. SET Detail=%UpCase:e=E%
  311. SET Detail=%UpCase:f=F%
  312. SET Detail=%UpCase:g=G%
  313. SET Detail=%UpCase:h=H%
  314. SET Detail=%UpCase:i=I%
  315. SET Detail=%UpCase:j=J%
  316. SET Detail=%UpCase:k=K%
  317. SET Detail=%UpCase:l=L%
  318. SET Detail=%UpCase:m=M%
  319. SET Detail=%UpCase:n=N%
  320. SET Detail=%UpCase:o=O%
  321. SET Detail=%UpCase:p=P%
  322. SET Detail=%UpCase:q=Q%
  323. SET Detail=%UpCase:r=R%
  324. SET Detail=%UpCase:s=S%
  325. SET Detail=%UpCase:t=T%
  326. SET Detail=%UpCase:u=U%
  327. SET Detail=%UpCase:v=V%
  328. SET Detail=%UpCase:w=W%
  329. SET Detail=%UpCase:x=X%
  330. SET Detail=%UpCase:y=Y%
  331. SET Detail=%UpCase:z=Z%
  332. GOTO:EOF
  333.  

page last modified: 2024-04-16; loaded in 0.0276 seconds