1

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
6
  • For moving, no need to use robocopy when Move-Item does the job: Move-Item -Path $oldpath -Destination $newpath.
    – harrymc
    Commented Apr 10, 2019 at 19:27
  • @harrymc I tried that, but also no result, I described my difficulties in the Edit above.
    – Kleajmp
    Commented Apr 10, 2019 at 19:52
  • Try Move-Item -Path "$oldpath" -Destination "$newpath"
    – harrymc
    Commented Apr 10, 2019 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
    Commented Apr 10, 2019 at 23:42
  • Does this work? Get-ChildItem F:\Music -Filter "*[flac]*" -Recurse | ? { $_.PsIsContainer } | foreach { Move-Item -Path $_.FullName -Destination "F:\Musicflac" }
    – SimonS
    Commented Apr 11, 2019 at 6:33

1 Answer 1

0

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

11
  • 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
    Commented Apr 16, 2019 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? Commented Apr 17, 2019 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. Commented Apr 17, 2019 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. Commented Apr 17, 2019 at 3:04
  • I edited the path names in my code to reflect the paths shown in your latest edit. Commented Apr 17, 2019 at 3:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .