{"id":1056,"date":"2010-04-27T10:26:02","date_gmt":"2010-04-27T08:26:02","guid":{"rendered":"https:\/\/www.peppercrew.nl\/?p=1056"},"modified":"2011-05-18T20:48:57","modified_gmt":"2011-05-18T19:48:57","slug":"empty-variables-during-for-loops-in-batch-files","status":"publish","type":"post","link":"https:\/\/ingmarverheij.com\/en\/empty-variables-during-for-loops-in-batch-files\/","title":{"rendered":"Empty variables during FOR loops in batch files"},"content":{"rendered":"<div>Batch files (*.bat \/ *.cmd) are easy to build and yet powerful scripts .<br \/>\nAlthough time is catching up and batch files are more and more replaced by vbScript and PowerShell, there are occasions you need them.<\/div>\n<div>Batch files are especially powerful when you\u2019re using (for) loops and variables. But the combination of those two is somewhat of a problem. Why? It doesn\u2019t (seem to) work\u2026<\/div>\n<div>Let\u2019s take the following situation.<br \/>\nYou want to build a script that searches for files with a given extension in a given directory. You then strip the extension of the filename and process the file. This script would look like this:<br \/>\n<!--more--><\/div>\n<blockquote>\n<div><code>@ECHO OFF<br \/>\nREM Set constants<br \/>\nREM -------------<br \/>\nSET strSourceDirectory=C:App-V<br \/>\nSET strWildcard=*.osd<\/code><\/div>\n<p><code><\/p>\n<div>REM Search for files<br \/>\nREM ----------------<br \/>\nFOR \/F \"tokens=* delims=\" %%A IN ('DIR \/B \/S %strSourceDirectory%%strWildcard%') DO (<\/div>\n<div>REM Determine filename<br \/>\nREM ------------------<br \/>\nSET strFileName=%%A<\/div>\n<div>REM Determine filename without extension<br \/>\nREM ------------------------------------<br \/>\nSET strFileNameWithoutExtension=%strFilename:~0,-4%<br \/>\nECHO Filename = %strFileNameWithoutExtension%<br \/>\n)<\/div>\n<p><\/code><\/p><\/blockquote>\n<div>You would expect a list of files without the extension (.osd is 4 characters). However, the outcome of the script looks like this:<br \/>\n<code><\/p>\n<blockquote><p>Filename =<br \/>\nFilename =<br \/>\nFilename =<br \/>\nFilename =<\/p><\/blockquote>\n<p><\/code><\/div>\n<p><code> <\/code><\/p>\n<div>\nThere is no result, no outcome. Not what you would expect.<\/div>\n<div>The reason for this lies in the way  a batch file processes variables (or expands the variables). This doesn\u2019t happen during command execution, but before execution.  Of course you can solve this issue. By enabling <em>delayed variable expansion<\/em> and reading the variables the right way.<\/div>\n<div>First of all you need to enable <em>delayed variable expansion<\/em>. This can be achieved by calling the command interpreter via the command CMD \/V:ON \/C or simply by adding SETLOCAL ENABLEDELAYEDEXPANSION in the batch file.<\/div>\n<div>Next you need to read the variables on a different way. Not with percent signs, like you\u2019re used to, but with exclamation marks.<\/div>\n<div>Let take a look at the same script, but this time with <em>delayed variables expansion<\/em>.<\/div>\n<blockquote>\n<div><code>@ECHO OFF<br \/>\nREM Enable delay expansion<br \/>\nREM ----------------------<br \/>\nSETLOCAL ENABLEDELAYEDEXPANSION <\/code><\/div>\n<p><code><\/p>\n<div>REM Set constants<br \/>\nREM -------------<br \/>\nSET strSourceDirectory=C:App-V<br \/>\nSET strWildcard=*.osd<\/div>\n<div>REM Search for files<br \/>\nREM -----------------<br \/>\nFOR \/F \"tokens=* delims=\" %%A IN ('DIR \/B \/S %strSourceDirectory%%strWildcard%') DO (<\/div>\n<div>REM Determine filename<br \/>\nREM ------------------<br \/>\nSET strFileName=%%A<\/div>\n<div>REM Determine filename without extension<br \/>\nREM ------------------------------------<br \/>\nSET strFileNameWithoutExtension=!strFilename:~0,-4!<br \/>\nECHO Filename : !strFileNameWithoutExtension!<br \/>\n)<\/div>\n<p><\/code><\/p><\/blockquote>\n<div>Now if we execute the batch file, this would be the result. As expected.<br \/>\n<code><\/p>\n<blockquote><p>Filename = C:App-VGlobalSoftGrid ClientOSD Cacheffd89791-cecb-4550-a3b3-3d9805961e13<br \/>\nFilename = C:App-VGlobalSoftGrid ClientOSD Cacheffd89791-cecb-4550-a3b3-3d9805961e13<br \/>\nFilename = C:App-VGlobalSoftGrid ClientOSD Cacheffd89791-cecb-4550-a3b3-3d9805961e13<br \/>\nFilename = C:App-VGlobalSoftGrid ClientOSD Cacheffd89791-cecb-4550-a3b3-3d9805961e13<\/p><\/blockquote>\n<p><\/code><\/div>\n<p><code> <\/code><\/p>\n<div>Ingmar Verheij<\/div>","protected":false},"excerpt":{"rendered":"<p>Batch files (*.bat \/ *.cmd) are easy to build and yet powerful scripts . Although time is catching up and batch files are more and more replaced by vbScript and PowerShell, there are occasions you need them. Batch files are especially powerful when you\u2019re using (for) loops and variables. But the combination of those two [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[304],"tags":[158,159,160,161,162,163],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-batch-scripting","tag-batch","tag-delayed-variable-expansion","tag-for-loop","tag-no-outcome","tag-nothing","tag-variables"],"_links":{"self":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":2,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"predecessor-version":[{"id":2306,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/posts\/1056\/revisions\/2306"}],"wp:attachment":[{"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ingmarverheij.com\/en\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}