Copy files if they contain a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
somen
Posts: 2
Joined: 22 May 2012 04:42

Copy files if they contain a string

#1 Post by somen » 22 May 2012 05:03

Hello Guys,

I'd like to ask your help. I'm a beginner in batch programming, so don't be too hard with me.
I need to write a batch command, which copies files if they contain certain string.
I googled findstr /m "string" *.dat, but I don't know how to pipeline with copy command. Could you please help me :) ?

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

Re: Copy files if they contain a string

#2 Post by foxidrive » 22 May 2012 05:06

Untested:

Code: Select all

@echo off
for %%a in (*.*) do (
findstr /c:"my string to find" "%%a" >nul && copy "%%a" "c:\target folder\"
)

somen
Posts: 2
Joined: 22 May 2012 04:42

Re: Copy files if they contain a string

#3 Post by somen » 22 May 2012 05:10

Huh, it was very quick 8) ! Thank you mate, it works.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Copy files if they contain a string

#4 Post by Fawers » 22 May 2012 09:29

Perhaps it can be done with FOR /f too?

Code: Select all

@echo off
for /f "delims=" %%a in ('findstr /m /c:"string" *.*') do ^
copy "%%a" "C:\path\to\your\folder"

Post Reply