mercredi 19 juin 2019

If / Else in Batch file isn't working as expected

I'm created a batch file to record permissions on a user home drive. In our environment, we have a user account from each domain applied to the user profile. My code is as follows..

    For /f "tokens=1,2" %%c in ('cacls %USERPROFILEPATH% ^| findstr /i "%LOGGEDONUSER%"') do (
        echo %%d
        echo %%c
        IF NOT "%%D"=="" (
        echo %datetime%: Do File Path Permissions is %%d 
        ) Else (
        echo %datetime%: Else File Path Permissions is %%c
        )

)

With the nature of cacls and how it reports back, I run the cacls command on it it's own, I get..

D:\Users\Steve DomainA\SteveAccount
               DomainB\SteveAccount

I don't want the D:\Users\Steve token, just the account names tokens. Since the return from the For command contains two lines, it goes through two loops. From the echo replies in the script, I get..

DomainA\SteveAccount
D:\Users\Steve
Echo is off
DomainB\SteveAccount

This what I expect because %%d is null on the second loop.

The problem I have is that even though %%d is now Null is never goes into the ELSE part of the IF NOT statement

19/06/2019 17:19: Checking file permissions 
19/06/2019 17:19: File Path is D:\Users\SteveAccount 
DomainA\SteveAccount
D:\Users\SteveAccount 
19/06/2019 17:19: Do File Path Permissions is DomainA\SteveAccount 
ECHO is off.
DomainB\SteveAccount
19/06/2019 17:19: Do File Path Permissions is

So,even though the Echo shows %%d as being empty, it still enters the IF statement as if %%d still has a value. I've tried doing set d= to clear the variable after the first loop, but it still enters the Do File Path Permissions statement.

What have I missed?

Steve

Aucun commentaire:

Enregistrer un commentaire