4

I was wondering if it was possible using a batch file to find a specific folder in any mapped drive of a PC and have it report the folders full UNC path. Lets say a PC has drives mapped of N:\ & S:\ (or any usual network drive letter for that matter). The folder could be in either folder and I run some code to find where the folder is, it will be in the root of the drive share (avoiding need for recursive subfolder scanning) e.g. N:\Special_Folder however, the end result of the code will report the FULL UNC e.g.

Folder Found!..

\server-vm01\data\shared_area\special_folder

Hope that makes sense. I have tried with some code I already use to identify a USB stick, but am having difficulty porting it to the needs above. Any pointers would be great, thank you.

for %%a in (d: e: f: g: h: i: j: k: l: m: n: o: p: q: r: s: t: u: v: w: x: y: z:) do | find "special_folder" >nul && set unc=%%a:
echo %unc%
pause

edit for troubleshoot, output net use

Microsoft Windows [Version 10.0.14393] (c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\Admin>net use New connections will be remembered.

Status       Local     Remote                    Network

-------------------------------------------------------------------------------
OK           Y:        \\DISKSTATION\NetBackup   Microsoft Windows Network
OK           Z:        \\DISKSTATION\NetBackup   Microsoft Windows Network
The command completed successfully.


C:\Users\Admin>

I tried to fix and I got as far as trying FOR /F "tokens=2-3 delims= " in the first line and that removes the Microsoft Windows Networ but then drops a letter out the path e.g \DISKSTATION\NetBacku\specialfolder

also If I run as admin, as will need to, the code doesn't echo any path, does non admin ? Thanks

EDIT (final)

Thank you both for these really helpful codes. I shall edit the original question with what works, both do as it happens, but I can only tick one solution by the looks of it.

Test Environment:-

Win10 (UAC is ON)

Win7 (UAC is off)

Two Mapped Drives in each with a test Folder called Daphne in root of just one of the 2 drives.

    @echo off
    FOR /F "skip=2 delims=" %%G IN ('"wmic logicaldisk where drivetype=4 get name,providername /format:csv"') do (
       FOR /F "tokens=2,3 delims=," %%H IN ("%%G") DO (
           IF EXIST "%%I\Daphne\" echo %%I\Daphne
        )
     )
pause

The above works perfectly on the Win7 (UAC is off) test system, Admin or not. Works as non Admin Win10 (UAC is ON), doesn't work running as "Admin" though.

Below worked everywhere regardless of admin or not, in both environments.

The original code suggested works fine (FYI) but I just needed it to output the path with no quotations or additional texts, so I am just adding the authors generous efforts (tweaked slightly) to conclude the original question in line with my original intentions.

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

set "_FolderToSearch=Daphne"                  
for /F "delims=" %%D in ('reg query "HKCU\Network"') do (
  for /F "tokens=1,2,*" %%f in ('reg query "%%~D" /v RemotePath ^| find /I "RemotePath"') do (
    if exist "%%~h\%_FolderToSearch%\" echo %%~h\%_FolderToSearch%
  )
)
pause

Below is the output on the screen from both code snippets in their working forms:-

\\DISKSTATION\NetBackup\Daphne
Press any key to continue . . .

\\DISKSTATION\NetBackup\Daphne
Press any key to continue . . .

I can now easily set this as a path statement in a config file to completely automate client software installations by having the customer just tell me the folder name of where they want our data on their network. I shall test first and report back.

Thankyou!

Tika9o9
  • 405
  • 4
  • 22
  • 1
    If they are mapped drives you are searching then use the NET USE command. – Squashman Oct 05 '16 at 18:16
  • I am familiar with that but not sure how to incorporate into a find folder routine? Thanks – Tika9o9 Oct 05 '16 at 18:26
  • So they changed the output for Windows 10. – Squashman Oct 05 '16 at 23:32
  • So you can now see how things can spiral out of control when you don't provide enough details up front about your problem. – Squashman Oct 06 '16 at 12:38
  • Problems only became apparent when trying to execute the code though. If I knew UAC blocked net use commands I would have happily stated something along the lines as such right back at the start. Thanks again. – Tika9o9 Oct 06 '16 at 16:08

2 Answers2

3

Mapped drives are not available from an elevated prompt when UAC is configured to "Prompt for credentials" in Windows.

Next code snippet should work even from an elevated command prompt:

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

set "_FolderToSearch=models"                  folder name to be searched for

echo no recursion: folder in the root of the drive share:

for /F "delims=" %%D in ('reg query "HKCU\Network"') do (
  for /F "tokens=1,2,*" %%f in ('reg query "%%~D" /v RemotePath ^| find /I "RemotePath"') do (
    if exist "%%~h\%_FolderToSearch%\" echo # FOUND [%%~nD:] "%%~h\%_FolderToSearch%"
  )
)

echo recursive search:

for /F "delims=" %%D in ('reg query "HKCU\Network"') do (
  for /F "tokens=1,2,*" %%f in ('reg query "%%~D" /v RemotePath ^| find /I "RemotePath"') do (
    for /F "delims=" %%# in ('dir /B /S /AD "%%~h\%_FolderToSearch%*" 2^>NUL') do (
        if /I "%%~nx#"=="%_FolderToSearch%" echo # found [%%~nD:] "%%~#"
    )
  )
)
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • @Squashman nice solution (does not search in subfolders) but **1**. query for _key property_ `DeviceID` instead of `name` and **2**. add some propperty e.g. `SystemName` to keep clear of _Carriage Return_ problem. Try `… get DeviceID, providername, SystemName /format:csv`. – JosefZ Oct 06 '16 at 02:06
  • My original code searched sub folders but then the user told me they did not want to do that. **it will be in the root of the drive share (avoiding need for recursive subfolder scanning)** – Squashman Oct 06 '16 at 02:10
