<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx</link><description>And its logical consequences.</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714734</link><pubDate>Wed, 23 Aug 2006 17:39:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714734</guid><dc:creator>Tom M</dc:creator><description>So the problem was that ERRORLEVEL was 0 at the point the batch script was loaded?</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714757</link><pubDate>Wed, 23 Aug 2006 17:51:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714757</guid><dc:creator>Mark</dc:creator><description>No, at the point the line - which spills onto multiple lines by using ( ) - was read. &amp;nbsp;Think of it as:&lt;br&gt;&lt;br&gt; &amp;nbsp;for ... do IsVarMap.exe ... &amp;amp; echo %ERRORLEVEL%</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714774</link><pubDate>Wed, 23 Aug 2006 18:04:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714774</guid><dc:creator>Mike</dc:creator><description>Are &amp;quot;procedures&amp;quot; inlined/precompiled too (I suspect not)? If not, could one potential solution be to &amp;quot;call&amp;quot; a &amp;quot;procedure&amp;quot; (a label) in the same batch file, assuming this is for NT5 or later?&lt;br&gt;&lt;br&gt;In a way, it reminds me of the pattern of C++ std::for_each, where you give start, end and a function to call for each iteration.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714840</link><pubDate>Wed, 23 Aug 2006 18:27:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714840</guid><dc:creator>KJK::Hyperion</dc:creator><description>Mike: &amp;quot;call :label&amp;quot; effectively reparses the batch file and resets the state - even the %0, %1, ... constants are reset. You even have to call &amp;quot;setlocal&amp;quot; at the beginning of a procedure, or your variables are going to spill into the caller's context&lt;br&gt;&lt;br&gt;So, yes, you can &amp;quot;call :label &amp;lt;arguments&amp;gt;&amp;quot; in a loop, even recursively, but I don't recommend it, as string escaping gets funky across a call</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714842</link><pubDate>Wed, 23 Aug 2006 18:28:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714842</guid><dc:creator>Arlie Davis</dc:creator><description>Must... &amp;nbsp;destroy... &amp;nbsp;CMD.EXE...&lt;br&gt;&lt;br&gt;Destroy...&lt;br&gt;&lt;br&gt;too late...&lt;br&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714880</link><pubDate>Wed, 23 Aug 2006 19:03:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714880</guid><dc:creator>dave</dc:creator><description>The issue arises when you start programming in a language not originally intended for programming.
&lt;br&gt;
&lt;br&gt;"Proper" programming languages tend to be a little clearer about the difference between lexical processing and command interpretation. &amp;nbsp;And anyone who's used a decent macrogenerator (not the C preprocessor) gets the concept.
&lt;br&gt;
&lt;br&gt;But command languages originally designed for people to type tend to create a little more confusion.
&lt;br&gt;&lt;br&gt;&lt;div class="post"&gt;[&lt;i&gt;Very true. I doubt anybody actually enjoys writing batch files. It's not so much a programming language as a script. -Raymond&lt;/i&gt;]&lt;/div&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714894</link><pubDate>Wed, 23 Aug 2006 19:16:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714894</guid><dc:creator>James</dc:creator><description>For extra pain - er, credit - try a .cmd script which generates registry entries (with variable substitution) which will then be executed. Some of them, via cmd.exe. Oh, you need to pipe variable stuff into another command while you're doing it.&lt;br&gt;&lt;br&gt;(Oh, the joys of building a setup which partitions, formats and installs a system automatically - with different partitioning schemes for different arguments, and different applications installed depending on the machine's DNS entry!)</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714905</link><pubDate>Wed, 23 Aug 2006 19:26:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714905</guid><dc:creator>OrsoYoghi</dc:creator><description>I'm missing something..&lt;br&gt;&lt;br&gt;why is SETLOCAL ENABLEDELAYEDEXPANSION required ? &lt;br&gt;I mean, cmd.exe can already tell between the twos because all old code is %WRITTENTHISWAY% while code using the new expansion style is !WRITTENTHISWAY! ...&lt;br&gt;&lt;br&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714926</link><pubDate>Wed, 23 Aug 2006 19:43:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714926</guid><dc:creator>pcooper</dc:creator><description>OrsoYoghi: I assume that's for backward compatibility with old batch files that assumed that text between exclamation points was just text between exclamation points. You can't just suddenly change the syntax of the file without requiring people to opt into the new functionality.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714937</link><pubDate>Wed, 23 Aug 2006 19:49:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714937</guid><dc:creator>Jonathan</dc:creator><description>@OrsoYoghi: Yes, but what if old code depended on !STUFF_WRITTEN_LIKE_THIS! being treated as a literal string? The only backwords-compatible way is to use the old method with an option to enable the new one. &lt;BR&gt;&lt;BR&gt;(Then again, couldn't you enable the new method by default in .cmd files, and leave .bat files alone?)&lt;BR&gt;&lt;BR&gt;&lt;DIV CLASS=post&gt;[&lt;I&gt;Making it on by default for .cmd files would break compatibility with NT4 .cmd files (which expected ! to mean !.) -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#714939</link><pubDate>Wed, 23 Aug 2006 19:51:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:714939</guid><dc:creator>Jonathan</dc:creator><description>I guess I was too slow. :) Sorry pcooper.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#715447</link><pubDate>Thu, 24 Aug 2006 00:47:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:715447</guid><dc:creator>Stu</dc:creator><description>That's interesting; the CMD team seem to think that it's ok to implement features that break compatability, as long as you make them opt-in. &lt;BR&gt;The rest of Microsoft just uses "it will break back-compatability" as a reason not to improve things...&lt;BR&gt;&lt;BR&gt;&lt;DIV CLASS=post&gt;[&lt;I&gt;cmd.exe is a much more tightly-scoped problem. Command scripts communicate with other command scripts only through environment variables and the command line. Command scripts can't inject themselves into other command scripts. There are no "cmd.exe plug-ins". -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#715490</link><pubDate>Thu, 24 Aug 2006 01:17:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:715490</guid><dc:creator>Sven Groot</dc:creator><description>I'd just like to say, thank God for Monad. :)</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#715728</link><pubDate>Thu, 24 Aug 2006 03:30:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:715728</guid><dc:creator>Mark Yocom</dc:creator><description>[Very true. I doubt anybody actually enjoys writing batch files. It's not so much a programming language as a script. -Raymond]&lt;br&gt;&lt;br&gt;Oh, I dunno - I enjoy writing them. &amp;nbsp;CMD is like your weird Uncle Harold. &amp;nbsp;He's fun to be around, despite his quirks.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#715861</link><pubDate>Thu, 24 Aug 2006 04:23:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:715861</guid><dc:creator>Barry Kelly</dc:creator><description>Ah yes, yet another reason I use bash / rxvt / Cygwin.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#715880</link><pubDate>Thu, 24 Aug 2006 04:31:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:715880</guid><dc:creator>Cooney</dc:creator><description>Yes, thank god for bash. cmd.exe isn't a script so much as a juiced up command.com. No wonder people hate the command line.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#716371</link><pubDate>Thu, 24 Aug 2006 09:22:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:716371</guid><dc:creator>geffner</dc:creator><description>&amp;gt; For extra pain - er, credit - try a .cmd&lt;br&gt;&amp;gt; script which generates registry entries&lt;br&gt;&amp;gt; (with variable substitution) which will then&lt;br&gt;&amp;gt; be executed. Some of them, via cmd.exe. Oh,&lt;br&gt;&amp;gt; you need to pipe variable stuff into another&lt;br&gt;&amp;gt; command while you're doing it. &lt;br&gt;&lt;br&gt;James, it sounds like you're using a batch file to write .reg files and execute them. It's much easier to use &amp;quot;%windir%\system32\reg.exe&amp;quot; from a batch file instead.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#716784</link><pubDate>Thu, 24 Aug 2006 12:19:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:716784</guid><dc:creator>James</dc:creator><description>Geffner: I am using reg, rather than creating .reg files; the 'will then be executed' refers to the entries themselves being RunOnceEx entries which get run after the second reboot. The stuff I'm piping into another command is a diskpart script, not a set of registry entries.&lt;br&gt;&lt;br&gt;Nothing too painful, just a fiddly set of scripts to tie all the partitioning and installation together. Especially now we have two virtual machines and a dual-boot Linux setup to contend with, and could be running anywhere from the labs (a dozen new user accounts created/deleted each day, permanent Net connection, everything from AutoCAD to Visual Studio running) to staff laptops (one user, usually offline).&lt;br&gt;&lt;br&gt;A bit more work upfront, but since the hardware's all heterogenous, a single &amp;quot;install&amp;quot; command with literally everything else automated saves enough staff time to justify it quite easily.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#717052</link><pubDate>Thu, 24 Aug 2006 16:07:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:717052</guid><dc:creator>dave</dc:creator><description>The real question is, of course, why sane people bother trying to program in a jumped-up MS-DOS 'batch' (which of course isn't a batch system at all) language when they could just install Perl.&lt;br&gt;&lt;br&gt;Perl has the benefit of bizarre syntax, thus giving ex-batch-programmers a warm and fuzzy feeling that there's something else for them to be puzzled over, but at the same time actually having the capabilities one needs.&lt;br&gt;&lt;br&gt;;-)</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#717372</link><pubDate>Thu, 24 Aug 2006 17:50:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:717372</guid><dc:creator>Batchman</dc:creator><description>Dave: Why don't you consider ms-dos batch files as a batch system at all?</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#717793</link><pubDate>Thu, 24 Aug 2006 19:12:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:717793</guid><dc:creator>mikeb</dc:creator><description>&amp;gt;&amp;gt; The real question is, of course, why sane people bother trying to program in a jumped-up MS-DOS 'batch' (which of course isn't a batch system at all) language when they could just install Perl. &amp;lt;&amp;lt;&lt;br&gt;&lt;br&gt;The reason is quite simple - because the cmd scripts will work without having to bother the user with installing Perl (or whatever other tool).&lt;br&gt;&lt;br&gt;The real problem with cmd scripting is that the syntax and semantics of the scripting language has been enhanced since the MS-DOS days in an ad-hoc manner rather than designing the language in a more formal manner. &amp;nbsp;At least that's what it looks like.&lt;br&gt;&lt;br&gt;The other big problem (at least for me) is that applications are responsible for parsing command lines instead of having the shell parse the commad line, so it can be a nightmare to convince some tools to accept parameters that contain special characters which need escaping (particularly quotes) that is also acceptable to cmd.exe.&lt;br&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#717896</link><pubDate>Thu, 24 Aug 2006 19:22:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:717896</guid><dc:creator>mikeb</dc:creator><description>&amp;gt;&amp;gt; The real question is, of course, why sane people bother trying to program in a jumped-up MS-DOS 'batch' (which of course isn't a batch system at all) language when they could just install Perl. &amp;lt;&amp;lt;&lt;br&gt;&lt;br&gt;The reason is quite simple - because the cmd scripts will work without having to bother the user with installing Perl (or whatever other tool).&lt;br&gt;&lt;br&gt;The real problem with cmd scripting is that the syntax and semantics of the scripting language has been enhanced since the MS-DOS days in an ad-hoc manner rather than designing the language in a more formal manner. &amp;nbsp;At least that's what it looks like.&lt;br&gt;&lt;br&gt;The other big problem (at least for me) is that applications are responsible for parsing command lines instead of having the shell parse the commad line, so it can be a nightmare to convince some tools to accept parameters that contain special characters which need escaping (particularly quotes) that is also acceptable to cmd.exe.&lt;br&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#720846</link><pubDate>Fri, 25 Aug 2006 08:04:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:720846</guid><dc:creator>steveg</dc:creator><description>No offence to the cmd team, but MS could've done it &amp;quot;properly&amp;quot; years ago. The big mistake IMHO was NT (3.51) maintained DOS cmd compatibility -- I'll let the Win9x family off the hook. Although perhaps the ultimate blame lies with OS2.&lt;br&gt;&lt;br&gt;It would've been very very useful if shell-level scripting had been built into the Window Manager right from the start (imagine being able to test/manipulate your UI from a script supported by the OS? App.Window[1].Maximise (as an aside it would've been awesome if Noah Webster had had another hobby apart from butchering the english language)).&lt;br&gt;&lt;br&gt;As for the &amp;quot;just use [perl|cygwin|.*]&amp;quot; argument, it doesn't always work in large organisations where introducing a new technology into the production environment is a long slow process requiring meetings of dozens, evaluation committees etc. .bat is very very good compared to facing that rubbish (maybe that's only the Australian Govt for you).</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#720849</link><pubDate>Fri, 25 Aug 2006 08:09:57 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:720849</guid><dc:creator>steveg</dc:creator><description>Before anyone mentions it vba makes a good scripting language not.</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#728485</link><pubDate>Mon, 28 Aug 2006 18:35:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:728485</guid><dc:creator>Jules</dc:creator><description>Raymond, your explanation of why this behaviour is the default doesn't make sense. &amp;nbsp;You say "prior to Windows NT, that was the only type of expansion supported by the command interpreter." &amp;nbsp;But prior to NT, the command interpreter only supported executing a single command per line read, so the question of when variables were expanded never arose: it happened immediately prior to execution, which also happened to be immediately after reading them. &lt;BR&gt;&lt;BR&gt;When the NT interpreter was written, somebody decided that this way was the best way of doing it. &amp;nbsp;I'm sure that CMD.EXE has always been written in C or some other high-level language, so the complexity of doing it the other way can't have been that bad. &amp;nbsp;So I still wonder why this decision went the way it did, when the other way is much more useful.&lt;BR&gt;&lt;BR&gt;&lt;DIV CLASS=post&gt;[&lt;I&gt;It also had to be compatible with OS/2 batch files, and OS/2's command interpreter was written in assembly language as I vaguely recall. -Raymond&lt;/I&gt;]&lt;/DIV&gt;</description></item><item><title>re: Environment variable expansion occurs when the command is read</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#733103</link><pubDate>Thu, 31 Aug 2006 11:00:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:733103</guid><dc:creator>Rexxx</dc:creator><description>OS/2 batch files = Rexx&lt;br&gt;Quite superior to Batch.</description></item><item><title>Using DOS command to Auto-Pick today&amp;#8217;s wallpaper &amp;laquo; maonet technotes</title><link>http://blogs.msdn.com/oldnewthing/archive/2006/08/23/714650.aspx#5243673</link><pubDate>Tue, 02 Oct 2007 18:13:46 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5243673</guid><dc:creator>Using DOS command to Auto-Pick today’s wallpaper « maonet technotes</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://maonet.wordpress.com/2007/10/01/using-dos-command-to-auto-pick-todays-wallpaper/"&gt;http://maonet.wordpress.com/2007/10/01/using-dos-command-to-auto-pick-todays-wallpaper/&lt;/a&gt;&lt;/p&gt;
</description></item></channel></rss>