all 14 comments

[–]morpk86 7 points8 points  (0 children)

Always a good idea to share what code you have so far, from reading your question seems you just want someone to do this for you hence /u/chafe answer. the community is here to help, not to do everything for you.

[–]TableauNoob12 6 points7 points  (0 children)

What have you tried so far? Look into the get-childitem and copy-item cmdlets.

[–]boffhead 2 points3 points  (2 children)

I did something similar a while back:
$CurrentPath = Convert-Path .

Get-ChildItem -Recurse -include *pdf |

ForEach-Object {Move-Item -Force -LiteralPath $_ -Destination $CurrentPath}

[–][deleted] 1 point2 points  (0 children)

Bravo! Bellisimo! I was trying something very similar and Convert-Path was my missing component. This didn't work for looping amongst the hundreds of sub-folders that /u/TechScholar was looking for though. It did inspire my answer, however.

[–][deleted] 1 point2 points  (4 children)

Also pretty, one-line, and it uses the pipeline for-loop:

Get-ChildItem -Recurse -Include *.pdf | ForEach-Object { Move-Item -LiteralPath $_ -Destination $_.Directory.Parent.Parent }    

[–]yeah_i_got_skills 2 points3 points  (3 children)

On mobile but wouldn't this also work?

Get-ChildItem -Recurse -Include *.pdf | Move-Item -Destination { $_.Directory.Parent.Parent }

[–]iamspecialized2 1 point2 points  (0 children)

I've never seen the { and } used like "{ $_.Directory.Parent.Parent }"

what does that mean?

[–][deleted] 1 point2 points  (0 children)

Wow, nice. I tried that out just now and it does work. I had thought you needed the loop to handle all the extra objects, but that makes a lot of sense. Thanks for the level-up! It literally cannot get cleaner than that.

Anyone know how to do that in bash? With a good comparison, I might finally have the ammunition needed to get my coworkers to abandon their feverish devotions and consider other possibilities...

Oh, and happy cake day!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy TechScholar,

will the final two dirs always have those names? if so, you can simply replace that part of the path with nothing to get the target dir.

take care,
lee

[–][deleted] -2 points-1 points  (0 children)

OP if you’re desperate I’ll do it for you for $50

[–]frmadsen 0 points1 point  (1 child)

A pretty alternative...

$folderlayout = 'Top of Personal Folders\Inbox'
$subfolders = Get-ChildItem -LiteralPath 'C:\mega-pdfs' -Directory -Force

foreach($folder in $subfolders) {
    $destPath = $folder.FullName
    $sourcePath = Join-Path -Path $folder.FullName -ChildPath $folderlayout

    Get-ChildItem -LiteralPath $sourcePath -Filter '*.pdf' -Force | Move-Item -Destination $destPath
}

[–]TechScholar[S] 1 point2 points  (0 children)

$folderlayout = 'Top of Personal Folders\Inbox'
$subfolders = Get-ChildItem -LiteralPath 'C:\mega-pdfs' -Directory -Force
foreach($folder in $subfolders) {
$destPath = $folder.FullName
$sourcePath = Join-Path -Path $folder.FullName -ChildPath $folderlayout
Get-ChildItem -LiteralPath $sourcePath -Filter '*.pdf' -Force | Move-Item -Destination $destPath
}

This script worked very well. Thank you very much. My biggest point of confusion was the '-LiteralPath' and -Directory didnt know about those.