home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.msdos.batch.nt      Fun with Windows NT batch files      68,980 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 67,967 of 68,980   
   JJ to R.Wieser   
   Re: echo a variable containing an & some   
   20 Nov 22 20:39:23   
   
   From: jj4public@outlook.com   
      
   On Sun, 20 Nov 2022 12:48:12 +0100, R.Wieser wrote:   
   > Hello all,   
   >   
   > I've written a test batch showing a directory tree.  In it I noticed that   
   > echo-ing an argument containing a pathname containing an "&" causes a   
   > problem when done directly :   
   >   
   > echo %~xn1   
   >   
   > but doesn't when done indirectly :   
   >   
   > for %%a in (%1) do echo %%~xna   
   >   
   > Does anyone have an explanation for it ?   
      
   FOR's variables are not parsed. AFAIK, FOR variables are expanded *after*   
   the command line has been parsed. Non-DelayedExpansion normal variables in   
   the command line are expanded *before* the command line is parsed.   
      
   > And a question : How do I tell the batchfile parser *not* to parse the %~xn1   
   > results but instead just display it as-is ?   
   >   
   > I know that I can wrap it in doublequotes :   
   >   
   > echo "%~xn1"   
   >   
   > but those doublequotes get displayed too. :-(   
      
   DelayedExpansion can be used, but escape character may need to be specified   
   twice when it's needed. Adding complexity for normal variables usage. It may   
   cause a problem which is unsolvable instead.   
      
   For non-DelayedExpansion normal variables, we'd have to use FOR variable to   
   display it. e.g.   
      
   @echo off   
   setlocal   
   set s=abc ^& def   
      
   echo this causes error   
   echo %s%   
      
   echo.   
   echo this won't cause error   
   for %%A in ("%s%") do echo %%~A   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca