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=
> @ECHO OFF
> SET SID1=WMIC useraccount where name="%%USERNAME%%" get sid
> ECHO %SID1%
> FOR /F "DELIMS=" %%%V IN ('"%SID1%"') DO SET SID2=%%%V
> ECHO %SID2%
> PAUSE
>


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

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

>
> @ECHO OFF
> SET OUT=M:\!AA.TXT
>
> 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%
> PAUSE
>

@ECHO OFF
SET OUT=M:\!AA.TXT

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

ECHO:%D1%
ECHO:%D2%

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

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%

(
ECHO:%D1%
ECHO:%D2%
) >>%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%

-or-

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


Hope all this helps.


Kind regards
Nick

285 views      
Thread locked
 

Messages in this Thread

 
94,580 Postings in 11,930 Threads, 348 registered users, 15 users online (0 registered, 15 guests)
Index | Admin contact |   Forum Time: Nov 17, 2019 - 11:11 am EST  |  Hits:29,151,364  (11,325 Today )
RSS Feed