1

I was way over thinking the code. Using WMIC and a simple IF EXIST should work.

 FOR /F "skip=2 delims=" %%G IN ('"wmic logicaldisk where drivetype=4 get name,providername /format:csv"') do (
    FOR /F "tokens=2,3 delims=," %%H IN ("%%G") DO (
        IF EXIST "%%I\SpecialFolder\" echo %%I\SpecialFolder
    )
 )
 pause
Squashman
  • 13,649
  • 5
  • 27
  • 36
  • Holy Moly, you pretty much nailed it!! It needs a tiny tweak if possible. Firstly it scanned through sub folders, if its possible to just search root of the mapped drives? appreciate your efforts very much. – Tika9o9 Oct 05 '16 at 19:17
  • @Tika9o9, that actually simplifies the code a lot and makes it run much faster. I totally overlooked your specifications in your question. My apologies. I will be right back with new code. – Squashman Oct 05 '16 at 19:19
  • no worries bro, I had a disconnected drive when I tested and that caused the "cant find drive error" so ignore that my bad. Just the tweak to the folder search now, thanks!!! – Tika9o9 Oct 05 '16 at 19:23
  • Impressive. Seriously impressive. Thanks very much. See I can now use that as a variable and use it in a config file. So by having a predefined folder somewhere on a share my installation can be practically fully automated and configured with the UNC loaded automatically!!! – Tika9o9 Oct 05 '16 at 19:33
  • I'm going to add the code to my existing script, which doesn't have setlocal delayed on it? Its fairly basic, should I add it to the whole script now or just leave it added as starting when this new code runs? thanks !! – Tika9o9 Oct 05 '16 at 19:36
  • @Tika9o9, I added a second set of code to find the folder in only the root directory. I also made both search case insensitive. Please update your batch file with my latest code. – Squashman Oct 05 '16 at 19:36
  • @Tika9o9 The `setlocal enabledelayedexpansion` is needed for the `UNC` variable. The UNC path had a hanging space at the end of the variable so I needed to remove it so that the full path looked correct. When you are inside a code block, you need to use delayed expansion for your variables when you are manipulating them. – Squashman Oct 05 '16 at 19:38
  • Thanks!! Seem to have a problem its including some text from the net use command in the path ...\\DISKSTATION\Downloads Microsoft Windows Networ\specialfolder...was okay previous...? – Tika9o9 Oct 05 '16 at 19:48
  • @Tika9o9, the find command should only be finding lines with a \\ in it. The **Microsoft Windows Network** wording is on its own line in my `NET USE` output. Are you saying that wording is on the same line in your `'NET USE` output? – Squashman Oct 05 '16 at 19:50
  • yes all on one line. was fine first few tries just reported the location now I mapped a new drive to test seems to have gone awry :( – Tika9o9 Oct 05 '16 at 20:07
  • also if I run as administrator it breaks it, doesn't show the path anymore :_( ? Hope you can fix it. – Tika9o9 Oct 05 '16 at 20:35
  • @Tika9o9, please edit your question and show me the output from the `NET USE` command. – Squashman Oct 05 '16 at 22:57
  • @Tika9o9, I tested my script with running as administrator and I still get the same output. So they may have changed a lot of things with Windows 10. – Squashman Oct 05 '16 at 23:34
  • I run in a win7 VM and I get the output mixed in with the path like on 10 .....\\DISKSTATION\NetBackup Microsoft Windows Networ\Daphne – Tika9o9 Oct 05 '16 at 23:38