Batch File to Search and Copy Results

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
joebaber
Posts: 2
Joined: 01 Jun 2010 13:07

Batch File to Search and Copy Results

#1 Post by joebaber » 01 Jun 2010 13:12

Hi,

I want to be able to do a search within a directory and then copy the results into both
1) A text file
2) Copy the found files into a new directory

So far this is what I have:

Code: Select all

@ECHO OFF
SET /p SEARCHTERM=Enter the Search Term:
SET /p DATEINPUT=Enter the Date (Ex: 2010-05-27)
cd "C:\TotalListOfTerms"

FINDSTR /m /i "%SEARCHTERM%" *.* > "C:\ByDate\%DATEINPUT%\%SEARCHTERM%.txt"


------------------

This part works ok, but I want the batch file to also copy the *found* files into C:\ByDate\%DATEINPUT%\


Any help is appreciated!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to Search and Copy Results

#2 Post by aGerman » 01 Jun 2010 13:35

You could try something like that:

Code: Select all

@echo off &setlocal
set /p "SEARCHTERM=Enter the Search Term: "
set /p "DATEINPUT=Enter the Date (Ex: 2010-05-27) "
md "C:\ByDate\%DATEINPUT%" 2>nul
cd "C:\TotalListOfTerms"
>"C:\ByDate\%DATEINPUT%\%SEARCHTERM%.txt" type nul
for /f "delims=" %%a in ('findstr /m /i "%SEARCHTERM%" *.*') do (
  >>"C:\ByDate\%DATEINPUT%\%SEARCHTERM%.txt" echo.%%a
  copy "%%a" "C:\ByDate\%DATEINPUT%\%%a">nul
)


Regards
aGerman

joebaber
Posts: 2
Joined: 01 Jun 2010 13:07

Re: Batch File to Search and Copy Results

#3 Post by joebaber » 01 Jun 2010 14:26

Thanks a ton! That worked :D

Post Reply