0

We migrated to OneDrive and one of the computers copied folders and added the user name to the end of the folder while still keeping some parts of the folder.

Now we are left with 600 folders. Our base folder is here:

C:\Users\Jason\OneDrive - company files\Company Files

Within it we have all of our project folders which look like this

\(Job Name)\
\(Job Name)-Dave-HP\

Even the subfolders were hit

\(Job Name)\(Subfolder)
\(Job Name)-Dave-HP\(Subfolder)-Dave-HP

Both of these folders have data in them that needs to merge. The script will need to merge the folders and overwrite any duplicate files that exist.

Anyone have any ideas without manually deleting the end off of each folder and letting Explorer merge them? Running Windows 10.

5
  • You'll have to Move-Item then, are there subfolders also with a trailing -DAVE-HP or why do you -Recurse?
    – LotPings
    Commented May 31, 2019 at 13:35
  • Yes there are subfolders too that have the same suffix
    – Jason S
    Commented May 31, 2019 at 14:04
  • Then please provide a better example by editing your question. Especially if after removing the -Dave-HP are there also conflicting file names, and what to do then - overwrite,rename?
    – LotPings
    Commented May 31, 2019 at 14:11
  • I made an adjustment. Sorry about that. There is some company info in the folder names I am not able to include, but I can adjust the script directories to reflect the actual folder name
    – Jason S
    Commented May 31, 2019 at 14:34
  • The script leaves the "Dave-HP" folders still, but they are emptied and the data has successfully moved. I'm easily able to search for them and delete them.
    – Jason S
    Commented Jun 3, 2019 at 13:03

1 Answer 1

0
## Q:\Test\2019\05\31\SU_1443421.ps1
$BaseDir = 'C:\' # 'A:\Test' # 
$Appendix= '-Dave-HP'

Get-ChildItem $BaseDir -Recurse -File | Where-Object Directory -Match $Appendix |
ForEach-Object{
    $DestDir = $_.Directory -Replace $Appendix
    if(!(Test-Path $DestDir -PathType Container)){MD $DestDir |Out-Null }
    $_ | Move-Item -Destination $DestDir\ -Force #-whatif
}

Uncomment (remove #) the -WhatIf to first check what the PowerShell script would do.

Changed logic completely, the script will leave empty directories with trailing $Appendix untouched.

2
  • Check changed script.
    – LotPings
    Commented May 31, 2019 at 16:18
  • If you find an answer helpful you should consider to accept the answer✔ and/or vote up
    – LotPings
    Commented Jun 3, 2019 at 13:21

You must log in to answer this question.

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