Batch - To search (Search.bat)

April 2018

How to search using command?

First the batch file needs to be called "search.bat" as you are typing Search.
The other thing is that the search.bat file should be in the same folder from where you are trying to run it. Other option would be the have the path where the search.bat resides added the environment variable "PATH"

One change to the code is needs as it seems that you are passing two parameters. One the director and the other the characters of the file

Save the code below Search.bat in your root c:\
  • Once saved,
  • Type: cmd
  • Then type: cd /d c:\
  • Then run search file,
    • example:

search "c:\windows" "sys"

REM search.bat 

set folder=%~1 
set str=%folder:~-1% 
if %str%==\ GOTO MoveOn 
set folder=%~1\ 

set files=*%~2* 

set searchFor="%folder%%files%" 
dir /a-d %searchFor%


Thanks to rizvisa1 for this tip on the forum.
Published by aakai1056. Latest update on February 9, 2010 at 06:39 AM by aakai1056.
This document, titled "Batch - To search (Search.bat)," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (
Removing the files remaining after uninstalling a program
Windows - handling the "Please Insert a Disk into Drive Error"