0phoi5 Posted October 17, 2016 Share Posted October 17, 2016 (edited) I'm attempting to create a FOR each user DO set a variable 'UserID' and then echo each User ID back. Why does this . . . FOR %%Z IN (SDESK1 SDESK2 SDESK3 SDESK4) DO (set UserID=%%Z echo %UserID%) . . . not set %UserID% correctly? It just echoes as '%Z' The output should technically just list each UserID one at a time; SDESK1 SDESK2 SDESK3 SDESK4 Sorry, it's been a while since I used FOR in batch and I'm sure I must be getting some syntax wrong! Cheers. Edited October 18, 2016 by haze1434 Resolved. Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted October 17, 2016 Share Posted October 17, 2016 you need to stop CMD from expanding the environment variables too early (i.e. before %%Z is populated by the for loop) try adding this before the for loop Setlocal EnableDelayedExpansion Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted October 17, 2016 Author Share Posted October 17, 2016 (edited) Unfortunately, that still doesn't seem to like it. At the moment, my script is this; @echo off Setlocal EnableDelayedExpansion FOR %%Z IN (SDESK1 SDESK2 SDESK3 SDESK4) DO (set UserID=%%Z call :DoIt) :DoIt echo %%Z Pause But the output is; %Z Press any key to continue . . . When it should be... SDESK1 Press any key to continue . . . Edited October 17, 2016 by haze1434 Quote Link to comment Share on other sites More sharing options...
Jason Cooper Posted October 17, 2016 Share Posted October 17, 2016 Try using ! instead of % to delay the expression evaluation. @echo off setlocal EnableDelayedExpansion FOR %%Z IN (SDESK1 SDESK2 SDESK3 SDESK4) DO ( set UserID=%%Z echo !UserID! ) Quote Link to comment Share on other sites More sharing options...
0phoi5 Posted October 17, 2016 Author Share Posted October 17, 2016 2 hours ago, Jason Cooper said: Try using ! instead of % to delay the expression evaluation. @echo off setlocal EnableDelayedExpansion FOR %%Z IN (SDESK1 SDESK2 SDESK3 SDESK4) DO ( set UserID=%%Z echo !UserID! ) You beauty. That got it. Thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.