2

I'm trying to copy all of the cover.jpg files in my music library to one folder. My attempts so far have either landed me with one file in the destination, or every desired file but also in their own folder matching the source (i.e. folders named for each album containing just the cover.jpg file).

Get-ChildItem "C:\Music" -recurse -filter *.jpg | Copy-Item -Destination "C:\Destination"

I realised that the copy-item command was simply overwriting the previous copies thus leaving me with only one file. I then tried going down the renaming route by moving the file then renaming it but of course that failed as the folder I was basing the rename off has now changed. I don't want to change the name of the file before I copy it as other programs still need the cover.jpg to function.

My question is... Does anybody know how to recursively look through each folder in my music library to find the cover.jpg file, rename it to match the parent folder (or even if possible, grandparent and parent) then copy that file to a new folder making sure to not copy or create any new folders in this destination?

As a bonus, could this check if a file already exists so that if I ran it in the future only new files will be copied?

The file structure for the library is pretty simple. \Music\Artist\Album title\cover.jpg

1
  • use G-CI with a filter of cover.jpg & recurse on to get all the files. ///// make a new name by taking as much of the dir path as you want and combining that with the .Name property. ///// copy the old file to the new location with the new name. Commented Jan 3, 2021 at 21:03

1 Answer 1

3

If you have a music library structure like that, the easiest way would be to use the properties Directory and Parent each FileInfo object returned by Get-ChildItem contains:

$sourcePath  = 'C:\Music'
$destination = 'C:\Destination'

# if the destination folder does not already exist, create it
if (!(Test-Path -Path $destination -PathType Container)) {
    $null = New-Item -Path $destination -ItemType Directory
}


Get-ChildItem -Path $sourcePath -Filter '*.jpg' -File -Recurse | ForEach-Object {
    $newName = '{0}_{1}_{2}' -f $_.Directory.Parent.Name, $_.Directory.Name, $_.Name
    $_ | Copy-Item -Destination (Join-Path -Path $destination -ChildPath $newName)
}
Sign up to request clarification or add additional context in comments.

1 Comment

You're a genius! I had done a bit more work this afternoon and managed to get the correct number of pictures copied over but it was the same one each time. They did all have different names though. $source = "C:\Music" $destination = "C:\Destination" Get-ChildItem -Path $source -Filter *.jpg -Recurse | % { $filename1 = $_.Directory.Name+'_'+$file.Basename+'.jpg' Copy-Item $file.FullName -Destination $dest -PassThru | Rename-Item -NewName $filename1 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.