I have a F:\Music folder with a lot of subfolders, some of them contain the [flac] string in their folder name, I like to move all the folders with their contents, that contain the [flac] string, into a new folder while keeping all the folder structures. Some paths go deeper than just one folder.
get-item "F:\Music\*\*[flac]" |
foreach ($_){
$oldpath = $_.FullName;
$newpath = "F:\Musicflac\"+$_.Parent;
robocopy $oldpath $newpath /MOVE /E
}
After searching and trying I came to this PS script, returning no errors but also no results. I found a similar topic but this only works in bash: Move folders containing files with extension. I'm completely new to PowerShell.
Edit 1:
I also tried with Move-Item:
$paths = Get-ChildItem "F:\Music\*\*[flac]" | select fullname | ForEach-Object {$_.fullname}
$destination = "F:\Musicflac\"
foreach ($path in $paths){
Move-Item $path -Destination $destination
}
Also nothing happens, the difficulties for me are: the must contain string [flac] and the keep folder structure.
Edit 2:
Your script gave some errors in the first place but after some adjustements I finally got results:
gci "F:\Zandbak\Music\" -directory -recurse -filter '*[flac]*' |
%{
#calculate new path
$newpath = $_.fullname -replace 'Music', 'Musicflac'
#check if new path excists, if not create it
if(!(test-path -Path $newpath)){ new-item $newpath -itemType directory }
#move the directory and its contents to another directory
Move-Item -Path $_.FullName -Destination $newpath
}
No more errors and the correct folders in the Musicflac directory are being created! Unfortunately the files in the folders are not moved along with the folders, only empty directories are being created.
Any advice for also moving the folder contents?
Edit 3:
directory structure:
F:\Zandbak\Music\A Winged Victory for the Sullen\2011 A winged victory for the sullen
F:\Zandbak\Music\A Winged Victory for the Sullen\2011 A winged victory for the sullen [flac]
F:\Zandbak\Music\A Winged Victory for the Sullen\2011 A winged victory for the sullen\2014 Atomos IXII
F:\Zandbak\Music\A Winged Victory for the Sullen\2011 A winged victory for the sullen\2014 Atomos IXII [FLAC]
F:\Zandbak\Musicflac
Move-Item -Path $oldpath -Destination $newpath
.Move-Item -Path "$oldpath" -Destination "$newpath"
Get-ChildItem F:\Music -Filter "*[flac]*" -Recurse | ? { $_.PsIsContainer } | foreach { Move-Item -Path $_.FullName -Destination "F:\Musicflac" }