Copying files which contain particular keywords, and putting them in another folder

I have stored file paths which match particular text which I am looking for, in a variable. Now, what I want to do is copy those files, which match content I'm looking for, and put them into a new folder.

I have written the following simple scripts, neither of which work:

(I)

Copy-Item -path $match in $filepaths -Destination C:\newfolder -Recurse


Corresponding error messages, in the following order:

"a positional parameter cannot be found which accepts argument 'in'"
"ParametersBindingException"
"PositionalParameterNotFound"

(II) 

foreach($match in $filepaths)
{
 Copy-Item -path $match -Destination C:\newfolder
}

Corresponding error message, showing for each item in $filepaths:

"cannot find path" 

Any ideas as to how to make them work?

June 21st, 2015 10:11am

Version 1 won't work but version 2 should do. From the error you're getting I'd suggest the issue is related to $filepaths, so either how it's being generated or something about the paths that are being produced.

How is that variable being generated? Where it is pulling its content from?

It might be worth replacing the copy-item line with $match, eg

foreach($match in $filepaths)
{
 $match
}
so it displays what is being generated, and then check that it is outputting paths that are 1) valid and 2) exist.

Free Windows Admin Tool Kit Click here and download it now
June 21st, 2015 10:23am

I tried the above test, and this returns valid file paths.

However, I did apply the following to $files [the variable containing the original matching results]:

$filepaths = $files | Format-Table * -wrap
Could wrapping the results cause any issues, or formatting the results into a table?

June 21st, 2015 10:36am

$filepaths = Get-ChildItem c:\ -File
foreach($file in $filepaths){
    Copy-Item -path $file.Fullname -DestinationC:\newfolder
}
Free Windows Admin Tool Kit Click here and download it now
June 21st, 2015 11:08am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics