I'm looking to find a specific file name within a specific folder name in a directory recursively and return the full path if there is a match or multiple matches. I'm looking for a pattern search that can possibly even use a 3 part lookup i.e (folderA\folderB\file.name). But 2 will do for now. So far my efforts have brought me to this. But it is terribly slow. 16 seconds to find the match.
::recursively find foldername\file.name combination
cd C:\searchFolder
for /r %a in (FolderName) do @if exist "%~fa\file.name" echo %~fa
I only need to venture about 2 to 4 levels deep and pull up the first folder\file.ext match and grab the full path. Is there a faster way to do this, with maybe the dir command or an exclusion list or even a true *folder\file pattern search.
CodePudding user response:
Let us first clarify the task with an example folder structure and a file with name file.name
in each subfolder of C:\SearchFolder
:
C:\SearchFolder
├───FolderA
│ │ file.name
│ │
│ ├───FolderA
│ │ │ file.name
│ │ │
│ │ ├───FolderA
│ │ │ │ file.name
│ │ │ │
│ │ │ ├───FolderA
│ │ │ │ file.name
│ │ │ │
│ │ │ └───FolderB
│ │ │ file.name
│ │ │
│ │ └───FolderB
│ │ │ file.name
│ │ │
│ │ ├───FolderA
│ │ │ file.name
│ │ │
│ │ └───FolderB
│ │ file.name
│ │
│ └───FolderB
│ │ file.name
│ │
│ ├───FolderA
│ │ │ file.name
│ │ │
│ │ ├───FolderA
│ │ │ file.name
│ │ │
│ │ └───FolderB
│ │ file.name
│ │
│ └───FolderB
│ │ file.name
│ │
│ ├───FolderA
│ │ file.name
│ │
│ └───FolderB
│ file.name
│
└───FolderB
│ file.name
│
├───FolderA
│ │ file.name
│ │
│ ├───FolderA
│ │ │ file.name
│ │ │
│ │ ├───FolderA
│ │ │ file.name
│ │ │
│ │ └───FolderB
│ │ file.name
│ │
│ └───FolderB
│ │ file.name
│ │
│ ├───FolderA
│ │ file.name
│ │
│ └───FolderB
│ file.name
│
└───FolderB
│ file.name
│
├───FolderA
│ │ file.name
│ │
│ ├───FolderA
│ │ file.name
│ │
│ └───FolderB
│ file.name
│
└───FolderB
│ file.name
│
├───FolderA
│ file.name
│
└───FolderB
file.name
That folder structure with the files can be created using:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BaseFolder=C:\SearchFolder"
for %%G in (A B) do (
for %%H in (A B) do (
for %%I in (A B) do (
for %%J in (A B) do (
md "