{"id":1592,"date":"2012-04-01T10:22:14","date_gmt":"2012-04-01T15:22:14","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1592"},"modified":"2012-04-01T10:22:15","modified_gmt":"2012-04-01T15:22:15","slug":"get-path-or-location-of-currently-executing-batchcommand-file-in-windows","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1592","title":{"rendered":"Get path or location of currently executing batch\/command file in Windows"},"content":{"rendered":"
\n

I didn\u2019t 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>\n<\/blockquote>\n

I\u2019ve 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\u2019ve 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\u2019s more fragile with external (flash\/USB) drives. As I\u2019ve 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>\n

Thanks to more than a few web sites, I now know there is a much better way!<\/strong><\/p>\n

There are basically two decent options, depending on your scenario and requirements.<\/p>\n

Option 1<\/h3>\n

If you are using drive letters (and not a mapped drive\\network share), then you can use the variable %CD%.<\/p>\n

It contains the \u201ccurrent directory.\u201d So, that actually may be more than you wanted if the current directory isn\u2019t the root of the drive.<\/p>\n

\"image\"<\/p>\n

Simple, just chop it off:<\/p>\n

\"image\"<\/p>\n

%CD:~0,2%<\/font><\/strong><\/p>\n

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>\n

\"image\"<\/p>\n

The above starts at character 4, and includes 3 characters.<\/p>\n

For fun, you can use negative values:<\/p>\n

\"image\"<\/p>\n

With only a single negative parameter, it returns the number of characters requested starting with the rightmost character.  (Check here<\/a> for a bunch of examples on string manipulation.)<\/p>\n

So, you could use knowledge in a batch file:<\/p>\n

\"image\"<\/p>\n

The above line uses 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 robocopy<\/strong> command was executing on the J: drive, the resulting robocopy<\/strong> command would be:<\/p>\n

\"image\"<\/p>\n

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>\n

Option 2<\/h3>\n

The other option is a bit different as it only works in a batch or command file. <\/p>\n

\"image\"<\/p>\n

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 \u201cdrive\u201d and the \u201cpath,\u201d excluding the file name. <\/p>\n

\"image\"<\/p>\n

You can manipulate the value as well:<\/p>\n

\"image\"<\/p>\n

I\u2019m immediately going to adopt the first option into all of my \u201crobocopy\u201d batch files.<\/p>\n","protected":false},"excerpt":{"rendered":"

I didn\u2019t 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\u2019ve 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. […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[4,5],"tags":[80,79],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pd5QIe-pG","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":22,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/22","url_meta":{"origin":1592,"position":0},"title":"Converting a mapped drive letter to a network path using C#","date":"March 2, 2008","format":false,"excerpt":"Occasionally you might have the need to convert a mapped drive letter to a UNC or network path. For example, a drive letter such as\u00a0 \"Z\" might be mapped to a network share: In this example, the \"Z\" drive is mapped to a \"Personal\" folder on a server named \"Home\".\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2165,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/2165","url_meta":{"origin":1592,"position":1},"title":"Automatic Reconnection of iSCSI Targets in Windows 10 using PowerShell","date":"April 4, 2017","format":false,"excerpt":"When my highly recommended Synology Disk Station reboots for a required update (I've got it set to automatically reboot), a shared Windows 10 PC in our house cannot always successfully reconnect to the iSCSI targets without manual intervention. Unfortunately, I haven't always noticed which has led to several features of\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2017\/04\/2017-04-04_19-07-23.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1773,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1773","url_meta":{"origin":1592,"position":2},"title":"AV Club \/ Our home setup 2012","date":"December 12, 2012","format":false,"excerpt":"I\u2019ve done a lot of reworking of our computers and network recently and thought I\u2019d post some details about the various physical and digital parts that make our humble WiredPrairie home work. Using Google Docs, I created a labeled diagram with most of the moving parts of our house (WOW,\u2026","rel":"","context":"In "Recommendations"","img":{"alt_text":"Home Setup","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/12\/Home-Setup_thumb1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1541,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1541","url_meta":{"origin":1592,"position":3},"title":"Adobe Lightroom and exporting to subfolders","date":"February 8, 2012","format":false,"excerpt":"For some reason, versions 1 \u2013 3 of Adobe Lightroom cannot export images in a way that mirrors the original structure of your photo library. I can\u2019t offer a reason why other than it was missed by the development and design teams. There\u2019s been enough interest in it apparently that\u2026","rel":"","context":"In "Photography"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2012\/02\/image2.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":1880,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1880","url_meta":{"origin":1592,"position":4},"title":"Using Windows CSCRIPT to compile a Handlebar.js template","date":"March 28, 2013","format":false,"excerpt":"I was looking for an alternative to using Node.JS for a JavaScript build process today on a Windows machine. I wanted something that relied as much on natively installed elements of a modern Windows PC as possible so that the build process would be portable. So, I broke out my\u2026","rel":"","context":"In "Coding"","img":{"alt_text":"image","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2013\/03\/image_thumb9.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":78,"url":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/78","url_meta":{"origin":1592,"position":5},"title":"How to use Vista’s Search Folders (You should try them!)","date":"April 7, 2008","format":false,"excerpt":"If you aren't using Vista's Search Folders yet, you should! (At least try them!). Here's how to create one. I'll use PSD files in this example. PSD files are files created by Photoshop. But you can substitute any file type as you follow along. First, start Windows Search: Windows Start\u2026","rel":"","context":"In "General"","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.wiredprairie.us\/blog\/wp-content\/uploads\/2008\/04\/image-thumb9.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1592"}],"collection":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/comments?post=1592"}],"version-history":[{"count":1,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1592\/revisions"}],"predecessor-version":[{"id":1593,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/posts\/1592\/revisions\/1593"}],"wp:attachment":[{"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/media?parent=1592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/categories?post=1592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wiredprairie.us\/blog\/index.php\/wpjson\/wp\/v2\/tags?post=1592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}