0

I have a folder that contains several thousand files. I would like to write a Powershell script that loops through the files and copies each file whose filename contains a specific keyword. In pseudocode:

For each file in C:\[Directory]
    If filename contains "Presentation" Then
        copy file in C:\[Directory 2]

4 Answers 4

3

Simply like this ?

copy-item "C:\SourceDir\*Presentation*" "C:\DestinationDir"

or like this :

copy-item "C:\SourceDir\*" "C:\DestinationDir" -Filter "*rrrr*"

But a risk exist if you have a directory with "presentation" in his name into the source directory. Then take all method proposed here and add -file in get-childitem command. Like in this short version of Robdy code :

gci "C:\SourceDir" -file | ? Name -like "*Presentation*" | cpi -d "C:\DestinationDir"
Sign up to request clarification or add additional context in comments.

Comments

1

That code should do the trick:

$files = Get-ChildItem -Path "C:\path\to\source\folder"
$files | Where-Object Name -Like "*Presentation*" | Copy-Item -Destination "C:\path\to\destination\folder"

Of course can be written in one line but I put in two for visibility.

Edit: as Esperento57 pointed out, you might want to add -ItemType File to Get-ChildItem cmdlet to not include folders with 'Presentation' in their name. Also, depending on your needs you might also want to use -Recurse param to include files in subfolders.

If you have files in subfolders and you want to keep the path in destination folder you'll have to change the script a bit to something like:

Copy-Item -Destination $_.FullName.Replace('C:\path\to\source\folder','C:\path\to\destination\folder')

And for the above you'll have to make sure that folders are actually created (e.g. by using -Force for Copy-Item.

2 Comments

You should be add -file to Get-ChildItem, because if you have a dir with "Presentation" in his name... ;)
Thanks for pointing out. There's even more possible scenarios there like Get-ChildItem -Recurse (if files are in subfolders), Copy-Item -Force (if you need to create folder/recreate folder path in destination folder).
0

This seems to work:

$src = "Dir1"
$dst = "Dir2"

Get-ChildItem $src -Filter "*Presentation*" -Recurse | % {
    New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
    Copy-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force
}

1 Comment

You should be add -file to Get-ChildItem, because if you have a dir with "Presentation" in his name... ;)
0

Try something like this:

Get-ChildItem "C:\Your\Directory" -File -Filter *YourKeyWordToIsolate* | 
Foreach-Object { Copy-Item $_.FullName -Destination "C:\Your\New\Directory" }

... but, of course, you'll need to fill in some of the blanks left open by your pseudocode example.

Also, that's a one-liner, but I inserted a return carriage for easier readability.

2 Comments

You should be add -file to Get-ChildItem, because if you have a dir with "Presentation" in his name... ;)
@Esperento57 Good point. Updated based on your recommendation. It's also worth noting that overwriting existing files could be problems as well... but for simplicity, that should be addressed separately I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.