search & copy
Moderator: DosItHelp
search & copy
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.
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
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
ok. i didn't see the pushd.
ok. how about if "word1" and "word2" should be in the same line to copy.
ok. how about if "word1" and "word2" should be in the same line to copy.
Re: search & copy
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.
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
@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:
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
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
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
Or this one (using FindRepl.bat):
FindRepl offers wider regexp capabilities that can be used to achieve more specific or advanced searches...
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...