Jump to content

Need help with a small batch script


Recommended Posts

Hi guys,

Is there a way to check if there is a network drive connected that uses a specific letter (Y: in my case)?

In order for my script to run properly it needs to mount a network drive with the letter Y:

What I have now works, but it displays an error message if the drive is not mounted.

ECHO The Y: drive needs to be disconnected in order for this script to run

pause

net use Y: /delete

ECHO Mapping network drive...

net use Y: "\\it-server\software\winsoft"

Now what I would like is:

command that checks if the drive is in use...

if Y: is in use

ECHO Y: Needs to be disconnected... Disconnecting drive now.

net use Y: /delete

if Y: is not in use

ECHO Mapping network drive...

net use Y: "\\it-server\software\winsoft"

Your help is greatly appreciated! Thanks in advance...

Martijn

Link to comment
Share on other sites


Try this:

if exist Y: (
ECHO Y: Needs to be disconnected... Disconnecting drive now.
net use Y: /delete
)
ECHO Mapping network drive...
net use Y: "\\it-server\software\winsoft"

Thanks a lot! Works great! All I had to add was a small pause so users can read the feedback.

if exist Y: (
ECHO Y: Needs to be disconnected... Disconnecting drive now.
ping -n 5 127.0.0.1>nul && net use Y: /delete
)
ECHO Mapping network drive...
net use Y: "\\it-server\software\winsoft"

Link to comment
Share on other sites

Just a small word of warning:

The batch script example provided above doesn't differentiate between types of drive; drive E: may exist, but may not be assigned to another network drive.

Wouldn't the net use command just fail without doing damage?

Link to comment
Share on other sites

Wouldn't the net use command just fail without doing damage?
It wouldn't cause damage, it would fail twice whereas your original attempt was a problem if it failed once!
Is there a way to check if there is a network drive connected that uses a specific letter (Y: in my case)?

In order for my script to run properly it needs to mount a network drive with the letter Y:

What I have now works, but it displays an error message if the drive is not mounted.

Your request was to check if a network drive was connected to Y: all I did was warn you that the script didn't do this!
Link to comment
Share on other sites

Wouldn't the net use command just fail without doing damage?
It wouldn't cause damage, it would fail twice whereas your original attempt was a problem if it failed once!
Is there a way to check if there is a network drive connected that uses a specific letter (Y: in my case)?

In order for my script to run properly it needs to mount a network drive with the letter Y:

What I have now works, but it displays an error message if the drive is not mounted.

Your request was to check if a network drive was connected to Y: all I did was warn you that the script didn't do this!

Aite... thanks :)

Link to comment
Share on other sites

...and the code in mine does ;)

This is the relevant snippet:

:: List drive letters for network drives
FOR /F "tokens=2" %%A IN ('NET USE ^|FINDSTR /R /C:" [A-Z]: "') DO SET BUSYDRV=!BUSYDRV!,%%A
:: Remove leading comma
SET BUSYDRV=%BUSYDRV:~1%
:: Remove backslashes
SET BUSYDRV=%BUSYDRV:\=%

jaclaz

Link to comment
Share on other sites

If you want to try a VBS script this will check to see if there is a Network drive = Y

I have no network to test this on, I only made sure there was no run time errors.

If this works I can add the code to do what you requested.

Save As Chk_NetDrive_Y.vbs

'-> Varibles
Dim Drv, Obj, NetDisk, NetPath, Path, StrComputer :StrComputer = "."
'-> Varibles As Objects
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:\\" & StrComputer & "\root\cimv2")
'-> Wmi Querry For Network Drives
Set NetDisk = Wmi.ExecQuery("Select * From Win32_LogicalDisk Where DriveType = 4")
'-> Loop To Look To See If There Are Any Map Drives
For Each Obj in NetDisk
If InStr(LCase(Obj.DeviceID),LCase("y")) Then
Drv = False
WScript.Echo "Confirm Network Drive Y"
NetPath = Obj.DeviceID & Obj.ProviderName
Else
Drv = True
Path = Path & Obj.DeviceID & Obj.ProviderName & vbCrLf
End If
Next
'-> If There Is No Network Drives Mapped
If NetDisk.Count = 0 Then
WScript.Echo "Missing Network Drive Y"
Else
'-> Show List Of Network Drives
If Drv = True Then
WScript.Echo "List Of Network Drives" & vbCrLf & NetPath
'-> Loop To Read The Individual Drives
' NetPath = Split(Path, vbCrLf)
' For Each Obj In NetPath
' Path = Split(Obj,":")
' WScript.Echo Path(0) & ":" & Chr(34) & Path(1) & Chr(34)
' Next
End If
End If

