Next available drive letter batch code explanation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
lferrante1
Posts: 2
Joined: 17 Aug 2015 17:42

Next available drive letter batch code explanation

#1 Post by lferrante1 » 17 Aug 2015 17:55

First post, long time DOS fan. I don't have a programmer's mind but have used the command line for years.

I'm here to ask if someone can explain in very simple terms, character by character, what this code does. Many years ago I asked a co-worker to wrote some batch code that would grab the next free drive letter attached to a fixed/specified network share. The guy who did it was a 20+ year coding vet who wrote masterpiece financial applications.

I'm sure this code would be appreciated by many who work in IT and who would probably be amazed at the simplicity of it. I've dug around for other ways to do this, and people often come up with convoluted multiline code.

However, I don't understand what is going on. I thought he explained once that it just looked at what was on the screen after a certain number of characters based on the result of the wildcard in NET USE and stored the value in the userstatedir var. But as I said, he never clearly explained it and I need a step by step child's explanation.

Code: Select all

FOR /f "eol=; tokens=1,2* delims= " %%a IN ('NET USE * "\\server\share" ') DO SET userstatedr_%%a=%%b


Thanks all,

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Next available drive letter batch code explanation

#2 Post by ShadowThief » 17 Aug 2015 18:32

Code: Select all

FOR /f "eol=; tokens=1,2* delims= " %%a IN ('NET USE * "\\server\share" ') DO SET userstatedr_%%a=%%b

This whole thing takes the output of NET USE * \\server\share and creates new variables that include the drive letter, which is then set to the network path.

Code: Select all

FOR /f

The /f flag for for loops is - in my opinion - the most powerful command in batch. If the bit inside the parentheses is not wrapped in quotes, the for loop treats it like a file and processes it line by line. If it is in single quotes, the for loop treats it like a console command and processes the output of the command line by line. If it is in double quotes, the for loop treats it like a string.

Code: Select all

eol=;

This says that ; is the very last character on any given line, and that the for loop should ignore anything on that line that comes after it

Code: Select all

tokens=1,2*

This splits the output into two variables, one containing all data on the left side of the delimiter and the other containing all data on the right side of the delimiter.

Code: Select all

delims= 

This tells the for loop to split up the output on spaces. When combined with the tokens option, this makes the first variable everything to the left of the first space, and the second variable everything to the right of the first space. If the delims option is skipped, all valid whitespace characters (spaces, tabs, and newline characters) are considered delimiters.

Code: Select all

%%a

This is the variable that the for loop is using to store the data that gets split on the delimiters. It will increment by letter based on how many tokens are declared, so %%a is the first token and %%b is the second token. It's worth noting that you can only use single letters for for loop variables, and that this is pretty much the only time that variable names are case-sensitive in batch.

Code: Select all

'NET USE * "\\server\share" '

Single quotes tell the for loop to process the output of the command, in this case, NET USE * \\server\share

Code: Select all

NET USE * "\\server\share

net use maps a network location to a local drive
* indicates that the next available drive letter should be used
\\server\share is the network location that should be mapped

Code: Select all

SET userstatedr_%%a=%%b

I don't have any network drives attached to my computer, so I can only guess what the output of a net use command looks like, but I imagine it's something along the lines of
DriveLetter: \\network\path
which (in this case) would make this command set a variable called userstatedr_DriveLetter: and set it equal to \\network\path

lferrante1
Posts: 2
Joined: 17 Aug 2015 17:42

Re: Next available drive letter batch code explanation

#3 Post by lferrante1 » 17 Aug 2015 21:06

Thank you for breaking it down. It'll have to digest.

How does it actually grab the result from the onscreen text ? Is the FOR code actually intercepting the value returned? Or does it just count 6 character spaces starting from the left side of the screen? I still don't understand how it "grabs" the value returned. Are the "1,2" values the "Z" and ":" (colon) ?

Image

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Next available drive letter batch code explanation

#4 Post by foxidrive » 18 Aug 2015 03:43

The code you showed creates more than one variable (one is useless) and is more convoluted than it needs to be.
Test this to see how it works: if the share fails then the variable will be empty.

Code: Select all

@echo off
set "sharedrv="
FOR /f "tokens=2" %%a IN ('NET USE * "\\server\share" ^|find "now connected" ') DO SET "sharedrv=%%a"
echo "%sharedrv%"
pause

Post Reply