Search code examples
powershellfile-copying

Powershell copy files with specific name


I tried to copy files from one folder to another which have word HIGH at the end of name of files in their names but didn't get it. Any suggestion?

$dest = "C:\transform"
$source = "D:\result"
get-childitem $source - filter ".jpg" -recurse | Where-Object {$_.DirectoryName -match "HIGH" | ForEach-Object { Copy-Item $.fullname $dest}

Solution

  • $_.DirectoryName holds the folder name, $_.Name the file name :

    $dest = "C:\transform"
    $source = "D:\result"
    Get-ChildItem $source -Filter ".jpg" -Recurse |
        ? { $_.BaseName -match "HIGH$" } |
        % { Copy-Item $_.FullName $dest}
    

    Or, as pointed by @Walter Mitty, a simpler :

    Copy-Item -Path $source -Filter "*HIGH.jpg" -Destination $dest –Recurse
    

    (in this case -Filter and -Include seem to behave the same)