Batch - To search (Search.bat)

December 2016




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 

@ECHO OFF 
cls 
set folder=%~1 
set str=%folder:~-1% 
if %str%==\ GOTO MoveOn 
set folder=%~1\ 

:MoveOn 
set files=*%~2* 

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

Note


Thanks to rizvisa1 for this tip on the forum.

Related :

This document entitled « Batch - To search (Search.bat) » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.