2

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
  • For moving, no need to use robocopy when Move-Item does the job: Move-Item -Path $oldpath -Destination $newpath. – harrymc Apr 10 '19 at 19:27
  • @harrymc I tried that, but also no result, I described my difficulties in the Edit above. – Kleajmp Apr 10 '19 at 19:52
  • Try Move-Item -Path "$oldpath" -Destination "$newpath" – harrymc Apr 10 '19 at 20:14
  • @harrymc I made the change but same result, no errors. I think it is more of a logical error in the loop structure or wildcard? – Kleajmp Apr 10 '19 at 23:42
  • Does this work? Get-ChildItem F:\Music -Filter "*[flac]*" -Recurse | ? { $_.PsIsContainer } | foreach { Move-Item -Path $_.FullName -Destination "F:\Musicflac" } – SimonS Apr 11 '19 at 6:33
1

My reputation isn't good enough to comment yet :D

Have you verified you're getting the items to begin with? Testing your Get-ChildItem command alone to see if it returns the desired folders?

I suggest trying:

gci "F:\Zandbak\Music" -directory -recurse -filter '*[flac]*' |
   %{
     $newpath = $_.Parent.fullname -replace 'F:\\Zandbak\\Music', 'F:\Zandbak\Musicflac'
     If (!(test-path $newpath))
        {new-item $newpath -itemType directory -whatif}
     Move-Item $_.FullName $newpath -Whatif
   }

If the output from the above code indicates it will create & move thee correct folders, remove the '-WhatIf' pareameters from new-item and move-item.

Keith

| improve this answer | |
  • I did some further testing and I partially made it work thanks to your code. Now only the folders are moved and not the contents within. I made an edit to your post with the working code (Edit 2) – Kleajmp Apr 16 '19 at 20:03
  • What errors were you getting with my code? Other than the fact that your original post didn't specify that Music was a sub-directory of Zandbak? – Keith Miller Apr 17 '19 at 0:31
  • 1
    I think I see where your edits went wrong. In the $newpath assignment statement, you replaced '$_.Parent.FullName' with '$_.fullname' and proceeded to use it as an arguement for 'new-item', thus creating an empty folder with the same name of the folder you want to move ---- thereby blocking 'move-item' from moving the source folder, as a folder with the same name now already exists. My reference to the parent folder and subsequent creation of the structure only up to the parent was intentional & necessary for 'move-item' to function correctly. – Keith Miller Apr 17 '19 at 2:54
  • 1
    So, at this point, you need to delete the new empty folder structure before running the code again. Also, I intentionally searched & replaced for the root of the path rather than simply the word 'music' because the word 'music' is quite common in many album names. Not knowing the names of all your sub folders, I didn't want to change "The Music of blan-blah-blah" to "The Muiscflac of blah-blah-blah".. The double slashes are necessary in the search parameter of the '-replace' operator (but not the replace parameter) because it is a regular expression, so backslashes must be escaped. – Keith Miller Apr 17 '19 at 3:04
  • I edited the path names in my code to reflect the paths shown in your latest edit. – Keith Miller Apr 17 '19 at 3:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.