Next Drive Letter?
What's everyone suggestion for finding the next available drive letter in a batch file?
0 Comments
[ + ] Show comments
Answers (10)
Please log in to answer
Posted by:
Robo Scripter
20 years ago
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,
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
20 years ago
Posted by:
Robo Scripter
20 years ago
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
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
20 years ago
Posted by:
Robo Scripter
20 years ago
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,
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
20 years ago
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.
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
20 years ago
Posted by:
olivier
20 years ago
Posted by:
guard
20 years ago
Forgot to mention what .ListAvail does:
Displays all available (unused and unmapped) drive letters, one per line, in alphabetical order. For example:
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 ('%.e ListAvail%') DO IF NOT DEFINED NextDrive SET NextDrive=%%D
*******
.ForAll - Place an entire line of output into a FOR variable (%%D)
.e ListAvail - "e scapified" version of .ListAvail (all cmdsymbols pre-escaped)
Both commands are in the FREE Advanced Command Library.
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:
%.ForAll% %%D IN ('%.
*******
.ForAll - Place an entire line of output into a FOR variable (%%D)
.
Both commands are in the FREE Advanced Command Library.
Posted by:
perucho
20 years ago
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]
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.
so that the conversation will remain readable.