OffHand Posted September 1, 2008 Share Posted September 1, 2008 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 runpausenet use Y: /deleteECHO 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: /deleteif Y: is not in useECHO 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 More sharing options...
Scr1ptW1zard Posted September 1, 2008 Share Posted September 1, 2008 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" Link to comment Share on other sites More sharing options...
jaclaz Posted September 1, 2008 Share Posted September 1, 2008 You may find some source of inspiration (or peruse my code ) in the freedrv2.cmd script.Originally posted here:http://www.911cd.net/forums//index.php?showtopic=21965http://www.911cd.net/forums//index.php?sho...=21965&st=6jaclazFreedrv.zip Link to comment Share on other sites More sharing options...
OffHand Posted September 1, 2008 Author Share Posted September 1, 2008 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 More sharing options...
OffHand Posted September 1, 2008 Author Share Posted September 1, 2008 You may find some source of inspiration (or peruse my code ) in the freedrv2.cmd script.Originally posted here:http://www.911cd.net/forums//index.php?showtopic=21965http://www.911cd.net/forums//index.php?sho...=21965&st=6jaclazThank you! I will check them out! Link to comment Share on other sites More sharing options...
Yzöwl Posted September 1, 2008 Share Posted September 1, 2008 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. Link to comment Share on other sites More sharing options...
OffHand Posted September 1, 2008 Author Share Posted September 1, 2008 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 More sharing options...
Yzöwl Posted September 2, 2008 Share Posted September 2, 2008 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 More sharing options...
OffHand Posted September 2, 2008 Author Share Posted September 2, 2008 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 More sharing options...
jaclaz Posted September 2, 2008 Share Posted September 2, 2008 ...and the code in mine does This is the relevant snippet::: List drive letters for network drivesFOR /F "tokens=2" %%A IN ('NET USE ^|FINDSTR /R /C:" [A-Z]: "') DO SET BUSYDRV=!BUSYDRV!,%%A:: Remove leading commaSET BUSYDRV=%BUSYDRV:~1%:: Remove backslashesSET BUSYDRV=%BUSYDRV:\=%jaclaz Link to comment Share on other sites More sharing options...
gunsmokingman Posted September 2, 2008 Share Posted September 2, 2008 If you want to try a VBS script this will check to see if there is a Network drive = YI 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 More sharing options...
OffHand Posted September 2, 2008 Author Share Posted September 2, 2008 If you want to try a VBS script this will check to see if there is a Network drive = YI 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 IfThanks 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 More sharing options...
Yzöwl Posted September 2, 2008 Share Posted September 2, 2008 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 offSetlocal 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 validityIf Not Exist "%S_%" ( Echo: Your UNC Path cannot be found Goto EndIt):: Check to see if D_ is already mappedFor /f "tokens 3*" %%# In ('2^>Nul Net use %D_%^|Find "\\"') Do (Set _=%%#):: Unmapped - Map it and finishIf Not Defined _ ( Call :Mapit Goto EndIt):: Mapped - steps follow below:: Finish whilst already mapped to intended letterIf /I "%_%" Equ "%S_%" ( Echo: %D_% is already mapped to %S_% Goto EndIt):: Disconnect the required letters currently allocated shareEcho: Required drive %D_% will be disconnectedNet use %D_% /delete:: Map the intended share to required letterCall :MapIt:: Map the users original share to the next available letter and finishEcho: The mapped share %_%Echo: will now be re-mapped to the next available device:: Include additional switches as necessaryNet use * "%_%":EndItEcho:Echo: Press any key to exit...Pause>NulGoto :Eof:MapItEcho:Echo: %M_%:: Include additional switches as necessaryNet use %D_% "%S_%" Link to comment Share on other sites More sharing options...
twig123 Posted September 4, 2008 Share Posted September 4, 2008 (edited) 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 September 4, 2008 by twig123 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now