Computer Hope

Microsoft => Microsoft DOS => Topic started by: wild4bits on October 25, 2019, 11:53:07 AM

Title: if and else routine returns an error
Post by: wild4bits on October 25, 2019, 11:53:07 AM
@echo off
setlocal EnableDelayedExpansion
for /f %%i in (filename.txt) do (dsquery user -samid %%i | dsget user -memberof | findstr /c:"string" > nul
if !errorlevel! == 0 (
   echo %%i is a member
 ) ELSE (
   echo %%i is NOT a member
 )
)
endlocal

this batch file returns "%i is NOT a member"

I checked my syntax, tried different options for 'errorlevel' but no luck, before the message the batch takes about 5 minutes. what am I doing wrong?
Title: Re: if and else routine returns an error
Post by: Salmon Trout on October 25, 2019, 02:33:55 PM
In batch string compares, white space counts, so this if !errorlevel! == 0 is comparing !errorlevel! and a space, with a space plus "0", and this will never be true.

Either do this if !errorlevel!==0 or else do a number compare: if !errorlevel! equ 0
Title: Re: if and else routine returns an error
Post by: wild4bits on October 25, 2019, 03:49:11 PM
thank you for answering my post, I truly appreciate it. I've been rolling on the ground with this thing for a while now.

I did try your suggestion I amended the line to read "if !errorlevel!==0" and then to "if !errorlevel! equ 0"; still I got the same erroneous outcome "%i is NOT a member" which suggest I'm doing something wrong.

please advise.
Title: Re: if and else routine returns an error
Post by: Salmon Trout on October 25, 2019, 04:09:03 PM
Why don't you try echoing some of the variables and looking at them? Also not redirecting the command to nul? Is the file really called filename.txt? Do the lines in filename.txt contain spaces?
Title: Re: if and else routine returns an error
Post by: wild4bits on October 25, 2019, 09:43:29 PM
I will make the batch file display as it processes; and report back or if I figure it out I will report what I did.
the name of the file is mgr.txt, and no, it does not have spaces. Actually this part of the code works:


for /f %%i in (mgr.txt) do (dsquery user -samid %%i | dsget user -memberof | findstr /C:"Syncplicity"

so, that part of the code is solid and proven.

note: when I run this is the command line I remove the second % symbol.

note: when run as above I add the >>file.txt line, but of course all I get is the name of the distinguished name of the group, which is useless.

What I'm trying to do is compare the usernames contained in mgr.txt to the names in the Syncplicity AD group. I would like for the routine to take the name from the text file and see if Syncplicity is one of the groups the users belongs to. if the user belongs to the group I'd like for the routine to display "xyxyxy" is a member, alternatively to display "xyxyxy" is NOT a member.

the original batch file would do this for one user, I ran the batch file like this "syncme xyxyxy". the batch file would run and return with the message desired. I tested the results of the file extensively and the results were trustworthy. So, the original file looks like this:

@echo off
dsquery user -samid %1 | dsget user -memberof | findstr /c:"Syncplicity" > nul
if %errorlevel% == 0 (
   echo %1 is a member
) ELSE (
   echo %1 is NOT a member
)

Title: Re: if and else routine returns an error
Post by: Salmon Trout on October 26, 2019, 03:33:37 AM
What does this do?

@echo off
setlocal enabledelayedexpansion
for /f %%i in (mgr.txt) do (
   echo testing %%i
   dsquery user -samid %%i | dsget user -memberof | findstr /c:"Syncplicity"
   REM > nul
   echo errorlevel is: !errorlevel!
   if !errorlevel==0 (
      echo %%i is a member
   ) ELSE (
      echo %%i is NOT a member
   )
)


Title: Re: if and else routine returns an error
Post by: Salmon Trout on October 27, 2019, 03:06:51 AM
Fixed fatal typing error! Sorry!

@echo off
setlocal enabledelayedexpansion
for /f %%i in (mgr.txt) do (
   echo testing %%i
   dsquery user -samid %%i | dsget user -memberof | findstr /c:"Syncplicity"
   REM > nul
   echo errorlevel is: !errorlevel!
   if !errorlevel!==0 (
      echo %%i is a member
   ) ELSE (
      echo %%i is NOT a member
   )
)



Title: Re: if and else routine returns an error
Post by: Geek-9pm on October 27, 2019, 01:50:13 PM
wild4bits,
You are getting good advice from Salmon Trout.

Now then, for what it is worth, many users are moving away from pure Batch file use. Instead, they use alternative methods that are easier to kern.

One is Power Shell, now s part of Windows.
Or even VB Script, which is very versatile.
And there are a number of other programs. That  make a claim to fame.
 
I would recommend VB Script .
Quote
VBScript ("Microsoft Visual Basic Scripting Edition") is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. It allows Microsoft Windows system administrators to generate powerful tools for managing computers with error handling, subroutines, and other advanced programming construct
Wikipedia › wiki › VBScript
Here is a introduction:
https://www.guru99.com/introduction-to-vbscript.html
IMHO, using Vb Script is abetter choice for most users.  8)