Help - Search - Members - Calendar
Full Version: Need help with a small batch script
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
OffHand
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
Scr1ptW1zard
Try this:

CODE
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"


jaclaz
You may find some source of inspiration (or peruse my code newwink.gif ) in the freedrv2.cmd script.

Originally posted here:
http://www.911cd.net/forums//index.php?showtopic=21965
http://www.911cd.net/forums//index.php?sho...=21965&st=6

jaclaz
OffHand
QUOTE (Scr1ptW1zard @ Sep 1 2008, 04:57 PM) *
Try this:

CODE
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.

CODE
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"
OffHand
QUOTE (jaclaz @ Sep 1 2008, 05:07 PM) *
You may find some source of inspiration (or peruse my code newwink.gif ) in the freedrv2.cmd script.

Originally posted here:
http://www.911cd.net/forums//index.php?showtopic=21965
http://www.911cd.net/forums//index.php?sho...=21965&st=6

jaclaz

Thank you! I will check them out!
Yzöwl
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.
OffHand
QUOTE (Yzöwl @ Sep 1 2008, 07:55 PM) *
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?
Yzöwl
QUOTE (OffHand @ Sep 1 2008, 10:56 PM) *
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!
QUOTE (OffHand @ Sep 1 2008, 03:42 PM) *
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!
OffHand
QUOTE (Yzöwl @ Sep 2 2008, 07:07 AM) *
QUOTE (OffHand @ Sep 1 2008, 10:56 PM) *
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!
QUOTE (OffHand @ Sep 1 2008, 03:42 PM) *
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 smile.gif
jaclaz
...and the code in mine does newwink.gif

This is the relevant snippet:
CODE
:: 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
gunsmokingman
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

QUOTE
CODE
'-> 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

OffHand
QUOTE (gunsmokingman @ Sep 2 2008, 05:15 PM) *
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

QUOTE
CODE
'-> 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 smile.gif 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
Yzöwl
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!
CODE
@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_%"
twig123
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 ?
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.