Reputation: 425
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!
Upvotes: 4
Views: 2894
Reputation: 30123
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:] "%%~#"
)
)
)
Upvotes: 3
Reputation: 14290
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
Upvotes: 1