1

I have a common issue in that I have multiple client setups on a server, and want to check in a particular folder in each of them for a filename, or file search pattern.

e.g. I might have this setup:

D:\AllClients\
    |_____ClientA\
            |____Specific\Subpath\
                             |_____MyFilename.html
    |_____ClientB\
            |____Specific\Subpath\

And I need it to look in the specific subpath of each of the client directories and return the path to any existing file with the supplied filename, or file search pattern.

i.e. I need to loop over the subdirectories within a given directory, then search within a subpath for a file pattern (unless, of course, there's a better way where I could wildcard out the name of the client's directory).

So far I have this:

@echo off
setlocal
setlocal enabledelayedexpansion

set filename=MyFilename.html
set basePath=D:\AllClients
set subPath=\Specific\Subpath\

echo %basePath%

for /D %%G in (%basePath%\*)  do (

    @set _searchPath=%%G%subPath%
    @echo Searching !_searchPath!
    @for /r "!_searchPath!" %%a in (%filename%) do (
        @echo ...%%a
        @IF EXIST "%%a" (
            @echo ...%%a
        )
    )

)

which gives me the right search path printed out, but my user path seems to get prepended to %%a.

1
  • Is PowerShell an option? Commented Aug 20, 2020 at 5:27

2 Answers 2

1

PowerShell would look like this:

$Root = 'D:\AllClients'
$SubPath = 'Specific\Subpath'
$FileName = 'FileName.html'

$FilePath = Join-Path $SubPath $FileName
Get-ChildItem $Root -Directory | ForEach {
    $Path = Join-Path $_.FullName $FilePath
    If ( Test-Path $Path ) {
        Write-Output "Found: $Path"
    } Else {
        Write-Output ('File not found in: {0}' -f ( Split-Path $Path ))
    }
}
1

You can simplify this search in a loop, using where /Recursive with find limiting the loop's output to its subpath only

@echo off && setlocal enabledelayedexpansion

set "_basePath=D:\AllClients"
set "_fileName=MyFilename.html"
set "_subPath=\Specific\Subpath"

echo\Searching !_basePath!!_subPath!.. & for /f tokens^=* %%i in ('
2^>nul where /r  "!_basePath!" "!_fileName!" ^| find/i "!_subPath!"
')do echo\Found: "%%~i" & echo\Path: "%%~dpi" & echo\Name: "%%~nxi"

timeout /t -1 & endlocal
  • Or...
@echo off

setlocal enabledelayedexpansion

set "_basePath=D:\AllClients"
set "_fileName=MyFilename.html"
set "_subPath=\Specific\Subpath"

echo\Searching !_basePath!!_subPath!.. 

for /f tokens^=* %%i in ('2^>nul where /r  "!_basePath!" "!_fileName!" ^| find /i "!_subPath!"') do (
    echo\File Found: %%~i
    echo\File Path: %%~dpi 
    echo\File Name: %%~nxi
)

timeout /t -1 & endlocal

The using args[]:

arg[1] fileName == %~1
arg[2] basePath == %~2
arg[3] subPath == %~3

@echo off && setlocal enabledelayedexpansion

echo\Searching %~2 %~3.. 
for /f tokens^=* %%i in ('2^>nul where /r  "%~2" "%~1" ^|find/i "%~3"
')do echo\Found: "%%~i" && echo\Path: "%%~dpi" && echo\Name: "%%~nxi"

timeout /t -1 & endlocal

The same without set variables:

@echo off && setlocal enabledelayedexpansion

echo\Searching D:\AllClients \Specific\Subpath... 

for /f tokens^=* %%i in ('
2^>nul where /r "D:\AllClients" "MyFilename.html" ^| find/i "\Specific\Subpath"
')do echo\Found: "%%~i" && echo\File Path: "%%~dpi" && echo\file Name: "%%~nxi"

timeout /t -1 & endlocal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .