ZTree.com  | ZEN  | About...  

 Index   Back

The batch anomalies are to do with your code, I'm afraid   [Q]

By: Nick Williams     West Sussex, England (forum time +5 hrs)  
Date: Sep 19,2017 at 09:54
In Response to: [Q] batch anomalies related with SID and >> operator (Ryan)

Hi Ryan,

Although you may notice differences between Windows versions, the problems (anomalies) you're experiencing are to do with errors in your code. Lets correct them and get things working.

Anomaly 1

First off, it's those pesky percent (%) symbols again. I've noticed over many of your code examples, you use three percents for a FOR-loop variable, e.g. %%%V.

This is definitely wrong, and although you may have "got away with it" up till now, it has come to bite you this time!

If you are writing a batch (*.BAT or *.CMD) file, you need to specify two percents for a FOR-loop variable, e.g. %%V. If you are typing batch commands directly into ZTreeWin's [F9] Menu, then you must double-up the percents, e.g. %%V becomes %%%%V.

Next, %%USERNAME%% is correct for ZTW Menu, but the corrected examples I'm providing (below) are written for a batch file, so I've reduced %%USERNAME%% to %USERNAME% to get it working.

The main problem was your use of single quotes around ("%SID1%"), i.e. ('"%SID1%"'). Single quotes are used for processing the output from a batch command, e.g. FOR /F "DELIMS=" %%V IN ('DIR C:\') DO etc.

Note also, the use of "ECHO:" instead of just plain "ECHO ". If there's nothing to ECHO, the ECHO command will display its current setting, e.g. "ECHO is off".
I use "ECHO:" to suppress this, but you will find numerous examples on the 'net, where "ECHO." is used -- it works just the same.

OK, lets get "anamoly 1" working:-

> anomaly 1
> AA=
> SET SID1=WMIC useraccount where name="%%USERNAME%%" get sid
> FOR /F "DELIMS=" %%%V IN ('"%SID1%"') DO SET SID2=%%%V

SET SID1=WMIC useraccount where name="%USERNAME%" get sid
FOR /F "DELIMS=" %%V IN ("%SID1%") DO SET SID2=%%V

The FOR loop could even be reduced to:-

FOR %%V IN ("%SID1%") DO SET SID2=%%~V

Anomaly 2

Anomaly 2 suffered with the same problems with three percents (%%%K), but other than that you were nearly there! Here's a corrected version:-

> FOR /F "TOKENS=1 DELIMS= " %%%K IN ("1 23") DO SET D1=%%%K
> FOR /F "TOKENS=2 DELIMS= " %%%K IN ("1 23") DO SET D2=%%%K
> ECHO works %D1%
> ECHO works %D2%
> ECHO doesnt work=%D1%>>%OUT%
> ECHO works but with a space at the end=%D1% >>%OUT%
> ECHO works=%D2%>>%OUT%


FOR /F "TOKENS=1" %%K IN ("1 23") DO SET D1=%%K
FOR /F "TOKENS=2" %%K IN ("1 23") DO SET D2=%%K



Note that FOR-loops are computationally "expensive". There's an opportunity here to condense your two FOR-loops into one, as follows:-

:: FOR /F "TOKENS=1" %%K IN ("1 23") DO SET D1=%%K
:: FOR /F "TOKENS=2" %%K IN ("1 23") DO SET D2=%%K

FOR /F "TOKENS=1,2" %%K IN ("1 23") DO (
SET D1=%%K
SET D2=%%L

Also your two ECHO-to-%OUT% statements could be grouped together, like this:-

:: ECHO:%D1%>>%OUT%
:: ECHO:%D2%>>%OUT%

) >>%OUT%

If you want to ensure that no trailing spaces appear in your %OUT% file, you can wrap your ECHO commands with parentheses, as follows:-

(ECHO:%D1%) >>%OUT%
(ECHO:%D2%) >>%OUT%


) >>%OUT%

Hope all this helps.

Kind regards

Thread locked

Messages in this Thread

95,078 Postings in 11,987 Threads, 350 registered users, 110 users online (0 registered, 110 guests)
Index | Admin contact |   Forum Time: Aug 15, 2020 - 10:12 am EDT  |  Hits:33,139,246  (11,075 Today )
RSS Feed