You didn't give detaailed info on the folder structure, but be aware you asked to move any
folder that contains one or more target
files. So if an undesired file is in the same folder as a target file, it will be moved, because we're moving entire folders, not individual files.
If you want the folders with only targeted files, that's a little more code. But at this point, I'll assume that wanted vs. unwanted files are always in separate folders.
There are a number of ways to filter the files, but for the average stand-alone user, the differences in processing for that are most likely minimal.
Working with the previous command, one way would be to create an array of strings containing the desired extensioins. The most straightforward way would be:
Code:
$TargetExt = @( '.mkv' , '.mp4' , '.avi' )
But, if you plan to re-use code, for ease of editing and eradability, I like using a
Here-String
and then using the
-Split
operator to split the string on the newline character to create the array:
Code:
$TargetExt = @'
.mkv
.mp4
.avi
' -split '\n'
WIth that, we can filter the output of
Get-ChidItem
(
gci
) using
Where-Object
(
where
) to check if the
Extension
property of the
FileInfo
object before piping it to
Move-Item
:
Code:
$TargetExt = @'
.mkv
.mp4
.avi
' -split '\n'
gci *web* -Recurse | where Extension -in $TargetExt | select -expand Directory -Unique | Move-Item -Destination 'C:\web' -WhatIf