A .bat or .wsh script that can search for files
Asked Answered
I

3

6

I am looking for some examples of a .bat OR .wsh script that can do the following:

  • Recursively read file names in a directory with a user-provided extension (.dll, .exe, etc)
  • Search a user-provided directory for the above file names
  • Generate a txt or xls report of the findings, like: x.txt was found in "C:\temp", "C:\blah"

TIA.

EDIT:

Oops, I should clarify: there are two directories and two searches here.

Search 1:

  • Search a user provided directory "Dir 1" for all *.dll's.

Search 2:

  • Search a different user provided directory "Dir 2" for the file names generated in Search 1. This search also needs to be recursive.

So, if Search 1 finds foo.dll, foo2.dll and foo3.dll in Dir 1, Search 2 should look in Dir 2 for foo.dll, foo2.dll and foo3.dll, and provide a report (simple listing) of each found file.

Initiate answered 5/1, 2009 at 22:21 Comment(0)
K
9

Put the following in a .bat file, say FindAll.bat:

@echo OFF

for /f %%F in ('dir %2\%1 /s /b') do (
    <nul (set /p msg=%%~nxF )
    for /f %%G in ('dir %3\%%~nxF /s /b') do (
        if exist %%G (
            @echo found at %%G
        ) 
    )
)

%1 is the user provided file mask.

%2 is the user provided directory to search first.

%3 is the user provided directory to search second.

Call from the command line to generate a report:

FindAll *.dll d:\dir1 d:\dir2 > dll_report.txt 2>&1

The <nul (set /p) trick will output text to the console without a new line (courtesy Pax from this thread: How to code a spinner for waiting processes in a Batch file?)

The 2>&1 added when calling the batch file is needed to capture all the output to the file (courtesy aphoria from this thread: Underused features of Windows batch files)

Keramic answered 6/1, 2009 at 2:27 Comment(2)
This gets us close; need to add a /s to the first line. What we really need is a way to strip the directory from the filename in %%f; right now it is looking for the full file name from dir1 in dir2. Also, the search in dir2 (%%3) needs to be recursive as well.Initiate
Great, thanks for coming back and altering this, and for the showing how to use <nul and 2>&1.Initiate
I
18

Why not use dir?

Search current directory and all subdirs for dlls

dir /S *.dll

Search all of C for dlls

dir /S C:\*.dll

Save a report

dir /S C:\*.dll > report.txt
Ium answered 5/1, 2009 at 22:27 Comment(2)
Check out my follow up answer, which clarifies the question.Initiate
Ah, thank you very much! It didn't even occur to me that dir would do this! Also, you can use dir /B /S "myPath""*myString*".Liberticide
K
9

Put the following in a .bat file, say FindAll.bat:

@echo OFF

for /f %%F in ('dir %2\%1 /s /b') do (
    <nul (set /p msg=%%~nxF )
    for /f %%G in ('dir %3\%%~nxF /s /b') do (
        if exist %%G (
            @echo found at %%G
        ) 
    )
)

%1 is the user provided file mask.

%2 is the user provided directory to search first.

%3 is the user provided directory to search second.

Call from the command line to generate a report:

FindAll *.dll d:\dir1 d:\dir2 > dll_report.txt 2>&1

The <nul (set /p) trick will output text to the console without a new line (courtesy Pax from this thread: How to code a spinner for waiting processes in a Batch file?)

The 2>&1 added when calling the batch file is needed to capture all the output to the file (courtesy aphoria from this thread: Underused features of Windows batch files)

Keramic answered 6/1, 2009 at 2:27 Comment(2)
This gets us close; need to add a /s to the first line. What we really need is a way to strip the directory from the filename in %%f; right now it is looking for the full file name from dir1 in dir2. Also, the search in dir2 (%%3) needs to be recursive as well.Initiate
Great, thanks for coming back and altering this, and for the showing how to use <nul and 2>&1.Initiate
E
-3

I would study Robocopy to see if this could help (the /L flag is a clue).

Eserine answered 6/1, 2009 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.