TLCONE
TLCONE

Reputation: 104

Why is this Windows Batch script not able to find the program net.exe?

I'm creating what was meant to be a very simple batch script to map a drive based on user input and check that the drive doesn't already exist first.

@echo off
:EnterInfo
set /P path=Please Enter The Path You Want To Map(EG: \\server\folder)          
set /P z=Please Chose A drive Letter To Map.(EG Z)  
goto :CheckExist

:CheckExist
%z%:\
pause
if exist %z%:\ (
    set /P Sure=A Drive Is Already Using Drive Letter %path%:\ Are You Sure You Want To Replace It?[Y/N]
    if /I "%Sure%" EQU "Y" goto :SetDrive
    if /I "%Sure%" EQU "N" exit)
goto :SetDrive

:SetDrive
C:
dir
net use z: "%path%"
pause

Here is the code so far. I'm getting an error:'net' is not recognized as an internal or external command, when the script gets to the(Net use Z: "%path%") Although the input to the command would work if i were to run it in a cmd window.

I've checked the location the script and it's running from C:\Users\%username%\ like a cmd box would default it's location too. I'm really confused as to why it's not recognising net Use. Thanks in advance

Upvotes: 1

Views: 407

Answers (1)

Govind Parmar
Govind Parmar

Reputation: 21562

The problem is this line:

set /P path=Please Enter The Path You Want To Map(EG: \\server\folder)          

The PATH environment variable is already used by Windows. It tells the command interpreter where to find programs like net.exe, which is in your sys(tem32|wow64) folder. You can't just overwrite it as you please and expect everything to work properly afterward. You set the variable PATH to be the network path for the drive you want the user to map, which overwrites the actual PATH variable.

Solution: Use a different variable name, like, say, mypath.

Upvotes: 2

Related Questions