<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">

<channel>
	<title>Case Else:</title>
	
	<link>http://caseelse.net</link>
	<description>/*** Code's last stand ***/</description>
	<pubDate>Mon, 15 Sep 2008 16:10:21 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/CaseElse" type="application/rss+xml" /><item>
		<title>Matching Arbitrary Name Lists in Active Directory</title>
		<link>http://caseelse.net/2008/09/14/matching-arbitrary-name-lists-in-active-directory/</link>
		<comments>http://caseelse.net/2008/09/14/matching-arbitrary-name-lists-in-active-directory/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 21:57:55 +0000</pubDate>
		<dc:creator>Neil</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://caseelse.net/?p=49</guid>
		<description><![CDATA[This script will take a CSV file of two columns — ostensibly in &#8216;last-name, first-name&#8217; format — and attempt to convert it into valid userIDs, despite irregularities in the list.

I frequently receive requests to automate the retrieval of user IDs for a list of arbitrary names from Active Directory. The name list largely consists of [...]]]></description>
			<content:encoded><![CDATA[<p>This <img class="size-medium wp-image-16" style="float: right; margin-left:10;margin-top:0; display:inline;" title="Hello, My Name Is sticker" src="http://caseelse.net/wp-content/uploads/2008/hellomynameis210.png" alt="Hello, My Name is sticker" />script will take a CSV file of two columns — ostensibly in &#8216;last-name, first-name&#8217; format — and attempt to convert it into valid userIDs, despite irregularities in the list.</p>
<p><span id="more-49"></span><br />
I frequently receive requests to automate the retrieval of user IDs for a list of arbitrary names from Active Directory. The name list largely consists of last names followed by first names, but some of the names are reversed. Some of the names are nicknames or initials.</p>
<p>In our example case, the names in Active Directory aren&#8217;t properly normalized either. Merging AD Domains from multiple, older networks often results in non-normalized data (or more specifically, differently normalized data). Names may be entered as proper names or nicknames, and nicknames may be notated in ad-hoc formats (e.g., in parentheses, etc). Names may be initialed—especially middle names—and abbreviations may or may not have periods. Last names may have suffixes (Jr, III, etc.) in a variety of formats.</p>
<p>Because the names in AD are not normalized, plus the arbitrary state of the names list we have to match, there is no way that I know of for a script to reliably normalize the list or the database, let alone compare them both. We could attempt to build-in dictionaries of names and compare them, but we would still fail on corner cases.</p>
<p>For example, if I tell you a full name is some arrangement of &#8216;Ernie&#8217;, &#8216;Ford&#8217;, and &#8216;Tennessee&#8217;, how can you guarantee what order the partial names are? While you may be able to make an educated guess, how do you write a script that guesses correctly? What about &#8216;John Olivia Newton&#8217;? Even if you built a dictionary of English names, what if I gave you &#8216;Ali Muhammad&#8217; with no specified order?</p>
<p>No matter how we attempt to automate matching these names to the data, there will still have to be a manual reckoning. The goal for the script is to make that manual process as simple and automatic as possible so that it can be done quickly, efficiently, and—most importantly—by somebody else. The goal isn&#8217;t to get 100% accuracy—80% would be nice with some lists. On the other hand, manually resolving 20% of a list of thousands of names is still a big job, but the 80% saving is huge.</p>
<p>If you have such a file, all you actually have to do is drop it onto this script, and the script will create an output file in the same folder. You can also pass the file name as a command-line parameter. The script doesn&#8217;t take extra parameters—the output file will be the input filename with &#8216;.out.tab&#8217; tacked onto it, etc.</p>
<p>Name dumps typically mix up first and last names, so this script tries to match names in both directions. The exception to that is the simplest translation. If &#8216;Smith, Andrew&#8217; is in the file, and matches to &#8216;Smith, Andrew&#8217; in AD, there is a strong indication (the comma) that the names are in the same order. That&#8217;s considered a quick match and warrants no further resolution.</p>
<p>If the simple translation fails, the script begins to run queries against AD, successively truncating the names (attempting to remove anything like initials, suffixes, etc.), and using them as wildcards until something is found, and eventually trying pieces of the names if necessary. The output includes profile information, as available, to help identify users.</p>
<p><strong>Caveats</strong></p>
<ul>
<li>Quick matches run through a fast translation (the AD NameTranslate object built into XP). There is some room for error; if the names have duplicates, for example (note Gene Cox in the list below). If this assumption of accuracy doesn&#8217;t work for your situation, you can disable that function (set it to just return an empty string).</li>
<li>There is no realistic way for a script to know that &#8216;Bob Smith&#8217; could be listed as &#8216;Robert Smith&#8217;, so it keeps broadening the net until something becomes plausible. If there is already a ‘B. Smith’ of some other name, broader scans will not run, so Robert may never show in the list.</li>
<li>Some queries (such as Bob Brown or Will Smith) may return too many names to be useful. There is a configurable threshold for that eventuality.</li>
</ul>
<p>You will notice that the output file is a .tab (which is probably an unrecognized type on your system) and that it looks oddly formatted. The file is designed so that you can run through the output fairly rapidly in a text editor and delete extraneous names, leaving the correct user IDs. Open the remainder in Excel (just right-click the file, and open it with Excel, in most cases), and all of the user IDs are in the third column—so even though editing is a manual process, it’s still relatively quick and easy.</p>
<p>Attached is a sample of the output, with mostly trouble names included.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p49code3'); return false;">View Code</a> TXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p493"><td class="code" id="p49code3"><pre class="txt" style="font-family:monospace;">Wesley Walker	=	DOMAIN\UserName
Betn Gotham	=	DOMAIN\UserName
MacLarsen, William C. (Carlson) = ...
      {searching for 'MacLarsen' &amp; 'William C. (Carlson)'}
      Carlson MacLarsen (Carlson MacLarsen Project Office) (MAC Services - Office) =	UserName
Ferrous, B D = ...
      {searching for 'Ferrous' &amp; 'B D'}
      Ferrous, Brandon (Desktop Ops) =	UserName
      Ferrous, BD (Corporate Services) =	UserName
O'Malley, Katherine = ...
      {searching for 'O''Malley' &amp; 'Katherine'}
      Katherine O'Malley (Company User Administration) (Company Administration) =	UserName
      Katherine O'Malley (Network &amp;amp; Server) =	UserName
Thistle, Bob = ...
      {searching for 'Thistle' &amp; 'Bob'}
      did not find 'B* Thistle' or 'B* Bob'
      Broadening Search...
      {searching for 'Thistle'}
      Robert Thistle (Network &amp; Server Group) =	UserName
      Thistle, Alex (Michael Robert) =	UserName
      {searching for 'Bob'}
      did not find '* Bob'
White, Clarence = ...
      {searching for 'White' &amp; 'Clarence'}
      Found 57 of 'C* White' or 'B* Clarence'. 30 is too many.
Garofolo, Gerald = ...
      {searching for 'Garofolo' &amp; 'Gerald'}
      Jerry Garofolo (Project Coordinator) (Business Support) =	UserName
Gene Cox      =     DOMAIN\ADMIN
Priestley Jr, Carl = ...
      {searching for 'Priestley Jr' &amp; 'Carl'}
      did not find 'C* Priestley Jr' or 'C* Carl'
      Broadening Search...
      {searching for 'Priestley Jr'}
      did not find '* Priestley Jr'
      Trying just the first part (Priestley)
      {searching for 'Priestley*'}
      Nick Priestley =	UserName
      Priestley, Carl (Legacy Support)  =	UserName
      {searching for 'Carl'}
      Kelly Carl (HR) =	UserName
      John Carl (HR) =	UserName</pre></td></tr></table></div>

<p>Below is the script:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p49code4'); return false;">View Code</a> VB</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p494"><td class="code" id="p49code4"><pre class="vb vb" style="font-family:monospace;"><span style="color: #b1b100;">Option</span> <span style="color: #b1b100;">Explicit</span>
&nbsp;
<span style="color: #b1b100;">Const</span> LDAPOU = <span style="color: #ff0000;">&quot;dc=domain,dc=Company,dc=NET&quot;</span>
<span style="color: #b1b100;">Const</span> TOOMANYCHOICES=<span style="color: #cc66cc;">30</span>
&nbsp;
<span style="color: #b1b100;">Const</span> ForReading = <span style="color: #cc66cc;">1</span>
<span style="color: #b1b100;">Const</span> ForWriting = <span style="color: #cc66cc;">2</span>
&nbsp;
<span style="color: #b1b100;">dim</span> objArgs
<span style="color: #b1b100;">Set</span> objArgs = WScript.<span style="color: #66cc66;">Arguments</span>
<span style="color: #b1b100;">If</span> objArgs.<span style="color: #b1b100;">count</span> &lt; <span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">Then</span>
	wscript.<span style="color: #66cc66;">echo</span> <span style="color: #ff0000;">&quot;Usage: userstocomputers.vbs input.csv&quot;</span>
	wscript.<span style="color: #66cc66;">quit</span>
<span style="color: #b1b100;">End</span> <span style="color: #b1b100;">If</span>
&nbsp;
<span style="color: #b1b100;">Dim</span> strInputFileName, strOutputFileName
&nbsp;
strInputFileName = objArgs<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>
strOutputFileName=strInputFileName &amp; <span style="color: #ff0000;">&quot;.tab&quot;</span>
&nbsp;
<span style="color: #b1b100;">Dim</span> objFSO, objInputFile, objOutputFile, szScratch, szScratch1
<span style="color: #b1b100;">Set</span> objFSO = <span style="color: #b1b100;">CREATEOBJECT</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">Set</span> objInputFile = objFSO.<span style="color: #66cc66;">OpenTextFile</span><span style="color: #66cc66;">&#40;</span>strInputFileName,ForReading,<span style="color: #b1b100;">false</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">Set</span> objOutputFile = objFSO.<span style="color: #66cc66;">OpenTextFile</span><span style="color: #66cc66;">&#40;</span>strOutputFileName,ForWriting,<span style="color: #b1b100;">true</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">Dim</span> strLineText, objSystemSet, objSystem
<span style="color: #b1b100;">While</span> <span style="color: #b1b100;">not</span> objInputFile.<span style="color: #66cc66;">AtEndOfStream</span>
&nbsp;
	strLineText = <span style="color: #b1b100;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">Trim</span><span style="color: #66cc66;">&#40;</span>objInputFile.<span style="color: #66cc66;">Readline</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;,&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">ubound</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#41;</span> &gt; 0 <span style="color: #b1b100;">then</span>
		strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>=<span style="color: #b1b100;">trim</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>=<span style="color: #b1b100;">trim</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		szScratch= strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot; &quot;</span> &amp; strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>
		szScratch1=TranslateADName<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">3</span>, szScratch<span style="color: #66cc66;">&#41;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>szScratch1<span style="color: #66cc66;">&#41;</span> &gt; 0 <span style="color: #b1b100;">then</span>
			objOutputFile.<span style="color: #66cc66;">WriteLine</span> szScratch &amp; <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;=&quot;</span> &amp; <span style="color: #b1b100;">vbtab</span> &amp; szScratch1
		<span style="color: #b1b100;">else</span>
			szScratch= strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;, &quot;</span> &amp; strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			szScratch1=TranslateADName<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">3</span>, szScratch<span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>szScratch1<span style="color: #66cc66;">&#41;</span> &gt; 0 <span style="color: #b1b100;">then</span>
				objOutputFile.<span style="color: #66cc66;">WriteLine</span> szScratch &amp; <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;=&quot;</span> &amp; <span style="color: #b1b100;">vbtab</span> &amp; szScratch1
			<span style="color: #b1b100;">else</span>
				objOutputFile.<span style="color: #66cc66;">WriteLine</span> szScratch &amp; <span style="color: #ff0000;">&quot; = ...&quot;</span>
&nbsp;
				<span style="color: #b1b100;">if</span> LookupADName<span style="color: #66cc66;">&#40;</span> strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">then</span>
					objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;Broadening Search...&quot;</span>
					<span style="color: #b1b100;">if</span> LookupADName<span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">then</span>
						<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>&gt;0 <span style="color: #b1b100;">then</span>
							objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;Trying just the first part (&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>,<span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;)&quot;</span>
							LookupADName <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>,<span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>&amp;<span style="color: #ff0000;">&quot;*&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>
						<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
					<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
					<span style="color: #b1b100;">if</span> LookupADName<span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">then</span>
						<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>&gt;0 <span style="color: #b1b100;">then</span>
							objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;Trying just the first part (&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;)&quot;</span>
							LookupADName <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #b1b100;">instr</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot; &quot;</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>&amp;<span style="color: #ff0000;">&quot;*&quot;</span>, <span style="color: #ff0000;">&quot;&quot;</span>
						<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
					<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
				<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
			<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
		<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
	<span style="color: #b1b100;">elseif</span> <span style="color: #b1b100;">ubound</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#41;</span> = 0 <span style="color: #b1b100;">then</span>
		strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>=<span style="color: #b1b100;">trim</span><span style="color: #66cc66;">&#40;</span>strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		szScratch1=strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>  &amp; <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;=&quot;</span> &amp; <span style="color: #b1b100;">vbtab</span> &amp; TranslateADName<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">3</span>, strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>szScratch1<span style="color: #66cc66;">&#41;</span> &gt; 0 <span style="color: #b1b100;">then</span>
				objOutputFile.<span style="color: #66cc66;">WriteLine</span> szScratch &amp; <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;=&quot;</span> &amp; <span style="color: #b1b100;">vbtab</span> &amp; szScratch1
			<span style="color: #b1b100;">else</span>
				objOutputFile.<span style="color: #66cc66;">WriteLine</span> szScratch &amp; <span style="color: #ff0000;">&quot; = ...&quot;</span>
				LookupADName strLineText<span style="color: #66cc66;">&#40;</span>0<span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;&quot;</span>
			<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
	<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
<span style="color: #b1b100;">Wend</span>
Wscript.<span style="color: #66cc66;">Echo</span> <span style="color: #ff0000;">&quot;Done&quot;</span>
&nbsp;
<span style="color: #b1b100;">Function</span> TranslateADName<span style="color: #66cc66;">&#40;</span>FromType, ToType, FromName<span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">Dim</span> objTranslator
	<span style="color: #b1b100;">Const</span> ADS_NAME_INITTYPE_GC					= <span style="color: #cc66cc;">3</span>
&nbsp;
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_1779					= <span style="color: #cc66cc;">1</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_CANONICAL				= <span style="color: #cc66cc;">2</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_NT4					= <span style="color: #cc66cc;">3</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_DISPLAY					= <span style="color: #cc66cc;">4</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_DOMAIN_SIMPLE			= <span style="color: #cc66cc;">5</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_ENTERPRISE_SIMPLE			= <span style="color: #cc66cc;">6</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_GUID					= <span style="color: #cc66cc;">7</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_UNKNOWN				= <span style="color: #cc66cc;">8</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_USER_PRINCIPAL_NAME		= <span style="color: #cc66cc;">9</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_CANONICAL_EX				= <span style="color: #cc66cc;">10</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_SERVICE_PRINCIPAL_NAME		= <span style="color: #cc66cc;">11</span>
	<span style="color: #b1b100;">Const</span>   ADS_NAME_TYPE_SID_OR_SID_HISTORY_NAME	= <span style="color: #cc66cc;">12</span> 
&nbsp;
	<span style="color: #b1b100;">Set</span> objTranslator = <span style="color: #b1b100;">CreateObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;NameTranslate&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #b1b100;">on</span> <span style="color: #b1b100;">error</span> <span style="color: #b1b100;">resume</span> <span style="color: #b1b100;">next</span>
	objTranslator.<span style="color: #66cc66;">Init</span> ADS_NAME_INITTYPE_GC, <span style="color: #ff0000;">&quot;&quot;</span>
	objTranslator.<span style="color: #b1b100;">Set</span> FromType, FromName
&nbsp;
	TranslateADName = objTranslator.<span style="color: #b1b100;">Get</span><span style="color: #66cc66;">&#40;</span>ToType<span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">on</span> <span style="color: #b1b100;">error</span> <span style="color: #b1b100;">goto</span> 0
<span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span>
&nbsp;
<span style="color: #b1b100;">Function</span> LookupADName<span style="color: #66cc66;">&#40;</span>fname, lname<span style="color: #66cc66;">&#41;</span> <span style="color: #808080;">' Returns number of records</span>
	<span style="color: #b1b100;">dim</span> objConnection, objCommand, objRecordSet, searchscope, strItem
	<span style="color: #b1b100;">On</span> <span style="color: #b1b100;">Error</span> <span style="color: #b1b100;">Resume</span> <span style="color: #b1b100;">Next</span>
	<span style="color: #b1b100;">Const</span> ADS_SCOPE_SUBTREE = <span style="color: #cc66cc;">2</span>
&nbsp;
	fname=<span style="color: #b1b100;">trim</span><span style="color: #66cc66;">&#40;</span>replace<span style="color: #66cc66;">&#40;</span>fname, <span style="color: #ff0000;">&quot;'&quot;</span>, <span style="color: #ff0000;">&quot;''&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	lname=<span style="color: #b1b100;">trim</span><span style="color: #66cc66;">&#40;</span>replace<span style="color: #66cc66;">&#40;</span>lname, <span style="color: #ff0000;">&quot;'&quot;</span>, <span style="color: #ff0000;">&quot;''&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>fname<span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">and</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>lname<span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">then</span> <span style="color: #b1b100;">exit</span> <span style="color: #b1b100;">function</span>
&nbsp;
	<span style="color: #b1b100;">Set</span> objConnection = <span style="color: #b1b100;">CreateObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ADODB.Connection&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #b1b100;">Set</span> objCommand =   <span style="color: #b1b100;">CreateObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ADODB.Command&quot;</span><span style="color: #66cc66;">&#41;</span>
	objConnection.<span style="color: #66cc66;">Provider</span> = <span style="color: #ff0000;">&quot;ADsDSOObject&quot;</span>
	objConnection.<span style="color: #b1b100;">Open</span> <span style="color: #ff0000;">&quot;Active Directory Provider&quot;</span>
	<span style="color: #b1b100;">Set</span> objCommand.<span style="color: #66cc66;">ActiveConnection</span> = objConnection
&nbsp;
	objCommand.<span style="color: #66cc66;">Properties</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Page Size&quot;</span><span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">1000</span>
	objCommand.<span style="color: #66cc66;">Properties</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Searchscope&quot;</span><span style="color: #66cc66;">&#41;</span> = ADS_SCOPE_SUBTREE
	<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>fname<span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">or</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>lname<span style="color: #66cc66;">&#41;</span>=0 <span style="color: #b1b100;">then</span>
		lname = fname &amp; lname
		<span style="color: #b1b100;">if</span> lname = <span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #b1b100;">then</span>
			objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;Skipping '*'&quot;</span>
			<span style="color: #b1b100;">exit</span> <span style="color: #b1b100;">function</span>
		<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
		objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;{searching for '&quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;'}&quot;</span>
		objCommand.<span style="color: #66cc66;">CommandText</span> = <span style="color: #ff0000;">&quot;SELECT sAMAccountName, displayName, department, description FROM 'LDAP://&quot;</span> &amp; LDAPOU &amp; <span style="color: #ff0000;">&quot;' WHERE objectCategory='user' AND &quot;</span> &amp; _
			<span style="color: #ff0000;">&quot;sn='&quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;'&quot;</span>
		searchscope=<span style="color: #ff0000;">&quot;'* &quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;'&quot;</span>
	<span style="color: #b1b100;">else</span>
		objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;{searching for '&quot;</span> &amp; fname &amp; <span style="color: #ff0000;">&quot;' &amp; '&quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;'}&quot;</span>
		objCommand.<span style="color: #66cc66;">CommandText</span> = <span style="color: #ff0000;">&quot;SELECT sAMAccountName, displayName, department, description FROM 'LDAP://&quot;</span> &amp; LDAPOU &amp; <span style="color: #ff0000;">&quot;' WHERE objectCategory='user' AND &quot;</span> &amp; _
			<span style="color: #ff0000;">&quot;( (givenName='&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>lname,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;*' AND sn='&quot;</span> &amp; fname &amp; <span style="color: #ff0000;">&quot;') OR &quot;</span> &amp; _
			<span style="color: #ff0000;">&quot;(givenName='&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>fname,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;*' AND sn='&quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;') )&quot;</span>
		searchscope=<span style="color: #ff0000;">&quot;'&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>lname,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;* &quot;</span> &amp; fname &amp; <span style="color: #ff0000;">&quot;' or '&quot;</span> &amp; <span style="color: #b1b100;">left</span><span style="color: #66cc66;">&#40;</span>fname,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;* &quot;</span> &amp; lname &amp; <span style="color: #ff0000;">&quot;'&quot;</span>
	<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
	<span style="color: #b1b100;">Set</span> objRecordSet = objCommand.<span style="color: #66cc66;">Execute</span>
	objRecordSet.<span style="color: #66cc66;">MoveFirst</span>
	LookupADName = objRecordSet.<span style="color: #66cc66;">RecordCount</span>
	<span style="color: #b1b100;">if</span> objRecordSet.<span style="color: #66cc66;">RecordCount</span>&gt;=TOOMANYCHOICES <span style="color: #b1b100;">then</span>
		objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;Found &quot;</span> &amp; objRecordSet.<span style="color: #66cc66;">RecordCount</span> &amp; <span style="color: #ff0000;">&quot; of &quot;</span> &amp; searchscope &amp; <span style="color: #ff0000;">&quot;. &quot;</span> &amp; TOOMANYCHOICES &amp; <span style="color: #ff0000;">&quot; is too many.&quot;</span>
	<span style="color: #b1b100;">elseif</span> objRecordSet.<span style="color: #66cc66;">RecordCount</span>=0 <span style="color: #b1b100;">then</span>
		objOutputFile.<span style="color: #66cc66;">WriteLine</span> <span style="color: #b1b100;">vbtab</span> &amp; <span style="color: #ff0000;">&quot;did not find &quot;</span> &amp; searchscope
	<span style="color: #b1b100;">else</span>
		<span style="color: #b1b100;">Do</span> Until objRecordSet.<span style="color: #b1b100;">EOF</span>
			objOutputFile.<span style="color: #b1b100;">Write</span> <span style="color: #b1b100;">vbtab</span> &amp; objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;displayName&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span>
			<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">not</span> <span style="color: #b1b100;">isnull</span><span style="color: #66cc66;">&#40;</span>objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;description&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span>
				<span style="color: #b1b100;">For</span> each strItem in objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;description&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span>
					<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span> <span style="color: #66cc66;">&#40;</span>strItem<span style="color: #66cc66;">&#41;</span>&gt;0 <span style="color: #b1b100;">then</span> objOutputFile.<span style="color: #b1b100;">Write</span>  <span style="color: #ff0000;">&quot; (&quot;</span> &amp; strItem &amp; <span style="color: #ff0000;">&quot;)&quot;</span>
				<span style="color: #b1b100;">Next</span>
			<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
			<span style="color: #b1b100;">if</span> <span style="color: #b1b100;">len</span><span style="color: #66cc66;">&#40;</span>objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;department&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span><span style="color: #66cc66;">&#41;</span>&gt;0 <span style="color: #b1b100;">then</span> objOutputFile.<span style="color: #b1b100;">Write</span>  <span style="color: #ff0000;">&quot; (&quot;</span> &amp; objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;department&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span> &amp; <span style="color: #ff0000;">&quot;)&quot;</span>
			objOutputFile.<span style="color: #66cc66;">WriteLine</span>  <span style="color: #ff0000;">&quot; =&quot;</span>&amp; <span style="color: #b1b100;">vbtab</span> &amp; objRecordSet.<span style="color: #66cc66;">Fields</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sAMAccountName&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #66cc66;">Value</span>
			objRecordSet.<span style="color: #66cc66;">MoveNext</span>
		<span style="color: #b1b100;">Loop</span>
	<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span>
	<span style="color: #b1b100;">on</span> <span style="color: #b1b100;">error</span> <span style="color: #b1b100;">goto</span> 0
<span style="color: #b1b100;">End</span> <span style="color: #b1b100;">Function</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://caseelse.net/2008/09/14/matching-arbitrary-name-lists-in-active-directory/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Why not to use Excel for Data Gathering</title>
		<link>http://caseelse.net/2008/08/06/why-not-to-use-excel-for-data-gathering/</link>
		<comments>http://caseelse.net/2008/08/06/why-not-to-use-excel-for-data-gathering/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 05:05:42 +0000</pubDate>
		<dc:creator>neil</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://caseelse.net/?p=36</guid>
		<description><![CDATA[Here&#8217;s a request that happens fairly regularly in different offices I&#8217;ve been associated with:


We receive surveys/orders/documents that have been filled out by various people as Excel spreadsheets
We need to put the information into a database
We want you to write a script to automate the imports.


My thoughts as a scripter:

This is an excellent way to keep [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a request that happens fairly regularly in different offices I&#8217;ve been associated with:</p>
<blockquote>
<ol>
<li>We receive surveys/orders/documents that have been filled out by various people as Excel spreadsheets</li>
<li>We need to put the information into a database</li>
<li>We want you to write a script to automate the imports.</li>
</ol>
</blockquote>
<p>My thoughts as a scripter:<span id="more-36"></span></p>
<ol>
<li>This is an excellent way to keep a contractor in a job for a long time.</li>
<li>You will <strong>never</strong> get everybody to input the data the same way into Excel. Nobody <strong>ever</strong> has.
<ul>
<li>People will put things on different lines, because it is Excel, and they can.</li>
<li>People will never enter data the same way. If you give them a yes/no column in a spreadsheet, some time you will get &#8220;yes/no&#8221;, sometimes &#8220;y/n&#8221;, &#8220;t,f&#8221;, &#8220;X, &#8221;, &#8220;☑,☒&#8221;, &#8220;☺,☹&#8221;, &#8220;+,-&#8221;. Every time somebody creates a new item, the script breaks, and has to be modified to compensate, or you have to send the form back or correct it. With a web form, you just give them a yes/no box, or a check box, and they can&#8217;t put in different values (it&#8217;s self-correcting).</li>
<li>Whenever the user changes the values, if you haven&#8217;t predicted what they will enter, you will have to manually redo those files, because they will break the script. This <strong>always</strong> happens. Then you have to go back and fix the script.</li>
</ul>
</li>
<li>If you want to run the script on a server, you either have to work out how to dissect XLS files, or install Excel on the server. The first is a royal pain, and the second shouldn&#8217;t be allowed to happen.</li>
</ol>
<p>The best way to deal with this is to avoid it altogether:</p>
<ol>
<li>Get your database in a SQL database back end (and don&#8217;t confuse Excel with a database.)</li>
<li>Set up a web form that looks like the spreadsheet, to do data input straight into the database</li>
<li>Set up list boxes, etc, so that the data is picked, and not created on the fly.</li>
<li>Instead of filling out the spreadsheet, users just fill out a web page.</li>
<li>If you really want, you can send out Access-via-Outlook forms to anybody with Outlook. They are like Access forms, except they get emailed, and they send the data straight into a database.</li>
</ol>
<p>Miscellaneous advantages to web form:</p>
<ol>
<li>It takes almost all of the work out</li>
<li>You don&#8217;t have to correct data for people. If there&#8217;s a problem, it can let them know as they enter it, and they can correct it on the spot.</li>
<li>You don&#8217;t have to worry about different versions of Excel and interoperability.</li>
<li>You don&#8217;t even have to worry if people <strong>have</strong> Excel.</li>
<li>People can fill in a web form from any machine&#8211;PC, Macintosh, Blackberry.</li>
<li>If you change things, the web form changes along with the back end, and anybody going to the form automatically gets the new version&#8211;with Excel, you get back what you sent out any time ago.</li>
</ol>
<p>Doing this as a script will cost you a lot of time and effort (which probably translates into money), and will never work right (which will cost you a lot more money). Doing this as a web form will be relatively easy (and cheap), and the data will be self-correcting. If you want to do Access-via-Outlook forms, you will have plenty of money left over, in case you need to hire somebody to create that (it shouldn&#8217;t be hard, nor take long to make.) Besides, starting with a website, and just having an input form added to it is barely more work than the website by itself&#8211;and whoever builds the website will probably build the form as they build the site, anyway, just to do test data (I always do, when I build databases.)</p>
<p>If you still insist on doing this as a script, do it in Perl. Perl has modules to parse XLS files (so it doesn&#8217;t need Excel) and is built to parse native language text&#8211;which it will have to do for anything people can type in wrong&#8211;and can compile into an EXE, if you need to run it on a server without installing a new language. If you don&#8217;t need to run it on a server, Perl is still the best language to do it in. It&#8217;d still be an expensive and troublesome pain, but not as much as other scripting languages.</p>
<p>This kind of project is a knock-off for a web-developer. It&#8217;s a career for a scripter.</p>
]]></content:encoded>
			<wfw:commentRss>http://caseelse.net/2008/08/06/why-not-to-use-excel-for-data-gathering/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Multithreading in Batch Script, Part 2: Running Code</title>
		<link>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-2-running-code/</link>
		<comments>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-2-running-code/#comments</comments>
		<pubDate>Thu, 22 May 2008 20:25:54 +0000</pubDate>
		<dc:creator>neil</dc:creator>
		
		<category><![CDATA[Batch Scripts]]></category>

		<guid isPermaLink="false">http://caseelse.net/?p=21</guid>
		<description><![CDATA[Wherein we take the multithread concept, and run like blazes.

This article uses some of the files from Multithreading in Batch Script, Part 1: An Example; so before you get deep into it, grab the master.bat and computers.csv from there.
This thread script:

takes a given PC name, pings it and reverse-resolves it.
logs it as responsive or unresponsive [...]]]></description>
			<content:encoded><![CDATA[<p>Wherein we take the multithread concept, and run like blazes.<br />
<span id="more-21"></span><br />
This article uses some of the files from <a href="http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-1-an-example/">Multithreading in Batch Script, Part 1: An Example</a>; so before you get deep into it, grab the master.bat and computers.csv from there.</p>
<p>This thread script:</p>
<ol>
<li>takes a given PC name, pings it and reverse-resolves it.</li>
<li>logs it as responsive or unresponsive (if unresponsive, it quits)</li>
<li>maps a drive to the PC, with the same letter as the thread</li>
<li>runs arbitrary code against the pc</li>
<li>unwinds</li>
<li>deletes itself</li>
</ol>
<p>The drive mapping can take a list of username/password combinations, starting with the user who runs it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p21code6'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p216"><td class="code" id="p21code6"><pre class="dos dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">ECHO</span> OFF
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">ip</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">pcname</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">drive</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">num</span>=00000<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">logfile</span>=logs\<span style="color: #33cc33;">%</span><span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>.txt<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">outputname</span>=thread<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">outputfile</span>=.\log.csv<span style="color: #66cc66;">&#41;</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::::::::::::::::::::::::::::::::</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100; font-weight: bold;">Set</span> variables above. This file called as;</span>
:<span style="color: #b100b1; font-weight: bold;">: newletter.bat    count newletter LineFromCSV</span>
:<span style="color: #b100b1; font-weight: bold;">: parameters are     %<span style="color: #448888;">1</span>      <span style="color: #33cc33;">%</span><span style="color: #448888;">2</span>      <span style="color: #33cc33;">%</span><span style="color: #448888;">3</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">4</span>...</span>
:<span style="color: #b100b1; font-weight: bold;">:</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> Write a batch file that scans what you</span>
:<span style="color: #b100b1; font-weight: bold;">: need locally, and uses the variables above.</span>
:<span style="color: #b100b1; font-weight: bold;">:</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> Update the &quot;:::Variable Part:::&quot; section</span>
:<span style="color: #b100b1; font-weight: bold;">: with your batch file.</span>
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::::::::::::::::::::::::::::::::</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">ECHO</span>. <span style="color: #33cc33;">%</span><span style="color: #448888;">*</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">num</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">num:~-5</span><span style="color: #33cc33;">%</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">pingnum</span>=<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">pingtarget</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>
:<span style="color: #b100b1; font-weight: bold;">PINGSTAT</span>
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">for</span> /F &quot;delims=&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>'ping -n <span style="color: #33cc33;">%</span><span style="color: #448888;">pingnum</span><span style="color: #33cc33;">%</span> -a <span style="color: #33cc33;">%</span><span style="color: #448888;">pingtarget</span><span style="color: #33cc33;">%</span>'<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
 <span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #33cc33;">%</span>A&quot;</span>
 <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">for</span> /F &quot;tokens=<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">11</span>&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>B <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>&quot;<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A&quot;<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>B&quot;==&quot;Pinging&quot; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">resolvedip</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>C<span style="color: #66cc66;">&#41;</span>
  <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>B&quot;==&quot;Packets:&quot; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">percentloss</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">DEFINED</span> percentloss <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">percentloss</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">percentloss:~<span style="color: #cc66cc;">1</span>,-1</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">DEFINED</span> resolvedip <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">ip</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">resolvedip:~<span style="color: #cc66cc;">1</span>,-1</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #00b100; font-weight: bold;">IF</span> <span style="color: #000000; font-weight: bold;">NOT</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">percentloss</span><span style="color: #33cc33;">%</span>&quot;==&quot;<span style="color: #cc66cc;">100</span>&quot; <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:RESPONSIVE</span>
:<span style="color: #b100b1; font-weight: bold;">: quarter ping failed. Check <span style="color: #00b100; font-weight: bold;">for</span> partial connectivity.</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #00b100; font-weight: bold;">if</span> full ping failed, <span style="color: #00b100; font-weight: bold;">exit</span>.</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span> Failed ping <span style="color: #66cc66;">&#91;</span>*<span style="color: #33cc33;">%</span><span style="color: #448888;">pingnum</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>&quot;</span>
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">pingnum</span><span style="color: #33cc33;">%</span>&quot;==&quot;<span style="color: #cc66cc;">1</span>&quot; <span style="color: #66cc66;">&#40;</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">pingnum</span>=<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;retrying with <span style="color: #cc66cc;">4</span> pings . . .&quot;</span>
 <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:PINGSTAT</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxULC &quot;%<span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">ip</span><span style="color: #33cc33;">%</span>,Unavailable. Lost <span style="color: #33cc33;">%</span><span style="color: #448888;">percentloss</span><span style="color: #33cc33;">%</span> percent of <span style="color: #33cc33;">%</span><span style="color: #448888;">pingnum</span><span style="color: #33cc33;">%</span> pings&quot;</span>
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:END</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">Responsive</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #448888;">percentloss</span><span style="color: #33cc33;">%</span> percent Loss&quot;</span>
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;&quot;==&quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">percentloss</span><span style="color: #33cc33;">%</span>&quot; <span style="color: #66cc66;">&#40;</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxULC &quot;%<span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">ip</span><span style="color: #33cc33;">%</span>,<span style="color: #000000; font-weight: bold;">Not</span> Found&quot;</span>
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:END</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">: At this point, the machine is online and available</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">:::::::::::: connect drive :::::::::::::</span>
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #000000; font-weight: bold;">exist</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>:\<span style="color: #0000ff; font-weight: bold;">nul</span> <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">ADDDRIVE</span>
:<span style="color: #b100b1; font-weight: bold;">DRIVEEXISTS</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span> exists . . .&quot;</span>
<span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">for</span> /F &quot;delims=&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>'net use <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>: /delete /y'<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span><span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #33cc33;">%</span>A&quot;<span style="color: #66cc66;">&#41;</span></span>
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">exist</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>:\<span style="color: #0000ff; font-weight: bold;">nul</span> <span style="color: #66cc66;">&#40;</span>
 ping -n <span style="color: #cc66cc;">3</span> localhost &amp;gt;null
 <span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">:DRIVEEXISTS</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;%<span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>:\<span style="color: #0000ff; font-weight: bold;">nul</span> doesn't exist&quot;</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">ADDDRIVE</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;attaching \\%<span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>\c$ to <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>:&quot;</span>
&nbsp;
<span style="color: #00b100; font-weight: bold;">FOR</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>&quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">USERID</span><span style="color: #33cc33;">%</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">PASSWORD</span><span style="color: #33cc33;">%</span>&quot;,&quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>\Administrator Password&quot;<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #00b100; font-weight: bold;">For</span> /F &quot;tokens=<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>B <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span><span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">connect %<span style="color: #33cc33;">%</span>B <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>C</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">ERR</span><span style="color: #33cc33;">%</span>&quot;==&quot;0&quot; <span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">:MAIN</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLC &quot;%<span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">IP</span><span style="color: #33cc33;">%</span>,Can't Connect&quot;</span>
<span style="color: #00b100; font-weight: bold;">Goto</span> <span style="color: #b100b1; font-weight: bold;">:End</span>
:<span style="color: #b100b1; font-weight: bold;">:::::::::::: /connect drive :::::::::::::</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">MAIN</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;Entering Main . . . &quot;</span>
<span style="color: #b1b100; font-weight: bold;">del</span> netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt -y
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::: Variable part ::::::::::::::::::</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">: Log success</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log SxxLC &quot;%<span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">ip</span><span style="color: #33cc33;">%</span></span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::: /Variable part ::::::::::::::::::</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;Disconnecting . . . &quot;</span>
net use <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>: /delete /y
&nbsp;
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:END</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">log</span>
:<span style="color: #b100b1; font-weight: bold;">: SFUL <span style="color: #66cc66;">&#40;</span>Success Failure Unavailable LogDetails CommonOutput<span style="color: #66cc66;">&#41;</span> &quot;Message&quot;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">log</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">echo</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">~<span style="color: #cc66cc;">2</span>
<span style="color: #00b100; font-weight: bold;">if</span> &quot;</span><span style="color: #33cc33;">%</span>log:~0,<span style="color: #cc66cc;">1</span><span style="color: #33cc33;">%</span><span style="color: #448888;">&quot;==&quot;S&quot; <span style="color: #b1b100; font-weight: bold;">ECHO</span>.</span><span style="color: #33cc33;">%</span>~<span style="color: #cc66cc;">2</span>&amp;gt;&amp;gt;<span style="color: #33cc33;">%</span><span style="color: #448888;">outputname</span><span style="color: #33cc33;">%</span>.Updated.log
<span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">log:~<span style="color: #cc66cc;">1</span>,1</span><span style="color: #33cc33;">%</span>&quot;==&quot;F&quot; <span style="color: #b1b100; font-weight: bold;">ECHO</span>.<span style="color: #33cc33;">%</span><span style="color: #448888;">~<span style="color: #cc66cc;">2</span>&amp;gt;&amp;gt;</span><span style="color: #33cc33;">%</span>outputname<span style="color: #33cc33;">%</span><span style="color: #448888;">.Noupdate.log
<span style="color: #00b100; font-weight: bold;">if</span> &quot;</span><span style="color: #33cc33;">%</span>log:~<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">1</span><span style="color: #33cc33;">%</span><span style="color: #448888;">&quot;==&quot;U&quot; <span style="color: #b1b100; font-weight: bold;">ECHO</span>.</span><span style="color: #33cc33;">%</span>~<span style="color: #cc66cc;">2</span>&amp;gt;&amp;gt;<span style="color: #33cc33;">%</span><span style="color: #448888;">outputname</span><span style="color: #33cc33;">%</span>.Unavail.log
<span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">log:~<span style="color: #cc66cc;">3</span>,1</span><span style="color: #33cc33;">%</span>&quot;==&quot;L&quot; <span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">defined</span> logfile <span style="color: #b1b100; font-weight: bold;">ECHO</span>.<span style="color: #33cc33;">%</span><span style="color: #448888;">~<span style="color: #cc66cc;">2</span>&amp;gt;&amp;gt;</span><span style="color: #33cc33;">%</span>logfile<span style="color: #33cc33;">%</span><span style="color: #448888;">
<span style="color: #00b100; font-weight: bold;">if</span> &quot;</span><span style="color: #33cc33;">%</span>log:~<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">1</span><span style="color: #33cc33;">%</span><span style="color: #448888;">&quot;==&quot;C&quot; <span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">defined</span> outputfile <span style="color: #b1b100; font-weight: bold;">ECHO</span>.</span><span style="color: #33cc33;">%</span>~<span style="color: #cc66cc;">2</span>&amp;gt;&amp;gt;<span style="color: #33cc33;">%</span><span style="color: #448888;">outputfile</span><span style="color: #33cc33;">%</span>
<span style="color: #00b100; font-weight: bold;">Goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">connect</span>
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">EXIST</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>:\<span style="color: #0000ff; font-weight: bold;">nul</span> <span style="color: #00b100; font-weight: bold;">Goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
<span style="color: #00b100; font-weight: bold;">call</span> <span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">USER</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span>
<span style="color: #00b100; font-weight: bold;">call</span> <span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">PWD</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;connecting as %<span style="color: #448888;">USER</span><span style="color: #33cc33;">%</span>&quot;</span>
<span style="color: #00b100; font-weight: bold;">if</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">USER</span><span style="color: #33cc33;">%</span>&quot;==&quot;&quot; <span style="color: #66cc66;">&#40;</span>
 net use <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>: \\<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>\c$ &amp;gt; netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt <span style="color: #cc66cc;">2</span>&amp;gt;&amp;amp;<span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span>
 net use <span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>: \\<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>\c$ /USER:<span style="color: #33cc33;">%</span><span style="color: #448888;">USER</span><span style="color: #33cc33;">%</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">PWD</span><span style="color: #33cc33;">%</span> &amp;gt; netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt <span style="color: #cc66cc;">2</span>&amp;gt;&amp;amp;<span style="color: #cc66cc;">1</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">ERR</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">ERRORLEVEL</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;netuse returns '%<span style="color: #448888;">ERR</span><span style="color: #33cc33;">%</span>'&quot;</span>
type netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt
<span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">defined</span> logfile type netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt&amp;gt;&amp;gt;<span style="color: #33cc33;">%</span><span style="color: #448888;">logfile</span><span style="color: #33cc33;">%</span>
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">ERR</span><span style="color: #33cc33;">%</span>&quot;==&quot;0&quot; <span style="color: #66cc66;">&#40;</span>
 <span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;Connected as %<span style="color: #448888;">USER</span><span style="color: #33cc33;">%</span>.&quot;</span>
 <span style="color: #b1b100; font-weight: bold;">del</span> netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt -y
 <span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">FOR</span> /F &quot;tokens=<span style="color: #cc66cc;">3</span> skip=<span style="color: #cc66cc;">1</span>&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>p <span style="color: #00b100; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span>'Find /I &quot;System Error&quot; netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt'<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">DO</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">NETUSEERR</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>p<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;NETUSEERR = %<span style="color: #448888;">NETUSEERR</span><span style="color: #33cc33;">%</span>&quot;</span>
<span style="color: #00b100; font-weight: bold;">FOR</span> /F &quot;delims==&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>p <span style="color: #00b100; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span>'net helpmsg <span style="color: #33cc33;">%</span><span style="color: #448888;">NETUSEERR</span><span style="color: #33cc33;">%</span>'<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">DO</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">NETUSEDESC</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>p
<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">NETUSEERR</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">NETUSEERR</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">NETUSEDESC:,=</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">del</span> netuse<span style="color: #33cc33;">%</span><span style="color: #448888;">drive</span><span style="color: #33cc33;">%</span>.txt -y
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxULx &quot;%<span style="color: #448888;">num</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">pcname</span><span style="color: #33cc33;">%</span>,<span style="color: #33cc33;">%</span><span style="color: #448888;">ip</span><span style="color: #33cc33;">%</span>,Doesn't Connect as <span style="color: #33cc33;">%</span><span style="color: #448888;">USER</span><span style="color: #33cc33;">%</span>. '<span style="color: #33cc33;">%</span><span style="color: #448888;">NETUSEERR</span><span style="color: #33cc33;">%</span>'&quot;</span>
<span style="color: #00b100; font-weight: bold;">Goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">END</span>
<span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">log xxxLx &quot;Done.&quot;</span>
<span style="color: #b1b100; font-weight: bold;">del</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">0</span></pre></td></tr></table></div>

<p><code><a href='http://caseelse.net/wp-content/uploads/2008/05/thread2.bat'>download thread2.bat</a></code></p>
<p>This code does nothing to the target machines; it assumes that if it&#8217;s made it as far as a mapped drive, it&#8217;s finished. You can actually run just about anything against the machines: write a batch file to do *stuff* and use the variables listed in the block at the beginning of this script. When it works, plug it into this script, in the section marked
<pre>:::::::::::::::::: Variable part ::::::::::::::::::</pre>
<p> if you need to execute a process on the target machines, you can do so with psexec or beyondexec. You can with rclient, also, but it&#8217;s turning into a security liability, at this age.</p>
]]></content:encoded>
			<wfw:commentRss>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-2-running-code/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Multithreading in Batch Script, Part 1: An Example</title>
		<link>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-1-an-example/</link>
		<comments>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-1-an-example/#comments</comments>
		<pubDate>Thu, 22 May 2008 18:33:18 +0000</pubDate>
		<dc:creator>neil</dc:creator>
		
		<category><![CDATA[Batch Scripts]]></category>

		<guid isPermaLink="false">http://caseelse.net/?p=18</guid>
		<description><![CDATA[Multithreaded applications have the potential to run much faster than single-threaded applications, given the right circumstances. With batch scripts, we often write routines that spend a lot of their time waiting for other things to finish; downloading sets of files, for example, or pinging a range of servers to see if they are alive. Blocking [...]]]></description>
			<content:encoded><![CDATA[<p>Multithreaded applications have the potential to run much faster than single-threaded applications, given the right circumstances. With batch scripts, we often write routines that spend a lot of their time waiting for other things to finish; downloading sets of files, for example, or pinging a range of servers to see if they are alive.<img class="size-full wp-image-19" style="float: left;padding-right:10;margin-left:10;margin-right:10;margin-top:0;display:inline;" title="Various Agent Smiths" src="http://caseelse.net/wp-content/uploads/2008/05/agentsmith.png" alt="Various Agent Smiths" width="160" height="100" /> Blocking functions (ie, where the scripts stalls until they are done) that spend their time waiting for other things are the big beneficiaries of multithreading. With some care, we can get the same benefits by &#8216;multithreading&#8217; our batch scripts.</p>
<p><span id="more-18"></span></p>
<p>What makes multithreading worthwhile is lag and the fact that &#8220;all computers wait at the same speed&#8221;. Without some sort of wait, threading can actually slow a system down due to overhead. Think of it like this: the pony express moved mail at the maximum speed a horse could travel. If they had implemented it with two or more horses, running parallel, the mail wouldn&#8217;t have gotten there any faster. two horses (ignoring load, so carriages don&#8217;t enter into this) don&#8217;t run faster than one horse. On the other hand, if running parallel horses doesn&#8217;t increase your speed, but you add the overhead of having to split the mailbags (ie, take the time to count or weigh the mail, so that the horses carry the same amount) then you are running at a net loss.</p>
<p>The pony express, however, was a system that was running at near maximum efficiency from our point of view. Adding more horses into the mix could increase complexity and overhead, but not speed up the system. It&#8217;s a different story for pizza delivery boys, or jugglers. Jugglers spend most of their time waiting for the ball to drop&#8211;literally. They throw a ball into the air, and it flies up, stops, falls back down. It isn&#8217;t until the ball comes back down that the juggler has any more input into the process. A juggler can therefore achieve higher efficiency by launching multiple <em>asynchonous</em> balls. They&#8217;re not parallel, per se, but they are fired off one after another, and function on their own. In this case, the balls can slightly change the &#8216;running&#8217; of each other. To increase the numbers of balls in the air, it becomes necessary to throw them higher, requiring that their &#8216;runtime&#8217; be slightly longer. In general, though, you can keep adding balls until you are running your juggler at maximum speed before you have to lengthen the &#8216;run&#8217; of the balls. Once your juggler is maxed out, it will affect the speed of the system, but you&#8217;ve still drastically increased the amount of <em>stuff</em> that&#8217;s going on by utilizing the juggler&#8217;s idle time.</p>
<p>Say a busy pizza company guarantees delivery within x minutes. In this case, using one delivery boy could get very expensive&#8211;any delivery x/2 minutes away takes up the delivery boy&#8217;s whole transit time (x/2 minutes each way). Any order placed while the delivery boy is out will be delayed while he is out, and could easily be delayed beyond the time limit. The company could try to maximize their efficiency by organizing deliveries together on a trip, and hiring the fastest possible driver to minimize the transit times&#8211;but the customer is a wildcard. Nobody knows how long it&#8217;s going to take a customer to check their order, look for their checkbook, find out checks aren&#8217;t accepted, look for cash, and work out a tip&#8211;and all pizza boys wait at the same speed. What they need to do is hire several delivery boys, have each one take one existing order and leave (in parallel) and as soon as they are back, take the next order and run (asynchronously). In the pizza business, they probably want to grab the pizza that needs to be delivered furthest, but they could always settle on the first-come, first-served model.</p>
<p>Multithreading requires a process to run, and a control system to keep it from running amok. Below we have the control system for a simple multithread emulator. It takes a pool of necessary tasks and keeps a list of available threads that it can use to process those tasks. Our model is most like the pizza company. We have arbitrary length processes, and are going to fire off as many as we can at once, and as each finishes, it grabs the next and runs. We don&#8217;t prioritize the machines, so they&#8217;ll just be first-come, first-serve.</p>
<p>Our &#8216;thread&#8217; process will need do several things, in order to function as an efficient system.</p>
<ol>
<li>it will quarter-ping the target, to make sure it is online</li>
<li>if the quarter ping succeeds, excellent, but on failure the thread will perform a full ping, to account for network connectivity issues</li>
<li>if the machine is found online, the thread will map a drive to that machine&#8217;s default admin shares</li>
<li>the tread will perform an arbitrary process against the target</li>
</ol>
<p>The first three actions all require waiting for the network or the target machine to respond. While waiting, the process will use around 0% of the CPU. This makes it a candidate for threading. If processing a single target machine took 100% of the CPU, then threading would take more resources than it returned. In our case, we can take advantage of unused cycles.</p>
<ul>
<li>We&#8217;re going to emulate the task pool with a list of machines that need the same task run on each of them.</li>
<li>We&#8217;ll also emulate the available threads with a list of available network drives. In a full application, we&#8217;ll be mapping a drive to each of the PCs, so using the available drives gives us both a control (we only run as many threads as we have drives, so we can change the number of threads we wish to run) and a mechanism to identify what is connected where. This has the side-effect of making this file specific to a computer. To make it universal, we would want to have it generate it&#8217;s own list based on what drives are not mapped in the system.</li>
</ul>
<p><!-- file="http://caseelse.net/wp-content/uploads/2008/05/master.bat" --></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p18code10'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1810"><td class="code" id="p18code10"><pre class="dos dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">ECHO</span> OFF
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">drivestring</span>=J K L<span style="color: #66cc66;">&#41;</span>&amp;amp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">childtemplate</span>=thread.bat<span style="color: #66cc66;">&#41;</span>&amp;amp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">inputfile</span>=computers.csv<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">outputfile</span>=.\log.csv
<span style="color: #b1b100; font-weight: bold;">Echo</span>.number,machine,ip&amp;gt; <span style="color: #33cc33;">%</span><span style="color: #448888;">outputfile</span><span style="color: #33cc33;">%</span>
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #b1b100; font-weight: bold;">Set</span> variables above.</span>
:<span style="color: #b100b1; font-weight: bold;">: Make sure drivestrings reflect executing machine's available drives.</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #00b100; font-weight: bold;">If</span> common outputfile is used, make sure it matches <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">childtemplate</span><span style="color: #33cc33;">%</span></span>
:<span style="color: #b100b1; font-weight: bold;">::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">set</span> /P <span style="color: #448844;">USERID</span>=Enter your domain\userid:
<span style="color: #b1b100; font-weight: bold;">set</span> /P <span style="color: #448844;">PASSWORD</span>=Enter your password:
cls
&nbsp;
<span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">ECHO</span> OFF
<span style="color: #b1b100; font-weight: bold;">MD</span> logs
<span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">strlen</span>=0
<span style="color: #00b100; font-weight: bold;">for</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">drivestring</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #b1b100; font-weight: bold;">SET</span> /A <span style="color: #448844;">strlen+</span>=<span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">count</span>=0
&nbsp;
<span style="color: #00b100; font-weight: bold;">FOR</span> /F &quot;delims=&quot; <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #00b100; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">inputfile</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">DO</span> <span style="color: #66cc66;">&#40;</span>
 <span style="color: #00b100; font-weight: bold;">call</span> <span style="color: #b1b100; font-weight: bold;">SET</span> /A <span style="color: #448844;">count+</span>=<span style="color: #cc66cc;">1</span>
 <span style="color: #b1b100; font-weight: bold;">echo</span>.
 <span style="color: #00b100; font-weight: bold;">call</span> <span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">comline</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A
 <span style="color: #00b100; font-weight: bold;">call</span> :<span style="color: #b100b1; font-weight: bold;">processmachine %<span style="color: #33cc33;">%</span>A</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">pause</span>
<span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">processmachine</span>
  <span style="color: #b1b100; font-weight: bold;">echo</span> Processing #<span style="color: #33cc33;">%</span><span style="color: #448888;">count</span><span style="color: #33cc33;">%</span>: <span style="color: #33cc33;">%</span><span style="color: #448888;">comline</span><span style="color: #33cc33;">%</span>
  <span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">newletter</span>=
  <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">for</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">drivestring</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
   <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #000000; font-weight: bold;">exist</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D.bat <span style="color: #66cc66;">&#40;</span>
    <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #000000; font-weight: bold;">exist</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D:\<span style="color: #0000ff; font-weight: bold;">nul</span> <span style="color: #66cc66;">&#40;</span><span style="color: #00b100; font-weight: bold;">call</span> <span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">newletter</span>=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D<span style="color: #66cc66;">&#41;</span>
    <span style="color: #33cc33;">@</span><span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">exist</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D:\<span style="color: #0000ff; font-weight: bold;">nul</span> <span style="color: #b1b100; font-weight: bold;">Echo</span> <span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>D: already connected. Skipping.
   <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
  <span style="color: #00b100; font-weight: bold;">if</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #000000; font-weight: bold;">DEFINED</span> newletter <span style="color: #66cc66;">&#40;</span>
   <span style="color: #0000ff; font-weight: bold;">nul</span>
   <span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">processmachine</span>
  <span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span>
   <span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> new letter = '<span style="color: #33cc33;">%</span><span style="color: #448888;">newletter</span><span style="color: #33cc33;">%</span>'
   type <span style="color: #33cc33;">%</span><span style="color: #448888;">childtemplate</span><span style="color: #33cc33;">%</span> &amp;gt; <span style="color: #33cc33;">%</span><span style="color: #448888;">newletter</span><span style="color: #33cc33;">%</span>.bat
   START &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">count</span><span style="color: #33cc33;">%</span> -- <span style="color: #33cc33;">%</span><span style="color: #448888;">newletter</span><span style="color: #33cc33;">%</span>.bat&quot; <span style="color: #33cc33;">%</span><span style="color: #448888;">comspec</span><span style="color: #33cc33;">%</span> /c newletter<span style="color: #33cc33;">%</span><span style="color: #448888;">.bat </span><span style="color: #33cc33;">%</span>count<span style="color: #33cc33;">%</span> <span style="color: #33cc33;">%</span>newletter<span style="color: #33cc33;">%</span> <span style="color: #33cc33;">%</span>comline<span style="color: #33cc33;">%</span>
 <span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p><code><a href="http://caseelse.net/wp-content/uploads/2008/05/master.bat">download master.bat</a></code><br />
The master takes the list of available thread slots, and for each one, it makes a copy of the thread template named x.bat (where x=slot name), and launches that bat file. When it runs out of slots, it sleeps briefly, and looks through the list, to see if any of the bat files are missing&#8211;thereby using the bat file itself as a crude lock. As long as the file is running, and the file is there, that process &#8216;owns&#8217; that slot (and the corresponding drive mapping). The last thing a thread needs to do before it exits, is to delete itself.</p>
<p>This is weak as threaded systems go; it relies on the individual threads to clean up after themselves. If a thread batch dies, it leaves behind an orphaned lock file, and that thread doesn&#8217;t get reused&#8211;resulting in a resource leak. A better alternative would be to check the running process list for each thread-bat rather than relying on the existence of the lock.</p>
<p>All of this should fall well short of the mark for qualifying as a true &#8216;multithreaded&#8217; system; for the purpose of this discussion, I&#8217;m using the term &#8216;multithreading&#8217; loosely, vaguely, and expansively. I believe each command prompt executes in its&#8217; own processor thread (although that would be worth looking into); but that shouldn&#8217;t be enough to classify it as true multithreading. &#8220;Asynchronously executing multiple simultaneous batch processes&#8221; is probably a better description. This is, however, sufficient for quickly assembling network compliance scanners, application fixes, and tools of that nature.</p>
<p>The master runs against a list of PCs; for each machine, launching a thread, and passing it some pertinent information. To that end, we need a list. The master.bat is set up to read from a CSV file named computers.csv. For our purposes, all it needs to be is a list of PC Names, one per row:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p18code11'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1811"><td class="code" id="p18code11"><pre class="dos dos" style="font-family:monospace;">PCName1
PCName2
PCName3
PCName4
PCName5</pre></td></tr></table></div>

<p><code><a href="http://caseelse.net/wp-content/uploads/2008/05/computers.csv">download computers.csv</a></code><br />
Below, you will find a simple thread template. This one is really just an illustration, so it&#8217;s short; in part two, we will look at a significantly more intricate version.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p18code12'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1812"><td class="code" id="p18code12"><pre class="dos dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">echo</span> off
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">ip</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">pcname</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">drive</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span> <span style="color: #448844;">num</span>=00000<span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
Title Thread <span style="color: #33cc33;">%</span><span style="color: #448888;">~n0 handling </span><span style="color: #33cc33;">%</span>PCNAME<span style="color: #33cc33;">%</span><span style="color: #448888;">
&nbsp;
&amp;lt;nul <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span>/p z=Running<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">echo</span>.
&nbsp;
    <span style="color: #00b100; font-weight: bold;">for</span> /l </span><span style="color: #33cc33;">%%</span><span style="color: #448888;">A <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span></span><span style="color: #33cc33;">%</span>1,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">do</span> <span style="color: #66cc66;">&#40;</span>
        &amp;lt;nul <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">set</span>/p z=<span style="color: #33cc33;">%</span><span style="color: #33cc33;">%</span>A <span style="color: #66cc66;">&#41;</span>
        &amp;gt;nul ping 127.0.0.1 -n <span style="color: #cc66cc;">2</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">echo</span>.
&nbsp;
<span style="color: #b1b100; font-weight: bold;">Del</span> <span style="color: #33cc33;">%</span><span style="color: #448888;">0</span></pre></td></tr></table></div>

<p><code><a href="http://caseelse.net/wp-content/uploads/2008/05/thread.bat">download thread.bat</a></code><br />
This bat simply puts up a progress bar, then exits. Each bat runs for one second less than the previous one, beginning with 10 seconds. You can run the bat, if you like&#8211;it makes no changes on the machine&#8211;but realize that the last thing it does is delete itself. (Nothing will get you in the habit of backing up your work faster than working on self-deleting scripts.)</p>
<p>If you copy these three files down to a common directory and run them, it should all work. They&#8217;re set up to run five machines on three available threads, so you can get a good idea of how it works. When run, the master.bat will prompt for a username/password combination. This isn&#8217;t actually used in this iteration (it&#8217;s in there for <a href="http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-2-running-code/">Multithreading in Batch Script, Part 2: Running Code</a>). You can just enter through the prompts (<a href="http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-2-running-code/">Multithreading in Batch Script, Part 2: Running Code</a> is written so that if you enter through the prompts, it will use your current credentials.)</p>
]]></content:encoded>
			<wfw:commentRss>http://caseelse.net/2008/05/22/multithreading-in-batch-script-part-1-an-example/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Recursion in Batch, Part 2: a tree command</title>
		<link>http://caseelse.net/2008/05/21/recursion-in-batch-part-2-a-tree-command/</link>
		<comments>http://caseelse.net/2008/05/21/recursion-in-batch-part-2-a-tree-command/#comments</comments>
		<pubDate>Wed, 21 May 2008 05:58:02 +0000</pubDate>
		<dc:creator>neil</dc:creator>
		
		<category><![CDATA[Batch Scripts]]></category>

		<category><![CDATA[dev]]></category>

		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://caseelse.net/?p=15</guid>
		<description><![CDATA[FileTree is a rewrite of the Tree.com command. FileTree started off as a simple example of recursion, but turned out to be 2K long&#8211;so play with it if you wish. It turned out much better than I had anticipated; and it uses some Batch geek tricks to make things work. Treeing is the quintessential recursive [...]]]></description>
			<content:encoded><![CDATA[<p>FileTree is a rewrite of the Tree.com command.<a href="http://caseelse.net/wp-content/uploads/2008/05/recursionagain.jpg"><img class="size-medium wp-image-16" title="Motivational Poster for Recursion" src="http://caseelse.net/wp-content/uploads/2008/05/RecursionAgain250x200.png"  style='float: right; margin-left:10;margin-top:0; display:inline;' alt="Motivational Poster for Recursion" /></a> FileTree started off as a simple example of recursion, but turned out to be 2K long&#8211;so play with it if you wish. It turned out much better than I had anticipated; and it uses some Batch geek tricks to make things work. Treeing is the quintessential recursive *thing*. Without recursion, this sort of command would likely be impossible in Batch, and pretty damn hard in other languages.<br />
<span id="more-15"></span></p>
<p>Recursion is a simple concept once it gets into your head, and there are any number of treatises about it on the web&#8211;including <a href="http://caseelse.net/2008/05/21/recursion-in-batch-part-1-an-example/">Recursion in Batch, Part 1: An Example</a>&#8211;so go read that if you aren&#8217;t comfortable with it.</p>
<div class="boundingbox">
<div class="CMDPrompt">
<div class="CMDPromptTitle">C:\WINDOWS\System32\cmd.exe - filetree \windows\system32\wbem</div>
<div class="CMDPromptBox">
C:\test\Desktop\recurse>filetree \windows\system32\wbem<br />
<br />&nbsp;<br />
C:\WINDOWS\system32\wbem<br />
╞ AutoRecover<br />
╞ Logs<br />
╞ mof<br />
│  ╞ bad<br />
│  ╘ good<br />
╞ Performance<br />
╞ Repository<br />
│  ╘ FS<br />
╘ xml<br />
Press any key to continue . . .<br />
<br />&nbsp;
</div>
</div>
</div>
<p>To create a tree routine without recursion, you&#8217;d have to create large dynamic arrays, or something worse&#8211;I&#8217;ve had to do that in Wise (which has no array functionality) by appending folderspecs to a string, and keeping an eye on the length of the string, so I can shove overflow into another . . . it ain&#8217;t worth it.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p15code14'); return false;">View Code</a> DOS</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p1514"><td class="code" id="p15code14"><pre class="dos dos" style="font-family:monospace;"><span style="color: #33cc33;">@</span><span style="color: #b1b100; font-weight: bold;">ECHO</span> OFF
<span style="color: #00b100; font-weight: bold;">IF</span> <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>/?<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>
 <span style="color: #b1b100; font-weight: bold;">Echo</span> Graphically displays the folder structure of a drive or path.
 <span style="color: #b1b100; font-weight: bold;">Echo</span> Usage: <span style="color: #33cc33;">%</span><span style="color: #448888;">~nx0 <span style="color: #66cc66;">&#91;</span>/dasf<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drive:<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>path<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span> <span style="color: #66cc66;">&#91;</span>/dasf<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drive:<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span>path<span style="color: #66cc66;">&#93;</span> ...<span style="color: #66cc66;">&#93;</span>
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /d:  show File Dates
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /a:  Show File Attributes
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /s:  Show FileSizes
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /f:  Show Files <span style="color: #00b100; font-weight: bold;">in</span> Directories
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /t:  Use Tree.com format
 <span style="color: #b1b100; font-weight: bold;">Echo</span>   /c:  Use compact format - negates /t
 <span style="color: #b1b100; font-weight: bold;">Echo</span>.
 <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">Hasfiles</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">Root</span>=<span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">Params</span>=/
<span style="color: #00b100; font-weight: bold;">Goto</span> <span style="color: #b100b1; font-weight: bold;">:GotSwitch</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">ParseParams</span>
<span style="color: #00b100; font-weight: bold;">SHIFT</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PARAMS</span>=</span><span style="color: #33cc33;">%</span>~0
<span style="color: #00b100; font-weight: bold;">If</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS</span><span style="color: #33cc33;">%</span>&quot;==&quot;&quot; <span style="color: #66cc66;">&#40;</span><span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">ROOT</span><span style="color: #33cc33;">%</span>&quot;==&quot;&quot; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PARAMS</span>=.<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #00b100; font-weight: bold;">GOTO</span> :<span style="color: #b100b1; font-weight: bold;">DONE<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></span>
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS:~0,1</span><span style="color: #33cc33;">%</span>&quot;==&quot;/&quot; <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:GOTSWITCH</span>
<span style="color: #00b100; font-weight: bold;">IF</span> &quot;<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS:~0,1</span><span style="color: #33cc33;">%</span>&quot;==&quot;-&quot; <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:GOTSWITCH</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">Root</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS</span><span style="color: #33cc33;">%</span>
<span style="color: #00b100; font-weight: bold;">CALL</span> :<span style="color: #b100b1; font-weight: bold;">Root &quot;%<span style="color: #448888;">ROOT</span><span style="color: #33cc33;">%</span>&quot;</span>
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:ParseParams</span>
:<span style="color: #b100b1; font-weight: bold;">GOTSWITCH</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">COMPACT</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWDATE</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWATTRIB</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWSIZES</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWFILES</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">TREESTYLE</span>=<span style="color: #66cc66;">&#41;</span>
:<span style="color: #b100b1; font-weight: bold;">NEXTSWITCH</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PARAM</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS:~0,1</span><span style="color: #33cc33;">%</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PARAMS</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS:~1</span><span style="color: #33cc33;">%</span>
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>d<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWDATE</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>a<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWATTRIB</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWSIZES</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>f<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SHOWFILES</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>t<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">TREESTYLE</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAM</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>c<span style="color: #66cc66;">&#93;</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">COMPACT</span>=True
<span style="color: #00b100; font-weight: bold;">IF</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">PARAMS</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:NEXTSWITCH</span>
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">COMPACT</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>True<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">TREESTYLE</span>=<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:ParseParams</span>
&nbsp;
:<span style="color: #b100b1; font-weight: bold;">Done</span>
<span style="color: #b1b100; font-weight: bold;">pause</span>
<span style="color: #00b100; font-weight: bold;">GOTO</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
:<span style="color: #b100b1; font-weight: bold;">ROOT</span>
<span style="color: #b1b100; font-weight: bold;">ECHO</span>.
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> Showdate=<span style="color: #33cc33;">%</span><span style="color: #448888;">Showdate</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> SHOWATTRIB=<span style="color: #33cc33;">%</span><span style="color: #448888;">SHOWATTRIB</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> SHOWSIZES=<span style="color: #33cc33;">%</span><span style="color: #448888;">SHOWSIZES</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> SHOWFILES=<span style="color: #33cc33;">%</span><span style="color: #448888;">SHOWFILES</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> FileSing=<span style="color: #33cc33;">%</span><span style="color: #448888;">FileSing</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> FileMult=<span style="color: #33cc33;">%</span><span style="color: #448888;">FileMult</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> DirSing=<span style="color: #33cc33;">%</span><span style="color: #448888;">DirSing</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> DirMult=<span style="color: #33cc33;">%</span><span style="color: #448888;">DirMult</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #00b100; font-weight: bold;">goto</span> <span style="color: #b100b1; font-weight: bold;">:EOF</span>
&nbsp;
<span style="color: #b1b100; font-weight: bold;">ECHO</span> <span style="color: #33cc33;">%</span>~f1
:<span style="color: #b100b1; font-weight: bold;">WalkTree</span>
<span style="color: #b1b100; font-weight: bold;">SETLOCAL</span>
:<span style="color: #b100b1; font-weight: bold;">: <span style="color: #b1b100; font-weight: bold;">Set</span> up Remainder of switches -- ME DirSpec |<span style="color: #66cc66;">&#91;</span>|...<span style="color: #66cc66;">&#93;</span> FileSpec</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">Echo</span> Params: <span style="color: #33cc33;">%</span><span style="color: #448888;">*</span>
<span style="color: #00b100; font-weight: bold;">IF</span> <span style="color: #000000; font-weight: bold;">NOT</span> <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">1</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> PUSHD <span style="color: #33cc33;">%</span><span style="color: #448888;">1</span>
<span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">RECURPIPE</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">~<span style="color: #cc66cc;">2</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">LASTDIR</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">LASTFILE</span>=<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">For</span> /D </span><span style="color: #33cc33;">%%</span><span style="color: #448888;">D <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>*.*<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">DO</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">LASTDIR</span>=</span><span style="color: #33cc33;">%%</span><span style="color: #448888;">D
<span style="color: #00b100; font-weight: bold;">FOR</span> </span><span style="color: #33cc33;">%%</span><span style="color: #448888;">F <span style="color: #00b100; font-weight: bold;">in</span> <span style="color: #66cc66;">&#40;</span>*.*<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">DO</span> <span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">LASTFILE</span>=</span><span style="color: #33cc33;">%%</span>F
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">Echo</span> LastFile=<span style="color: #33cc33;">%</span><span style="color: #448888;">LASTFILE</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">Echo</span> LastDir =<span style="color: #33cc33;">%</span><span style="color: #448888;">LASTDIR</span><span style="color: #33cc33;">%</span>
<span style="color: #808080; font-style: italic;">REM</span> ~ <span style="color: #b1b100; font-weight: bold;">ECHO</span> RecursivePipe <span style="color: #33cc33;">%</span><span style="color: #448888;">RECURPIPE</span><span style="color: #33cc33;">%</span> Multi <span style="color: #33cc33;">%</span><span style="color: #448888;">FILEMULT</span><span style="color: #33cc33;">%</span> single <span style="color: #33cc33;">%</span><span style="color: #448888;">FILESING</span><span style="color: #33cc33;">%</span>
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">TREESTYLE</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>True<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">VPAD</span>=True<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PADDING</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SPC</span>=   <span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileSing</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileMult</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DirSing</span>=ÀÄÄÄ<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DirMult</span>=ÃÄÄÄ<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DIRPAD</span>=®<span style="color: #b1b100; font-weight: bold;">DIR</span>¯   <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> <span style="color: #00b100; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span>
 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">VPAD</span>=True<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PADDING</span>= <span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SPC</span>=  <span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileSing</span>=À<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileMult</span>=Ã<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DirSing</span>=Ô<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DirMult</span>=Æ<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PipeCHR</span>=³<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">DIRPAD</span>=®<span style="color: #b1b100; font-weight: bold;">DIR</span>¯   <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #00b100; font-weight: bold;">IF</span> /I <span style="color: #66cc66;">&#91;</span><span style="color: #33cc33;">%</span><span style="color: #448888;">COMPACT</span><span style="color: #33cc33;">%</span><span style="color: #66cc66;">&#93;</span>==<span style="color: #66cc66;">&#91;</span>True<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">Set</span> <span style="color: #448844;">VPAD</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">PADDING</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">SPC</span>=<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileSing</span>=À<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100; font-weight: bold;">SET</span> <span style="color: #448844;">FileMult</span>=Ã<span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #66cc66;">&#40;</span><span style="color: #