Link to comment
Share on other sites

If you want to try a VBS script this will check to see if there is a Network drive = Y

I have no network to test this on, I only made sure there was no run time errors.

If this works I can add the code to do what you requested.

Save As Chk_NetDrive_Y.vbs

'-> Varibles
Dim Drv, Obj, NetDisk, NetPath, Path, StrComputer :StrComputer = "."
'-> Varibles As Objects
Dim Act :Set Act = CreateObject("Wscript.Shell")
Dim Wmi :Set Wmi = GetObject("winmgmts:\\" & StrComputer & "\root\cimv2")
'-> Wmi Querry For Network Drives
Set NetDisk = Wmi.ExecQuery("Select * From Win32_LogicalDisk Where DriveType = 4")
'-> Loop To Look To See If There Are Any Map Drives
For Each Obj in NetDisk
If InStr(LCase(Obj.DeviceID),LCase("y")) Then
Drv = False
WScript.Echo "Confirm Network Drive Y"
NetPath = Obj.DeviceID & Obj.ProviderName
Else
Drv = True
Path = Path & Obj.DeviceID & Obj.ProviderName & vbCrLf
End If
Next
'-> If There Is No Network Drives Mapped
If NetDisk.Count = 0 Then
WScript.Echo "Missing Network Drive Y"
Else
'-> Show List Of Network Drives
If Drv = True Then
WScript.Echo "List Of Network Drives" & vbCrLf & NetPath
'-> Loop To Read The Individual Drives
' NetPath = Split(Path, vbCrLf)
' For Each Obj In NetPath
' Path = Split(Obj,":")
' WScript.Echo Path(0) & ":" & Chr(34) & Path(1) & Chr(34)
' Next
End If
End If

Thanks everybody who replied and helped... it is really appreciated :) I am happy with what I got now. The chances that Y is not a network drive are very slim... pretty much zero, so it's not really necessary to check if it actually is a network drive.

Peace,

Martijn

Link to comment
Share on other sites

You should ideally still be making some checks etc. in your script:

Here's a commented batch file to show you the kind of thought processes I'd make as part of this task. The file still isn't fully error trapped nor tested , but it should give you something to work with!

@Echo off
Setlocal enableextensions

:: Your UNC Path
(Set S_=\\Server\Share)

:: Your Intended Drive Letter
(Set D_=Y:)

:: String informing user impending mapping operation
(Set M_=Mapping network drive ...)

:: Check UNC Path validity
If Not Exist "%S_%" (
Echo: Your UNC Path cannot be found
Goto EndIt)

:: Check to see if D_ is already mapped
For /f "tokens 3*" %%# In ('2^>Nul Net use %D_%^|Find "\\"') Do (Set _=%%#)

:: Unmapped - Map it and finish
If Not Defined _ (
Call :Mapit
Goto EndIt)

:: Mapped - steps follow below

:: Finish whilst already mapped to intended letter
If /I "%_%" Equ "%S_%" (
Echo: %D_% is already mapped to %S_%
Goto EndIt)

:: Disconnect the required letters currently allocated share
Echo: Required drive %D_% will be disconnected
Net use %D_% /delete

:: Map the intended share to required letter
Call :MapIt

:: Map the users original share to the next available letter and finish
Echo: The mapped share %_%
Echo: will now be re-mapped to the next available device
:: Include additional switches as necessary
Net use * "%_%"

:EndIt
Echo:
Echo: Press any key to exit...
Pause>Nul
Goto :Eof

:MapIt
Echo:
Echo: %M_%
:: Include additional switches as necessary
Net use %D_% "%S_%"

Link to comment
Share on other sites

Does the drive have to specifically be Y: ?

If not, would it be easier to just grab user input as to what drive to map?

set /p usrnput="Enter a free drive letter to map (EX: E, F, G): "

and then just use %usrnput% for your coding ?

Edited by twig123
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...