Jump to content

[Help] NET USE Command


Asin

Recommended Posts

I'm trying to figure out how to get the next available drive letter.

I know that if I use the command:

net use * \\computername\sharename *

it will map to the next available drive letter. In this case, Y:\. How do know that if I'm trying to automate a batch script though?

Any help is greatly appreciated.

Edited by Sonic
Link to comment
Share on other sites


I'm trying to set up a script for others to use to install programs. I'm not fully aware of all that happens in this IT Department just yet since I've only been working here for a month.

There may be reserved drive letters or random changes that I have no control over nor know about in the future. I'm only going to be around for 3 more months so I wanted to make something that is somewhat universal.

The name of the computer isn't going to change after I leave. In my VMware tests, scripts seem a little more responsive if you use a constant drive letter as opposed to the network path.

Link to comment
Share on other sites

Using a drive letter vs. UNC shouldn't be any faster or slower. If you want something that is going to be universal and doesn't interfere with other already mapped network shares then just stick with the UNC.

If you stick with mapping the drive you should add /PERSISTENT:NO to the NET USE command so the drive doesn't automatically remap the next time they logon.

Link to comment
Share on other sites

speaking from experience here, it is a lot better to ensure that all users have the same drive letter for the same shares

that way when somebody rings up and says "i cant see f: drive!" or "i havent got access to XYZ in z: drive) you will be able to fix the problem much quicker

just my 2 cents :)

Link to comment
Share on other sites

Using a drive letter vs. UNC shouldn't be any faster or slower. If you want something that is going to be universal and doesn't interfere with other already mapped network shares then just stick with the UNC.

Sorry, when I said more responsive, I meant actually working. :P XCOPY tends to die on me when I try to copy all relative files (i.e. ".\*.*") from an Everyone Full Control network share. When I say die, I mean that nothing is copied. It keeps saying "0 file(s) copied".

speaking from experience here, it is a lot better to ensure that all users have the same drive letter for the same shares

that way when somebody rings up and says "i cant see f: drive!" or "i havent got access to XYZ in z: drive) you will be able to fix the problem much quicker

I think that I got into a bit of this above. I don't have a full listing for everyone's possibly network drives upon logon. As a result, I'm just trying to do this so that anyone can use it.

I'm running this in a batch to connect the share to the last free drive letter and write it to %drvlet%
FOR /f "usebackq tokens=2* eol=" %%i IN (`net use * \\server\share /PERSISTENT:NO`) DO (
SET drvlet=%%i
GOTO endfor
)
:endfor

This looks like it's exactly what I need. I'm not too familiar with the FOR loop command nor conditionals in DOS. What exactly is the exit condition in this code?

I know what it does since you typed that up in response to what I'm asking for. But can you or somebody else just explain it a little more?

Link to comment
Share on other sites

Hi Asin,

here a little explanation to the for loop ( i hope it is not to confusing ):

The command "net use * \\server\share /PERSISTENT:NO" connects the share to last free drive letter

The for loop would process every line of the output of the command, if there wouldn't be the goto :endfor

But because of the goto :endfor, only the first line, which already contains the drive letter is processed.

The usebackq options just changes the use of ' and ". The tokens=2* says that the second word is written to the variable %%i, the * within says, that the rest is written to variable %%j.

I hope this makes the batch a little more clear, I you've got any questions left, don't be shy to ask again

Link to comment
Share on other sites

  • 2 years later...

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