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.
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.
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
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.
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.
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.
Single quotes tell the for loop to process the output of the command, in this case, 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
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