ZTree.com  | ZEN  | About...  

 Index   Back

Another approach that doesn't use SETLOCAL   [Q]

By: Nick Williams     West Sussex, England (forum time +5 hrs)  
Date: Sep 21,2017 at 10:53
In Response to: i think i found the anomaly, looks like an XP's bug (Ryan)

> i didnt want to use EnableDelayed from the beginning. because of !
> thing that ruins the variables. and i dont know how to handle this well.

OK, here's another way of solving the problem. This approach doesn't rely on


so may prefer it:-

SET "SID1=WMIC useraccount where name="%USERNAME%" get sid"
ECHO:%%SID1%% = [%SID1%]
FOR /F "SKIP=1 DELIMS=" %%V IN ('"%SID1%"') DO (
SET "SID2=%%V"
ECHO:%%SID2%% = [%SID2%]

> BB=
> ECHO:%%%%SID1%%%%=[%SID1%]
> SetLocal EnableExtensions EnableDelayedExpansion
> FOR /F "DELIMS=" %%%%V IN ('"%SID1%"') DO (
> SET tempWorkVar=%%%%V
> IF /I "!tempWorkVar:~0,1!"=="A" (
> SetLocal DisableExtensions DisableDelayedExpansion
> SET SID2=%%%%V
> )
> )
> ECHO:%%%%SID2%%%%=[%SID2%]
> pause
> BB= could be my invention or something, i couldnt find anyone doing
> this way in website. BB= works even with !'s in variables as in file
> names or data strings with !'s in them
> so i concluded that
> "SetLocal DisableExtensions DisableDelayedExpansion" is "EndLocal"
> you can tell me about BB= what is wrong or why it works, because it
> seems logical to me that it must work but sometimes it says "too many
> set locals" in a loop because i turn it on and off whenever i need to
> get away from ! corruptions.

You are on the right lines, but remember that SETLOCAL must have a matching ENDLOCAL (unless the end of file is reached). Also, SETLOCAL-ENDLOCAL pairs can be nested.
Have a read of SETLOCAL /? for more details.

A couple of notes for you:-

(1) Don't forget to double-up ALL of your percent signs when pasting into ZTreeWin's [F9] Menu. (Yes, I know you can get away with not doubling-up some, but it's better to have a consistent approach to this.)

(2) I know you don't like double-quotes around SETs, but consider this example:-


The intention here is to initialise SID2 to UNDEFINED. But if your editor had some trailing space after the equals sign, you may not realise that the SID2 variable is being set to a string of one or spaces !

To remove any doubt, use double-quotes, as follows:-


Hope this helps.

Kind regards

Thread locked

Messages in this Thread

95,077 Postings in 11,987 Threads, 350 registered users, 30 users online (1 registered, 29 guests)
Index | Admin contact |   Forum Time: Aug 14, 2020 - 6:00 pm EDT  |  Hits:33,120,568  (20,330 Today )
RSS Feed