search & copy

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

search & copy

#1 Post by glob5 » 02 Jul 2013 21:26

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.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search & copy

#2 Post by foxidrive » 02 Jul 2013 23:07

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

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: search & copy

#3 Post by glob5 » 03 Jul 2013 00:23

what popd does?.

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: search & copy

#4 Post by glob5 » 03 Jul 2013 00:38

ok. i didn't see the pushd.
ok. how about if "word1" and "word2" should be in the same line to copy.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search & copy

#5 Post by foxidrive » 03 Jul 2013 00:59

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: search & copy

#6 Post by penpen » 03 Jul 2013 04:27

@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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: search & copy

#7 Post by foxidrive » 03 Jul 2013 05:24

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...

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: search & copy

#8 Post by penpen » 03 Jul 2013 05:38

Ups... sorry for that, here is the corrected version:

Code: Select all

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

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: search & copy

#9 Post by Aacini » 03 Jul 2013 09:16

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...

Post Reply