wmic logicaldisk get DeviceID
This lists all used drive letters, so you can determine which are free.
From within Diskpart,
List Volume will also list all used drive letters. It won't work from the command line, so you would have to create a Diskpart script, run it and capture the output.
You'd have to do the same to determine which disk has unpartitioned space as well, so you know which disk to select to create the partition on.
Then create a Diskpart script to create the partition and assign a drive letter using the
Assign command.
Then you can exit Diskpart and format the drive.
While you can use
Net Use to map to a share that exists on the same PC, you can't use
Net Use for this situation, as you would first have to create a share to map to, which requires knowing the drive letter so you can specify the path using
Net Share.
As for the drive mapping routine, glad you asked. Going though it again I found a few of bugs, and an unneeded variable (it would always be null at that point so wasn't needed).
If no username or password are passed, the variables will not be initialized, so the routine could use a pre-existing value for the _User and _Pass variables which could cause an error.
There was no way to prompt for the password and specify using the current user by using the * rather than typing the full user name.
The loop that checks the length should start with 4, not 5. Might be able to end the loop on 25 instead of 26, but I didn't test that.
This version clears some variables first,
then checks to see if a user (and password) were passed. If %4 is null, then neither were passed, so it now jumps over the check for the fifth parameter.
As the usage section shows, there are three variables that
must be passed to the routine:
- the path to be mapped
- the names of two variables, one to return the drive letter, and a flag variable:
0 if the map already existed, 1 if a new map was created
The 4th and 5th values are optional.
The Username will be Parameter 4 (%4), and the Password parameter 5 (%5)
So we first check to see if the username was supplied (%4). If a username is supplied, it creates the
/user: switch to be used latter. It then checks to see if a password was supplied.
Specifying * for the username means you will use the currently logged on user, and you can now specify * for the password so you will be prompted for the password. This could be useful in a work setting if the system is left unlocked, preventing anyone from running a script that uses this routine unless they know the password of the current user.
Specifying * for the password means you will be prompted for the password.
The Net Use command output is 4 columns: Status, Drive Letter, Remote Path, and Network Type. The Remote Path column is 25 characters wide, followed by a space, then
Microsoft Windows Network. Paths shorter than 25 characters are padded with spaces. If the Remote path is 26 characters or longer, the Network Type column is put on the next line. The Remote Path will still have one space added to the end though.
So we check the position of the last character of the share name we are looking for. The loop starts with 4 because the shortest share name is 5 characters (\\A\B). Remember that the first character is Position 0. If the last character is Position 24 or less (length of 25 or less), the name gets padded with spaces, then the first 25 characters are extracted.
We then add a single space and a period, then the period is removed. The period is used just to make it easier to see that a space is being added, and to make sure the space doesn't get eliminated in a copy/paste.
We then do a Net Use command, pipe it to find to eliminate any shares that do not conatin the name we are looking for, then remove the Microsoft Windows Network from the end if present.
The If statement then checks to see if we have an exact match.
This is needed because the name we are looking for could be a parent folder to an existing share. Example, if we want to map to
\\Computer1\Backups and also have maps to these:
\\Computer1\Backups\Monday
\\Computer1\Backups\Tuesday
Find will find them, so we need to do a check for the exact name.
If a match is found,
_PDrv is set to the Drive letter.
If
_Pdrv has a value after the loop completes, the routine is exited, setting the 2nd parameter to the drive letter and the 3rd parameter to the Flag value which is 0
If
_PDrv does not have a value, then
_Flag is set to 1, and a
Net Use command is executed to map the share to the next available letter.
If this completes without an error, we jump back to the For loop to find the Drive Letter that was assigned. You could also capture the output of the Net Use command using a For loop and parse the output for the
Drive X: is now connected to... part to get the Drive letter, but this wouldn't let you return the Errorlevel to the caller if there was an error without adding a bunch of extra checking.
If there is an error, it simply returns. The Errorlevel can then be checked to see if an error occured. For example, Errorlevel will be 2 if the Path can't be found (System Error 67).