<?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>Batch File &#8211; WiredPrairie</title>
	<atom:link href="blog/archives/tag/batch-file/feed" rel="self" type="application/rss+xml" />
	<link>/blog</link>
	<description>Yet another tech blog.</description>
	<lastBuildDate>Sun, 01 Apr 2012 15:22:15 +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>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>
