/build/static/layout/Breadcrumb_cap_w.png

Next Drive Letter?

What's everyone suggestion for finding the next available drive letter in a batch file?

0 Comments   [ + ] Show comments

Answers (10)

Posted by: Robo Scripter 19 years ago
Orange Senior Belt
0
batch file??
NET USE * \\server\share


Here's the help file?

C:\>net use /?
The syntax of this command is:


NET USE [devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]


Regards,
Posted by: usachrisk 19 years ago
Purple Belt
0
Robert,

I considered using Net Use, but after finding out the next drive, I need to run applications from it - and I wouldn't know on the next line of the batch file which drive letter that the previous command (net use) setup for me.

Chris.
Posted by: Robo Scripter 19 years ago
Orange Senior Belt
0
Chris,

I hate to waist the thread point, but I guess this is where instant messaging would help.

Q: Is there some reason we're using a batch instead of a VBS?

I know there is a way but it's been centuries since I've done any batch other than to start a vbs.

Regards,

Yahoo IM ID=robo_scripter@yahoo.com
Posted by: usachrisk 19 years ago
Purple Belt
0
A: Mainly because I haven't learned Visual Basic Scripting yet ;) On a sadder note, I really don't have time to learn it right now with my project plate, can you point me to an example of where I could map the next available drive, execute an application, and then unmap the drive?
Posted by: Robo Scripter 19 years ago
Orange Senior Belt
0
Ok, try this one.
Sorry for the delay I had to make it generic first.

--------------------- code start ---------------------
Dim oDir
Dim oDrives
sPath = "\\Server\SharePath"
xFile = "Application.exe"
Set oDir = CreateObject("WScript.Network")
' Collect Drives already mapped
Set oDrives = oDir.EnumNetworkDrives
'Number of Drives divided by mapped paths
numDrv = oDrives.Count
numDrv = numDrv / 2
'Get Next Drive letter After H
nDrv = Chr(72 + numDrv)
'Map Drive to path
oDir.MapNetworkDrive nDrv & ":", sPath, bUpdate
' Setup executable Path
xPath = nDrv & chr(58) & Chr(92) & xFile
WScript.Echo xPath
Set objShell=WScript.CreateObject("wscript.shell")
'Start executable on share point, wait until application closes;
objshell.run(xPath),1 ,true
'When application closes, unmap drive.
oDir.RemoveNetworkDrive nDrv & chr(58)

-------------------------- code end -------------



Regards,
Posted by: usachrisk 19 years ago
Purple Belt
0
Thanks for the help. It's similar enough to other languages so I'm pretty sure I understand what it's doing. Before trying it out, I logically went through this in my head. Now wouldn't this not work in many cases such as:

You have physical drives A, C, D. You have mapped drives H, I, K, S, T, Z. Now, first, oDrives.Count would be 6, right? Now numDrv, when you divide it by 2 is going to be 3 (6/3=2). Now you take ascii character 72 + 3 and you have 75, which is K - but K is already in use.

Sorry I can't IM, work firewall.
Posted by: guard 19 years ago
Senior Yellow Belt
0
See the ".ListAvail" command at (http://TheSystemGuard.com/MtCmds/ListValue/ListAvail.htm)


This is part of the FREE Advanced Command Library.
Posted by: olivier 19 years ago
Orange Senior Belt
0
set fs = createobject("Scripting.FileSystemObject")
function freedrive
for i = 69 to 90
select case value = fs.DriveExists(Chr(i))
case value = 0
driveletter = chr(i)
exit for
case value = 1
driveletter = ""
end select
next
freedrive = driveletter & ":"

end function

msgbox freedrive
Posted by: guard 19 years ago
Senior Yellow Belt
0
Forgot to mention what .ListAvail does:

Displays all available (unused and unmapped) drive letters, one per line, in alphabetical order. For example:

C:\GuardPost>%.ListAvail%
F
H
J
Z

C:\GuardPost>


To get only the next available drive letter into a variable for your own use.
In a script:

[font="Courier New"]SET NextDrive=
%.ForAll% %%D IN ('%.eListAvail%') DO IF NOT DEFINED NextDrive SET NextDrive=%%D

*******

.ForAll - Place an entire line of output into a FOR variable (%%D)
.eListAvail - "escapified" version of .ListAvail (all cmdsymbols pre-escaped)

Both commands are in the FREE Advanced Command Library.
Posted by: perucho 19 years ago
Yellow Belt
0
This is how you do it using DOS commands:

command line:
FOR /F "tokens=2" %A IN ('NET USE * \\computername\sharename ^| FIND ":"') DO SET v_nextdriveletter=%A

batch program:
FOR /F "tokens=2" %%A IN ('NET USE * \\computername\sharename ^| FIND ":"') DO SET v_nextdriveletter=%%A

If you are using the FOR command at the command line rather than in a batch program, specify %parameter instead of %%parameter.

http://www.ss64.com/nt/for.html

http://www.ss64.com

Let me know if it works for you! [:D]
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