ZTree.com  | ZEN  | About...  

 Index   Back

Begging For Trouble   [Q]

By: Art Kocsis     So Calif  
Date: Oct 02,2017 at 12:03
In Response to: it is not quite so, still not working, still suspicious. (Ryan)

> FOR /F "DELIMS=" %%V IN ('"%SID1%"') DO SET SID2=%%V
> i need ECHO thing without : because when i see "echo is on", i know
> that it tells me something is wrong with the upper line,
> i have to make sure SID must not be a null string or a half finished
> string because
> i have to import a lot of [-HKEY_CURREN...] to delete the registries
> before i write the new ones, yes it is a very dangerous operation.

It is very bad practice to rely on a non-obvious, non-related command behavior for testing - especially for error checking. When you get older (as I am), you will find that you need all the reminders, double checking and obviousness that you can get.

In this case, relying on your awareness to catch the "Echo is on" behavior and to NOT have an inadvertent twitch of yout finger on the keyboard for dangerous operations is just begging for trouble and catastrophy! After a few dozen or hundred of your thousands you may get a little sloppy!

Since it is so critical, you need a non-bypassable and [hopefully] reliable error trap such as:

FOR /F "DELIMS=" %%V IN ('"%SID1%"') DO SET SID2=%%V
Set x=%SID2:-=%

::For S-1-5-21-746137067-1417001333-682574330-1003 formats
Set y=S-%X:~1,1%-%X:~2,1%-%X:~3,2%-%X:~5,9%-%X:~14,10%-%X:~24,9%-%X:~33,4%
If "%SID2%" EQU "%Y%" GoTo Valid

::For S-1-5-21-1004336348-1177238915-682003330-512 formats
Set y=S-%X:~1,1%-%X:~2,1%-%X:~3,2%-%X:~5,10%-%X:~15,10%-%X:~25,9%-%X:~34,3%
If "%SID2%" EQU "%Y%" GoTo Valid

::Additional formats
Set y=S...
If "%SID2%" EQU "%Y%" GoTo Valid

::Error Condition Handler
Echo ### Error ### Invalid SID2
GoTo _____


See these links for further info on SIDs and their formats:

> ECHO 1>>m:\aa.txt doesnt work
> ECHO 11>>m:\aa.txt works
> ECHO a>>m:\aa.txt works
> ECHO ab>>m:\aa.txt works

> i think it doesnt like one number, there must be something special about this 1>> or 2>>

As NIck said, there is special meaning to a 0, 1 or 2 immediately preceding the redirection symbol.
Unless you have a compelling reason for no trailing spaces (or wrap them in parenteses as Nick suggested), get in the habit of inserting at least one space between the command line or Echo text and the ">" symbol. This removes the ambiguity for the command processor and makes for much more readable and error free code.

ECHO 1 >> m:\aa.txt should now work
ECHO 11 >> m:\aa.txt works
ECHO a >> m:\aa.txt works
ECHO ab >> m:\aa.txt works

BTW, a great source of info on redirection is Rob Van de Woude's page at: http://www.robvanderwoude.com/redirection.php
Lots of useful info - in fact, his entire site (as well as http://ss64.com), are founts of info for batch programming. Good sites to bookmark.

Namaste', Art

Press any key to exit,
Press any other key to continue...
- unknown

Thread locked

Messages in this Thread

95,078 Postings in 11,987 Threads, 350 registered users, 124 users online (0 registered, 124 guests)
Index | Admin contact |   Forum Time: Aug 15, 2020 - 9:54 am EDT  |  Hits:33,138,911  (10,740 Today )
RSS Feed