Powershell Match Filename with the same base name but different file extensions and copy them to a different folder

Powershell version 2

Need shell help, get a directory like the example below. Where there are many files, the common property is the name, but with different extensions. I need to rewrite through the directory the match with the same base name and copy them to the location. The files need to be copied to the destination together.

Sample files. 40127.wav, 40127.txt, 40127.ini, 40128.wav, 40128.txt, 40128.ini

My example script Works for only one element of the array. As I am calling the array member using $ test [0] to match the base name. Not sure how to get it to loop through the array as I seem to be breaking the foreach command.

$ source = c: \ audio

$ test = Get-ChildItem -Path $ source

foreach ($ item in $ test)

{get-childitem -path $ source -recurse | where {$ _. basename -eq $ test [0] .basename} | Move-Item -Destination C: \ Backup}

Result 3 files in C: \ backup 40127.wav, 40127.txt, 40127.ini.

Any help is greatly appreciated.

+3


source to share





All Articles