mercredi 24 mai 2017

Batch if exist always returns false

Never wrote a batch script before so I'm in the learning process but...

I'm trying to create a script to automatically map network drives. I included an if exist statement to determine if the folder exists. But it keeps exiting the if statement as if the folder doesn't exist even when it does. When I run the command separately it works (e.g. if exist \\domain\folder\desired_folder (echo true) else (echo false) will echo true). Seems like it passes the if statements and goes straight to the else statement

Here is my code that I'm having issues with, it should be going into the first if statement and mapping the drive... Any help would be appreciated.

REM check if I drive folder exists
if exist "\\domain\folder\%%A_%%B" (
REM delete current I drive
REM map I drive
net use i: /delete
net use i: \\domain\folder\%drivename% /P:YES
) else if exist \\domain\folder\%USERNAME% (
    net use i: /delete
    net use i: \\domain\folder\%USERNAME% /P:YES
) else (
    echo ERROR: File Not Found
    echo ERROR: Unable to map I drive

    REM for debugging purposes: %username% & %drivename% echo the desired result
    echo username %USERNAME%
    echo a_b %drivename%
    pause
    exit
)

Aucun commentaire:

Enregistrer un commentaire