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


SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

so may prefer it:-

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

> BB=
> @ECHO OFF
>
> SET SID1=DIR /B
> SET SID2=
> 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:-

SET SID2=

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:-

SET "SID2="

Hope this helps.


Kind regards
Nick

292 views      
Thread locked
 

Messages in this Thread

 
94,426 Postings in 11,918 Threads, 348 registered users, 8 users online (0 registered, 8 guests)
Index | Admin contact |   Forum Time: Sep 22, 2019 - 12:03 pm EDT  |  Hits:28,656,163  (1,005 Today )
RSS Feed