soapergem
soapergem

Reputation: 10029

How do I write a batch script to search for a specific file based on folder name?

I need to write a batch script that searches for occurrences of a file named SQLite.Interop.dll in a certain directory. There will actually be many occurrences of this file nested under different subdirectories, and specifically I'd like it to find the one where the folder name is net45.

I started to try and write a batch script myself by piecing together different StackOverflow answers, but ultimately didn't get very far. Here's my initial, feeble attempt:

@setlocal enableextensions enabledelayedexpansion
@echo off

for /r C:\Specified\Directory %%i in (SQLite.Interop.dll) do (set str = %%i & if x%str:net45=%==x%filestr% (copy %%i ./SQLite.Interop.dll & goto BreakStmt)

:BreakStmt
endlocal

Things not yet working:

Upvotes: 0

Views: 75

Answers (1)

woxxom
woxxom

Reputation: 73846

Use recursive dir /s and filter the output by the directory name surrounded by \, parse the result with for /f:

for /f "delims=" %%a in ('
    dir /s /b "C:\Specified\Directory\SQLite.Interop.dll" ^| find /i "\net45\"
') do copy /y "%%a" .

This method doesn't require delayed variable expansion.

Upvotes: 2

Related Questions