Page 1 of 1

search & copy

Posted: 02 Jul 2013 21:26
by glob5
hi, i have lots of CSV files in a dreictory and its subs. I am new to batch scripting. windows 7 dos.
i need a batch script that search all the CSV files in only a folder (C:\folder1 , not its sub-folders) and copy all the csv files that contain "word" and "word2" to C:\folder2. I need an option of going to the sub directories too.

Re: search & copy

Posted: 02 Jul 2013 23:07
by foxidrive
This may work - it is untested:

Code: Select all

@echo off
pushd "c:\folder1"
for %%a in (*.csv) do (
find "word1" <"%%a" >nul && find "word2" <"%%a" >nul && copy "%%a" "c:\folder2" >nul
)
popd

Re: search & copy

Posted: 03 Jul 2013 00:23
by glob5
what popd does?.

Re: search & copy

Posted: 03 Jul 2013 00:38
by glob5
ok. i didn't see the pushd.
ok. how about if "word1" and "word2" should be in the same line to copy.

Re: search & copy

Posted: 03 Jul 2013 00:59
by foxidrive
If you change the task then with batch files the code often changes dramatically.

Tell us what the exact details of the task are instead of changing the task when you get a reply to your previous question.
If you want to learn about techniques and tips then ask precise questions instead of asking how to solve a hypothetical problem.

Re: search & copy

Posted: 03 Jul 2013 04:27
by penpen
@glob5
This time you have good luck, that you can meet your new task with only changing foxidrive's code slightly.

Just change the content of the for loop a little bit to:

Code: Select all

findstr "word1.*word2" "%%a" >nul && copy "%%a" "c:\folder2" >nul

Re: search & copy

Posted: 03 Jul 2013 05:24
by foxidrive
penpen wrote:@glob5
Just change the content of the for loop a little bit to:

Code: Select all

findstr "word1.*word2" "%%a" >nul && copy "%%a" "c:\folder2" >nul


As long as he doesn't want to find word2 followed by word1 as well...

Re: search & copy

Posted: 03 Jul 2013 05:38
by penpen
Ups... sorry for that, here is the corrected version:

Code: Select all

findstr "word1.*word2 word2.*word1" "%%a" >nul && copy "%%a" "c:\folder2" >nul

Re: search & copy

Posted: 03 Jul 2013 09:16
by Aacini
Or this one (using FindRepl.bat):

Code: Select all

< "%%a" FindRepl "(word1.*word2)|(word2.*word1)" >nul || copy "%%a" "c:\folder2" >nul

FindRepl offers wider regexp capabilities that can be used to achieve more specific or advanced searches...