简体   繁体   中英

Powershell 2.0 Issue with script to Restore folders from backup location

I have a backup Folder F:\\DATA\\01172014 - this folder has five sub dirs \\Folder1, \\Folder2, Folder3, Folder4, Folder5

F:\\DATA\\01172014.. ..\\Folder1 ..\\Folder2 ..\\Folder3 ..\\Folder4 ..\\Folder5

I want to copy the the folders 1 through 5 to the E:\\Main location overwriting the existing folders

E:\\Main ..\\Folder1 ..\\Folder2 ..\\Folder3 ..\\Folder4 ..\\Folder5

My problem is when I run the script it finds the 01172014 folder and copy's it to the E:\\Main as the same name and does not overwrite the old folders

E:\\Main ..\\01172014 ..\\Folder1 ..\\Folder2 ..\\Folder3 \\Folder4 ..\\Folder5

My question is what am I missing

Here is my code I am using :-

Get-ChildItem -Path F:\DATA -r | 
Where-object {$_.PSIscontainer -and (($_.lastwritetime.date -eq ((get-date).adddays(-1)).date))}  |
% { Copy-Item  $_.fullName -destination E:\Main\ -force -R -whatif}

You're telling it to copy the date-stamped folder and contents, and it is.

Try something like this instead which will copy the contents of the date-stamped folder and its sub-folders, but not the folder itself ( -Container switch retains the structure):

$BackupDir = Get-ChildItem ("F:\DATA\" + (((get-date).AddDays(-1).ToString("MMddyyyy"))))|%{$_.FullName}  

Copy-Item $BackupDir -Destination ("E:\Main\" + (Split-Path $BackupDir -leaf)) -R -Container -Force -WhatIf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM