Searching in cmd [Solved/Closed]

Report
-
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
-
Hello,
i want to write a batch file that allows me to search a name in cmd for example if i write this command in cmd search c:\new folder s
everyfile that has s in the name of it should be listed like see saw or anything else

6 replies

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
760
if the 4 points i said were correct. then 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 the characters of the file

Save the code below Search.bat in your root c:\

once save, 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%
1
Thank you

A few words of thanks would be greatly appreciated. Add comment

CCM 2942 users have said thank you to us this month

Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
760
use dir command. To see what you can do with dir, use dir /? command

for your example
dir /a-d "C:\new folder\*s*"

no actually i want to make a command not using dir i want to write a batch file
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
760
Not entirely clear about what you are want to accomplish. If just you want to run the command as a batch, you can simply type the same into the batch program and run it.

in case you want to hard code the dir
dir /a-d "c:\new folder\*s*"

In case you want to supply it as a parameter. In this case enclose the c:\new folder\*s* in the ". Say batch file is test.bat, then run it as test "c:\new folder\*s*"
dir /a-d %1


If you are trying to capture the result, you may want to pipe it to some file, like
dir /a-d "C:\new folder\*s*" > mysearch.txt

u didnt understand what i say i want to write in cmd "search c:\windows s"
i want to add command search to cmd and define it anything which has s in name.
Posts
4476
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
August 2, 2020
760
Well correct me where I am falling off.
1. you are using vista
2. when you say "cmd" you are talking about the command "cmd" that open a dos sort windows.
3. you would be typing in cmd and open the dos window
4. there you would be typing in command "search c:\windows s"

If that all is correct, then the command i gave you would work. Only thing that needs to be ensured is that the location of the path is where the cmd windows open or is available in the PATH

i want to write search in cmd i did what u said but when i write search it doesnt understand and there is error