Answered by:
Moving matching file names, different extension (one last step I need)

Question
-
Hi all,
I'm wanting to move all matching file names with different extensions to a new folder. I know how to group and print the items that match, but not sure how to move them from there.
Let's say I have a folder named "Test" with files 1.txt, 2.txt and 1.jpg. I want to move 1.txt and 1.jpg to a new folder called "Destination"
Here is what I can put into the powershell right now to see which files match up.
Get-ChildItem -Recurse -File | Group-Object -Property Directory,BaseName | Where-Object Count -gt 1 | Select-Object Name
That displays the files that match, but now I need to move them. Any advice? Thanks in advance :)Friday, August 11, 2017 4:38 PM
Answers
-
This worked for me:
Get-ChildItem -Recurse -File | Group-Object -Property Directory,BaseName | Where-Object Count -gt 1 | foreach-object{ $string = $_.Name.Split(',') $path = $string[0] $basename = $string[1].TrimStart(' ') $items = get-childitem -Path $path -Recurse | ?{$_.name -like $basename + '*'} foreach($item in $items){ move-item ($path + '\' + $item) -Destination "C:\location" } }
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
- Marked as answer by wmc326 Friday, August 11, 2017 5:58 PM
Friday, August 11, 2017 5:34 PM
All replies
-
With that information there is no way to know which files to move. How do you knw which file to move of a set. You must choose which extension you want to select.
If the files are truly numeric then just choose the lowest number in the set.
\_(ツ)_/
Friday, August 11, 2017 4:49 PM -
This worked for me:
Get-ChildItem -Recurse -File | Group-Object -Property Directory,BaseName | Where-Object Count -gt 1 | foreach-object{ $string = $_.Name.Split(',') $path = $string[0] $basename = $string[1].TrimStart(' ') $items = get-childitem -Path $path -Recurse | ?{$_.name -like $basename + '*'} foreach($item in $items){ move-item ($path + '\' + $item) -Destination "C:\location" } }
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.
- Marked as answer by wmc326 Friday, August 11, 2017 5:58 PM
Friday, August 11, 2017 5:34 PM -
Thank you so much! This does exactly what I needed.Friday, August 11, 2017 5:58 PM