3

I've been searching all over for an answer and I didn't get anywhere so far.

I wish to do the following:

Scan my LAN for one specific folder (which I know the name), in any computer. This folder is always shared with full access to everyone. When found, map it to an unused drive letter in Windows.

Tried this option from other question in stackoverflow, just to find the folder and host, but no sucess:

@echo off
set folder=example
for /f %%a in ('net view ^|find "\\"') do if not "%%a"=="\\%computername%" (
    echo trying server "%%a"
    pushd "%%a" && (
             for /d /r %%b in (%folder%*) do if /i "%%~nxb"=="%folder%" echo server "%%a" folder "%%b"
             popd
            )
)
pause

Thanks in advance!

  • 1
    So what does net view return for you? Why does the script not work? – Seth Jan 20 '17 at 11:38
  • I tested with a few computers in my LAN with some other shared folders, it just doesn't find anything hehe. I'm pretty hopeless. Net view does find all the computers, including the ones I tried to "test". – Eduardo Costa da Rocha Jan 20 '17 at 11:45
  • Do you have a list of computers on your network? As in a list of all the machines? Otherwise you would have to start with a discovery of hosts that might host something and depending on your setup this is going to be difficult. You would probably have to scan the network first and try to access each host. If that folder is only shared on a single machine it should be easy to get the name of that machine. So whats your actual goal with mapping it? Why would it not be available from the same machine all the time? – Seth Jan 20 '17 at 13:10
  • @Seth I have a software that depends on this specific folder to update it's files. My goal is to accomplish this task in different environments, that follow the same idea: a shared folder with full access to everyone, that I know the name. I'm pretty sure if I can make it work on my own network, I'll be able to replicate in different LANs! So far I can see the computers and it's shared folders are visible to me if I use net commands in the command prompt. But I can't get to my original goal, which is find the folder and map it... – Eduardo Costa da Rocha Jan 20 '17 at 14:30
  • Which net commands? If you do have that specific information available you could possibly parse it. Much like your current script does. – Seth Jan 21 '17 at 6:58
1

Read pushd - UNC Network paths:

When a UNC path is specified, PUSHD will create a temporary drive map and will then use that new drive.
The temporary drive letters are allocated in reverse alphabetical order, so if Z: is free it will be used first.

The pushd "%%a" is incorrect as pushd requires a UNC path like \\computername\sharename or \\computername\sharename\some\subfolder (see also net use) while "%%a" evaluates only to \\computername.

Following script (with some minor enhancements) should do the job. Read Win32_Share class and wmic.exe for reference.

@ECHO OFF >NUL
SETLOCAL EnableExtensions DisableDelayedExpansion
set "folder=example"
for /f %%a in ('net view ^|find "\\"') do if not "%%a"=="\\%computername%" (
    echo server: "%%a"
    set "_server=%%a"
    call :tryServer
)
ENDLOCAL
goto :eof

:tryServer
  for /F "tokens=2 delims=," %%G in ('
    wmic /node:"%_server:~2%" Share where "Status='OK' and Type=0" get name^, type /Format:csv
                                     ') do (
        echo trying "%_server%\%%~G"
        pushd "%_server%\%%~G" && (
            for /d /r %%b in ("%folder%*") do (
                if /i "%%~nxb"=="%folder%" echo FOUND: "%_server%\%%~G" folder "%%b"
            )
            popd
        )
  )
goto :eof

Note that double quotes in for /d /r %%b in ("%folder%*") do … are necessary to correctly treat even a folder name containing space(s) like set "folder=foo bar".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.