Ian Laird
Ian Laird

Reputation: 17

Mapping Drive to an unknown share name?

I'm trying to create a batch file that can look through contents of network shares to find a specific named directory and subdirectory and then map a drive to the share that contains these. Problem being that, whilst the contents of the share have these folders in them to identify that this is the correct share, the share name itself may be different in different setups, so I can't just use the share name in the script.

In other words

\\server\share\sims\setups

the sims and setups directories will ALWAYS exist in the share, but the share name is the thing that can be different. The server name will already exist in the batch file as it is set by using the set /p command to prompt the user for the servers ip.

i have looked at the find and the forfiles commands but these seem to only work to search locally and not on network paths.

Thanks

Upvotes: 1

Views: 256

Answers (1)

KennyBOT
KennyBOT

Reputation: 447

As indicated by foxidrive you can use the net view command to list shares of a server.

The for command is able to iterate over output from commands. Combining these in the following rough commandline does map a drive for me if a certain folder/file is present.

for /F %a in ('net view \\srv') do if exist "\\srv\%a\sims\setup" net use z: \\srv\%a

(don't forget to change %a to %%a if you use this line in a command-script)

The net view does output some noise but that doesn't screw the result. You could add some extra handling to only handle shares after the '-----' line is received.

How does this work?

net view \\server outputs the following:

Shared resources at \\server

Awesome server

Share name     Type  Used as  Comment

-----------------------------------------------------------------------------
Configuration  Disk           System Configuration
Download       Disk           Download Share (Full Access)
Public         Disk           Public Share (Full Access)
Blah           Disk           Blah share (Full Access)
The command completed successfully.

The for /f command parses each line up to the next space. From the FOR /?

FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]

...you can use the FOR /F command to parse the output of a command. You do this by making the file-set between the parenthesis a back quoted string. It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file.

Using that technique would echo the following result if run on net view \\server:

Shared
Network
Share
-----------------------------------------------------------------------------
Configuration
Download
Public
Blah
The

The EXIST operator checks if a file/directory exists. By concatenating the servername, share and the expected folder you can find out if that folder does excist on that share.

As I said the first 4 lines and the last line are noise and should be ignored but they don't hurt too much in this scenario.

Upvotes: 1

Related Questions