0

I have code that looks for folders and afterwards it looks for the criteria I am telling it. So far so good. Then I got a ForEach-Object with a Copy-Item $_.Fullname in it. The code itself is working and spitting no errors, but I don't want to copy the folder itself + its content. I just want to copy the content of the folder. I already tried things like $_.Fullname/* and so on.

Here's how my code looks like:

Get-ChildItem -Path $DestinationBackup | Where {
    $_.Name -like "$BackupName_Differential3*"
} | ForEach-Object {
    Copy-Item -Path $_.FullName -Recurse -Destination $Differential_Destination -Force
}

Edit 1:

My Folder structure looks like this:

M143                       ← (Root Folder)
├─Backups                  ← (Folder, here are Backups stored)
│ ├─Fullbackup...
│ ├─Incrementalbackup...   ← Copy Content of this folder, without folder)
│ └─Differentialbackup...  ← (Into this folder)
└─Backup_Script.ps1
5
  • 1
    Get-ChildItem -Path "$DestinationBackup\*" | ...? Commented Jan 23, 2018 at 21:07
  • I tried your answer (With "/" and with " \"), but it's still doing the same as before. Commented Jan 23, 2018 at 21:55
  • 1
    Please show a sample of your directory structure and indicate what exactly you want copied. Commented Jan 23, 2018 at 22:35
  • Do you have multiple incremental backup folders or just one? Does your source folder contain only files or subfolders as well? Also, why would you want to copy incremental backups to a folder for differential backups in the first place? Commented Jan 24, 2018 at 11:24
  • I got a folder called "incremental backup". in this folders are for every incremental backup folders like "[Backupname]-Incremental-Date-Time". My Source Folder only contains files at the moment, but it should also work with subfolders. I am copying incremental backups to a folder for differential backups, because I am testing an "alternate" way for differential backup. (No RoboCopy, not allowed to use) Commented Jan 24, 2018 at 11:33

1 Answer 1

0

This ...

Copy-Item -Path $_.FullName -Recurse

... is your issue. The below should be what you wanted.

(Get-ChildItem -Path E:\BackUps -Recurse).FullName

Results before Copy

E:\BackUps\Differential
E:\BackUps\Full
E:\BackUps\Incremental
E:\BackUps\Incremental\IncBU001.bkp
E:\BackUps\Incremental\IncBU002.bkp
E:\BackUps\Incremental\IncBU003.bkp

Copy-Item -Path 'E:\BackUps\Incremental\*' -Destination 'E:\BackUps\Differential'

(Get-ChildItem -Path E:\BackUps -Recurse).FullName

Results after copy

E:\BackUps\Differential
E:\BackUps\Full
E:\BackUps\Incremental
E:\BackUps\Differential\IncBU001.bkp
E:\BackUps\Differential\IncBU002.bkp
E:\BackUps\Differential\IncBU003.bkp
E:\BackUps\Incremental\IncBU001.bkp
E:\BackUps\Incremental\IncBU002.bkp
E:\BackUps\Incremental\IncBU003.bkp
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I think that might do the trick. I just don't seem to implement it correct in my code. My code looks at the moment like this: $SearchBackup = (Get-ChildItem -Path E:\Backups -Recurse).FullName $SearchBackup | Where {$SearchBackup -like "$BackupName_Differential3*" | ForEach-Object { Copy-Item -Path 'E.\Backups\Incremental\*' -Destination 'E:\Backups\Differential' }
Thank you so much! I realised my mistake. Your Solution was: Copy-Item -Path 'I:\TBZ\M143\Backups\IncrementalBackup\*' -recurse -Destination $Differential_Destination -Force I wrote the following and now it works: Copy-Item -Path 'I:\TBZ\M143\Backups\IncrementalBackup\*\*' -recurse -Destination $Differential_Destination -Force So I just had to add an extra \asterik

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.