<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Windows &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/windows/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Tue, 04 Sep 2012 15:28:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0</generator>
<site xmlns="com-wordpress:feed-additions:1">193486638</site>	<item>
		<title>Forcing IE to run as IE9 with embedded web browser</title>
		<link>/blog/index.php/archives/1720</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Tue, 28 Aug 2012 12:49:56 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[WindowsForms]]></category>
		<category><![CDATA[WPF]]></category>
		<guid isPermaLink="false">/blog/?p=1720</guid>

					<description><![CDATA[If you embed IE in a windows application, you may notice that it runs in IE7 Emulated mode. This is likely NOT what you want. I’ve added code like below to solve the problem: string appName = &#34;&#34;; try { appName = Path.GetFileName(Assembly.GetEntryAssembly().Location); const string IE_EMULATION = @&#34;Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION&#34;;  using (var fbeKey = Registry.CurrentUser.OpenSubKey(IE_EMULATION, true))  [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>If you embed IE in a windows application, you may notice that it runs in IE7 Emulated mode. This is likely NOT what you want.</p>
<p>I’ve added code like below to solve the problem:</p>
<pre class="code"><p><span style="color: blue">string </span>appName = <span style="color: #a31515">&quot;&quot;</span>;
<span style="color: blue">try
</span>{
    appName = <span style="color: #2b91af">Path</span>.GetFileName(<span style="color: #2b91af">Assembly</span>.GetEntryAssembly().Location);
</p><p>    <span style="color: blue">const string </span>IE_EMULATION = <span style="color: #900312">@&quot;Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION&quot;</span>; <br /><span style="color: blue">    using </span>(<span style="color: blue">var </span>fbeKey = <span style="color: #257f9f">Registry</span>.CurrentUser.OpenSubKey(IE_EMULATION, <span style="color: blue">true</span>)) <br />    {  <br />        fbeKey.SetValue(appName, 9000, <span style="color: #257f9f">RegistryValueKind</span>.DWord);  <br />    }<br />}</p><span style="color: blue">catch</span>(<span style="color: #2b91af">Exception </span>ex)
{
    <span style="color: #2b91af">MessageBox</span>.Show(appName + <span style="color: #a31515">&quot;\n&quot; </span>+ ex.ToString(), <span style="color: #a31515">&quot;Unexpected error setting browser mode!&quot;</span>);
}</pre>
<p>There’s a special registry key that must be set before IE9 loads and is navigated … it must be set to the name of your application. Oddly, not the full path to your application, just the name of your executable.</p>
<p>You may want to delete this registry setting when your application exits.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1720</post-id>	</item>
		<item>
		<title>WinRT/Xaml/AKA Metro DataTemplate selection based on Data Types</title>
		<link>/blog/index.php/archives/1705</link>
					<comments>/blog/index.php/archives/1705#comments</comments>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Tue, 21 Aug 2012 01:02:03 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Metro]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Windows8]]></category>
		<category><![CDATA[Xaml]]></category>
		<guid isPermaLink="false">/blog/?p=1705</guid>

					<description><![CDATA[You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an ItemsControl. While unfortunate as this behavior is quite handy, it’s not too difficult to replicate the functionality using a DataTemplateSelector. WPF for example, could do something like this: &#60;DataTemplate DataType=&#34;{x:Type local:Task}&#34;&#62; [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.itemscontrol.aspx" target="_blank">ItemsControl</a>. While unfortunate as this behavior is quite handy, it’s not too difficult to replicate the functionality using a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.datatemplateselector" target="_blank">DataTemplateSelector</a>.</p>
<p>WPF for example, could do something like this: </p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">&lt;DataTemplate DataType=<span style="color: #006080">&quot;{x:Type local:Task}&quot;</span>&gt;<br />  &lt;StackPanel&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=TaskName}&quot;</span> /&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=Description}&quot;</span>/&gt;<br />    &lt;TextBlock Text=<span style="color: #006080">&quot;{Binding Path=Priority}&quot;</span>/&gt;<br />  &lt;/StackPanel&gt;<br />&lt;/DataTemplate&gt;</pre>
<p></div>
<p>When the Task type as shown above was found in a list, it would have been rendered as a <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.stackpanel.aspx" target="_blank">StackPanel</a> with three <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.textblock.aspx" target="_blank">TextBlock</a>s automatically. That rocked.</p>
<p>WinRT (Metro/Xaml, Windows 8 applications) are missing the DataType property of DataTemplates. Yes, some of you might say it’s not missing as it’s V1, but given the heritage of Windows 8 Xaml applications, I consider it missing.</p>
<p>While an exact duplicate of the functionality isn’t possible, it’s relatively simple to get close.</p>
<pre class="code"><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">GridView
    </span><span style="background: white; color: red">x</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">Name</span><span style="background: white; color: blue">=&quot;itemGridView&quot;
</span><span style="background: white; color: blue">    </span><span style="background: white; color: red">ItemsSource</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">Binding </span><span style="background: white; color: red">Source</span><span style="background: white; color: blue">={</span><span style="background: white; color: #a31515">StaticResource </span><span style="background: white; color: red">groupedItemsViewSource</span><span style="background: white; color: blue">}}&quot;
    </span><span style="background: white; color: red">ItemTemplateSelector</span><span style="background: white; color: blue">=&quot;{</span><span style="background: white; color: #a31515">StaticResource </span><span style="background: white; color: red">typedTemplateSelector</span><span style="background: white; color: blue">}&quot;
    </span><span style="background: white; color: red">SelectionMode</span><span style="background: white; color: blue">=&quot;None&quot;
    </span><span style="background: white; color: red">IsItemClickEnabled</span><span style="background: white; color: blue">=&quot;True&quot;</span><span style="background: white; color: blue">&gt;</span></pre>
<p>Take the GridView above for example (using the template project in Visual Studio 2012).</p>
<p>I’ve assigned the ItemTemplateSelector to an instance of the TypedTemplateSelector class I’ve created.</p>
<p>In the Resources for the Page, I added a custom DataTemplate:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/08/image.png" width="323" height="186" /></p>
<p>I’ve added a DataTemplate with a Key called <strong>Type : SampleDataItem</strong> (without the spaces though – they’re auto converted by my WordPress theme to a squiggle face: <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-confusedsmile" alt="Confused smile" src="blog/wpcontent/uploads/2012/08/wlEmoticon-confusedsmile.png" />).</p>
<p>There’s nothing special about the Template, just the name. It must start with <strong>Type:.</strong></p>
<p>Here the custom template selector is being constructed in the Resources:</p>
<pre class="code"><span style="background: white; color: black">    </span><span style="background: white; color: blue">&lt;</span><span style="background: white; color: #a31515">local</span><span style="background: white; color: blue">:</span><span style="background: white; color: #a31515">TypedTemplateSelector </span><span style="background: white; color: red">x</span><span style="background: white; color: blue">:</span><span style="background: white; color: red">Key</span><span style="background: white; color: blue">=&quot;typedTemplateSelector&quot;
                                 </span><span style="background: white; color: red">DefaultTemplateKey</span><span style="background: white; color: blue">=&quot;Standard250x250ItemTemplate&quot; /&gt;
&lt;/</span><span style="background: white; color: #a31515">Page.Resources</span><span style="background: white; color: blue">&gt;</span></pre>
<p>Here’s the class that you’d need below. It has a caching feature (<strong>IsCacheEnabled</strong>) to prevent the search from occurring more than once for a key. It’s set to True by default. When used, the object searches from the current Item through all Parents trying to match the type of the object (via the <a href="http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.visualtreehelper.aspx" target="_blank">VisualTreeHelper</a>). Only the ClassName is used as programmed below (and not the full namespace). You could easily change this behavior by removing the <strong>Split </strong>and <strong>Last </strong>calls.</p>
<pre class="code"><span style="background: white; color: blue">using </span><span style="background: white; color: black">System;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Collections.Generic;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Linq;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Text;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">System.Threading.Tasks;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml.Controls;
</span><span style="background: white; color: blue">using </span><span style="background: white; color: black">Windows.UI.Xaml.Media;

</span><span style="background: white; color: blue">namespace </span><span style="background: white; color: black">WiredPrairie.TemplateSelector
{
    </span><span style="background: white; color: blue">public class </span><span style="background: white; color: #2b91af">TypedTemplateSelector </span><span style="background: white; color: black">: </span><span style="background: white; color: #2b91af">DataTemplateSelector
    </span><span style="background: white; color: black">{
        </span><span style="background: white; color: blue">private </span><span style="background: white; color: #2b91af">Dictionary</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">, </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">&gt; _cachedDataTemplates;

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Fallback value for DataTemplate
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">public string </span><span style="background: white; color: black">DefaultTemplateKey { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">set</span><span style="background: white; color: black">; }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Cache search results for a type (defaults to Enabled)
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">public bool </span><span style="background: white; color: black">IsCacheEnabled { </span><span style="background: white; color: blue">get</span><span style="background: white; color: black">; </span><span style="background: white; color: blue">set</span><span style="background: white; color: black">; }

        </span><span style="background: white; color: blue">public </span><span style="background: white; color: black">TypedTemplateSelector()
        {
            IsCacheEnabled = </span><span style="background: white; color: blue">true</span><span style="background: white; color: black">;
        }

        </span><span style="background: white; color: blue">protected override </span><span style="background: white; color: black">Windows.UI.Xaml.</span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">SelectTemplateCore(</span><span style="background: white; color: blue">object </span><span style="background: white; color: black">item, Windows.UI.Xaml.</span><span style="background: white; color: #2b91af">DependencyObject </span><span style="background: white; color: black">container)
        {
            </span><span style="background: white; color: green">// grab the Type name. Type will be searched as Type:NAME as shown below
            /*
                &lt;DataTemplate x:Key=&quot;Type:SampleDataItem&quot;&gt;
                    &lt;Grid HorizontalAlignment=&quot;Left&quot; Width=&quot;250&quot; Height=&quot;250&quot;&gt;
                        &lt;TextBlock Text=&quot;{Binding Title}&quot; /&gt;
                    &lt;/Grid&gt;
                &lt;/DataTemplate&gt;
             */
            </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key = item != </span><span style="background: white; color: blue">null </span><span style="background: white; color: black">? </span><span style="background: white; color: blue">string</span><span style="background: white; color: black">.Format(</span><span style="background: white; color: #a31515">&quot;Type:{0}&quot;</span><span style="background: white; color: black">, item.GetType().Name.Split(</span><span style="background: white; color: #a31515">'.'</span><span style="background: white; color: black">).Last()) : DefaultTemplateKey;
            </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt = GetCachedDataTemplate(key);
            </span><span style="background: white; color: blue">try
            </span><span style="background: white; color: black">{
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">) { </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt; }

                </span><span style="background: white; color: green">// look at all parents (visual parents)
                </span><span style="background: white; color: #2b91af">FrameworkElement </span><span style="background: white; color: black">fe = container </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
                </span><span style="background: white; color: blue">while </span><span style="background: white; color: black">(fe != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    dt = FindTemplate(fe, key);
                    </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">) { </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt; }
                    </span><span style="background: white; color: green">// if you were to just look at logical parents,
                    // you'd find that there isn't a Parent for Items set
                    </span><span style="background: white; color: black">fe = </span><span style="background: white; color: #2b91af">VisualTreeHelper</span><span style="background: white; color: black">.GetParent(fe) </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
                }

                dt = FindTemplate(</span><span style="background: white; color: blue">null</span><span style="background: white; color: black">, key);
                </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt;
            }
            </span><span style="background: white; color: blue">finally
            </span><span style="background: white; color: black">{
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    AddCachedDataTemplate(key, dt);
                }
            }
        }

        </span><span style="background: white; color: blue">private </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">GetCachedDataTemplate(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key)
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(!IsCacheEnabled) { </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">; }
            VerifyCachedDataTemplateStorage();
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(_cachedDataTemplates.ContainsKey(key))
            {
                </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">_cachedDataTemplates[key];
            }

            </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">;
        }

        </span><span style="background: white; color: blue">private void </span><span style="background: white; color: black">AddCachedDataTemplate(</span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key, </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt)
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(!IsCacheEnabled) { </span><span style="background: white; color: blue">return</span><span style="background: white; color: black">; }
            VerifyCachedDataTemplateStorage();
            _cachedDataTemplates[key] = dt;
        }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Delay creates storage
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        </span><span style="background: white; color: blue">private void </span><span style="background: white; color: black">VerifyCachedDataTemplateStorage()
        {
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(_cachedDataTemplates == </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
            {
                _cachedDataTemplates = </span><span style="background: white; color: blue">new </span><span style="background: white; color: #2b91af">Dictionary</span><span style="background: white; color: black">&lt;</span><span style="background: white; color: blue">string</span><span style="background: white; color: black">, </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">&gt;();
            }

        }

        </span><span style="background: white; color: gray">/// &lt;summary&gt;
        /// </span><span style="background: white; color: green">Returns a template
        </span><span style="background: white; color: gray">/// &lt;/summary&gt;
        /// &lt;param name=&quot;source&quot;&gt;</span><span style="background: white; color: green">Pass null to search entire app</span><span style="background: white; color: gray">&lt;/param&gt;
        /// &lt;param name=&quot;key&quot;&gt;&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;
        </span><span style="background: white; color: blue">private static </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">FindTemplate(</span><span style="background: white; color: blue">object </span><span style="background: white; color: black">source, </span><span style="background: white; color: blue">string </span><span style="background: white; color: black">key)
        {
            </span><span style="background: white; color: blue">var </span><span style="background: white; color: black">fe = source </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">FrameworkElement</span><span style="background: white; color: black">;
            </span><span style="background: white; color: blue">object </span><span style="background: white; color: black">obj;
            </span><span style="background: white; color: #2b91af">ResourceDictionary </span><span style="background: white; color: black">rd = fe != </span><span style="background: white; color: blue">null </span><span style="background: white; color: black">? fe.Resources : </span><span style="background: white; color: #2b91af">App</span><span style="background: white; color: black">.Current.Resources;
            </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(rd.TryGetValue(key, </span><span style="background: white; color: blue">out </span><span style="background: white; color: black">obj))
            {
                </span><span style="background: white; color: #2b91af">DataTemplate </span><span style="background: white; color: black">dt = obj </span><span style="background: white; color: blue">as </span><span style="background: white; color: #2b91af">DataTemplate</span><span style="background: white; color: black">;
                </span><span style="background: white; color: blue">if </span><span style="background: white; color: black">(dt != </span><span style="background: white; color: blue">null</span><span style="background: white; color: black">)
                {
                    </span><span style="background: white; color: blue">return </span><span style="background: white; color: black">dt;
                }
            }
            </span><span style="background: white; color: blue">return null</span><span style="background: white; color: black">;

        }
    }
}
</span></pre>
]]></content:encoded>
					
					<wfw:commentRss>/blog/index.php/archives/1705/feed</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1705</post-id>	</item>
		<item>
		<title>Get path or location of currently executing batch/command file in Windows</title>
		<link>/blog/index.php/archives/1592</link>
		
		<dc:creator><![CDATA[Aaron]]></dc:creator>
		<pubDate>Sun, 01 Apr 2012 15:22:14 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Batch File]]></category>
		<category><![CDATA[Windows]]></category>
		<guid isPermaLink="false">/blog/?p=1592</guid>

					<description><![CDATA[I didn’t know it was this simple, and am posting this information on my blog so I find it in the future, but hopefully this will help someone else! I’ve created a number of batch files over the years which routinely copy files from one location to another, usually as part of a backup strategy. [&#8230;]]]></description>
										<content:encoded><![CDATA[<blockquote>
<p>I didn’t know it was this simple, and am posting this information on my blog so I find it in the future, but hopefully this will help someone else!</p>
</blockquote>
<p>I’ve created a number of batch files over the years which routinely copy files from one location to another, usually as part of a backup strategy. However, I’ve always just hard-coded the paths of the drives, etc. into the batch files. While this works for drives which are permanently attached (or internal), it’s more fragile with external (flash/USB) drives. As I’ve never understood the logic of drive letter selection in Windows (letters usually are the same, but occasionally not), it meant that I was tweaking the drive letter in the batch file before running. Annoying, but it worked.</p>
<p><strong>Thanks to more than a few web sites, I now know there is a much better way!</strong></p>
<p>There are basically two decent options, depending on your scenario and requirements.</p>
<h3>Option 1</h3>
<p>If you are using drive letters (and not a mapped drive\network share), then you can use the variable %CD%.</p>
<p>It contains the “current directory.” So, that actually may be more than you wanted if the current directory isn’t the root of the drive.</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image.png" width="172" height="102" /></p>
<p>Simple, just chop it off:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image1.png" width="200" height="36" /></p>
<p><strong><font style="background-color: #ffff00">%CD:~0,2%</font></strong></p>
<p>The colon and tilde character is a flag which indicates that a substring should be returned rather than the entire string. The first value is the zero-based starting index and the second is the number of characters you want to return:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image2.png" width="195" height="29" /></p>
<p>The above starts at character 4, and includes 3 characters.</p>
<p>For fun, you can use negative values:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image3.png" width="192" height="28" /></p>
<p>With only a single negative parameter, it returns the number of characters requested starting with the rightmost character.&#160; (Check <a href="http://www.dostips.com/DtTipsStringManipulation.php">here</a> for a bunch of examples on string manipulation.)</p>
<p>So, you could use knowledge in a batch file:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image4.png" width="425" height="23" /></p>
<p>The above line uses <strong><a href="http://technet.microsoft.com/en-us/library/cc733145(v=ws.10).aspx">robocopy</a></strong> (available in modern versions of Windows without an extra install) to copy from the folder \\server\Backups to the current path appended with \server\backups. So, if the batch file containing the <strong>robocopy</strong> command was executing on the J: drive, the resulting <strong>robocopy</strong> command would be:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image5.png" width="368" height="21" /></p>
<p>By using the :~0,2 syntax, regardless of the folder the batch file is located in, it always copies to the root of the J drive (as the first two characters are J and : ).</p>
<h3>Option 2</h3>
<p>The other option is a bit different as it only works in a batch or command file. </p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image6.png" width="129" height="47" /></p>
<p>Parameter zero (%0) in batch file represents the full path of the currently executing file (path and filename). The (dp) modifiers expand the value to be the “drive” and the “path,” excluding the file name. </p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image7.png" width="262" height="37" /></p>
<p>You can manipulate the value as well:</p>
<p><img loading="lazy" style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px auto; padding-left: 0px; padding-right: 0px; display: block; float: none; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="blog/wpcontent/uploads/2012/04/image8.png" width="354" height="118" /></p>
<p>I’m immediately going to adopt the first option into all of my “robocopy” batch files.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1592</post-id>	</item>
	</channel>
</rss>
