Search code examples
batch-filewindows-scripting

Search for a specified file on my Windows machine using batch scripting


I have a file TestProject.dll and it resides in different locations on my computer like D:\Folder1\TestProject.dll, D:\Test\Info\TestProject.dll etc.

I want to find all these locations wherever it is located and prepare a text file (SearchResults.txt) which looks like as shown below:

D:\Folder1\TestProject.dll
D:\Test\Info\TestProject.dll

I want to do this using a batch scripting file. I am new to this scripting. Please help me here.


Solution

  • use dir /s /b to search in a single drive.
    Build a loop around that to check every disk.
    put the complete output into a file.

    (
      for /f %%a in ('wmic logicaldisk where "drivetype=3" get caption^,size^|find ":"') do (
        echo now checking drive %%a...
        dir /b /s %%a\TestProject.dll 
      )
    )>SearchResults.txt
    

    Note: we don't really need the size here, this is only one of several ways to come around wmic's ugly line endings, that would ruin the rest of the code
    where drivetype=3 means "Harddisks only" (drop it, if you want to search in all drivetypes (Thumbdrives, CD, whatever)

    Remember: this will search through your whole file system(s), so it will need some time.