0

The script I need:

Search through the current directory and subfolders for specified name.

I don't want to specify the name within the script. I want to specify it after the script is running.

What I got so far:

echo   Type your filename to search
echo  .-=========================-.
SET Fileput=
SET /P Fileput=Filename:
IF /I '%Fileput%'=='1' GOTO Search
:search
For each "tokens=*" %%I in ('cd% /s /b') do set filefound="%%~fI"
echo %FILEFOUND% >> "C:\temp\%date%_search_result_%random%.txt"

I'm pretty sure the issue is at "IF /I '%Fileput%'=='1' GOTO Search" because thats not what I want. I want the text I type in to be the input to search for. Or perhaps its another way to SET the input... Thanks!

2
  • 1
    There is no for each loop in command prompt or batch scripts (I guess you mean for /F); you can enumerate a directory tree by for /D; however, to search for a certain file, you can use dir /S %Fileput% check %ErrorLevel% which is 1 if not found and 0 otherwise... Commented Dec 1, 2015 at 13:13
  • 1
    what's wrong with dir /b /s %Fileput%>result.txt? Commented Dec 1, 2015 at 14:37

2 Answers 2

2

Try this way :

@echo off

set /p "$SearchStr=Filename : "

for /f "delims=" %%a in ('dir /s/b/a-d') do (
     echo "%%~nxa" | find /i "%$SearchStr%" >nul && (
             echo Found [%$SearchStr%] in =^> %%a
             echo File Name =^> %%~nxa
             echo Path =^> %%~dpa
             echo * * * *)
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot @SachaDee, that seems to work. However, it just tells me that it found it, nothing more. I'd like the information of the file as well. Like full filename at least. Location would be great, but not necessary.
Now we're talking, @SachaDee. My last issue would be that this also includes folders for some weird reason. I looked it up, but the /a-d doesn't seem to do the job. The folder I have the script in has "test" in its name. If I write test in the search, the result will include absolutely all files within that folder. You you have any suggestions?
You are amazing, @SachaDee. That worked perfectly. Thanks yet again!
1

I believe you are setting the input just fine. You could just use:

@echo off

SET /P Fileput=Filename:
for /F "tokens=* USEBACKQ" %%a in (`dir /b /s "%Fileput%"`) do (
    echo %%~fa>>outputfile.txt
)

Note: USEBACKQ enables using backquotes for the command in the FOR.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.