# Moving older folders to archive

**URL:** <https://forums.powershell.org/t/moving-older-folders-to-archive/10389>\
**Category:** PowerShell Help\
**Created:** [March 26, 2018, 4:30am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10389 "2018-03-26T04:30:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dimis](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@dimis](https://forums.powershell.org/u/dimis)\
**Post date:** [March 26, 2018, 4:30am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10389/1 "2018-03-26T04:30:02Z")

</div>

Hi all PowerShell users!  
I’m completely noob in powershell so sorry if my question will sound “very stupid” for you.

Situation:  
I have a folder that contains patients DICOM-images and I need to move all folders that are older than 31 day to archive in external drive.

I know that’s sound simple but I’m stuck a little.

```
$Source = 'C:\Vitrea\Patients'
$Destenation = 'D:\PatientsArchive'
$Date = (Get-date.AddDays(-31))
$Directory = Get-ChildItem $Source -Recurse | Where {$_.CreationTime -lt $Date}
Move-Item -Path $Directory -Destination $Destenation
```

I’m was thinking of using If…Else construct in way like:  
If Archive folder exists and if there is some patients to archive then just do it  
Else if folder doesn’t exists then create it and archive patients there  
Else if folder exists and it’s no patients to archive just don’t do anything.

Problem is…I just don’t know how to do this. A little thought about that:

```
$Source = 'C:\Vitrea\Patients'
$Destenation = 'D:\PatientsArchive'
$Date = (Get-date.AddDays(-31))
$Directory = Get-ChildItem $Source -Recurse | Where {$_.CreationTime -lt $Date}
    if(Test-Path $Destenation){
    Write-Host "Archiving now"
    Move-Item -Path $Directory -Destination $Destenation
                               }#End if
        Elseif(Test-Path $Destenation){
        New-Item -ItemType Directory -Path $Destenation
        Move-Item -Path $Directory -Destination $Destenation
                                       }#End elseif
    Else{
    Write-Host "Nothing to archive"
        }# end Else
```

Any help would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [March 26, 2018, 11:57am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10389/2 "2018-03-26T11:57:15Z")

</div>

I would say the If/Else there is going to be a bit cumbersome, mainly because you gotta re-run your script to make sure everything is good. Instead, split them up a bit:

```
if (-not (Test-Path $FolderPath)) {
    New-Item -Type Directory -Path $FolderPath
}
# DO things to archive patients here
```

---

<div class="post-metadata">

**Author:** ![dimis](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@dimis](https://forums.powershell.org/u/dimis)\
**Post date:** [March 27, 2018, 12:37am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10389/3 "2018-03-27T00:37:03Z")

</div>

Thank you very much Joel!  
Now this scripts looks like:

```
$Source = 'C:\Vitrea\Patients'
$Destenation = 'D:\PatientsArchive'
$Date = Get-Date
$Directory = Get-ChildItem $Source -Recurse | Where {$_.CreationTime -lt $Date.AddDays(-31)}
    
    if( -not (Test-Path $Destenation)){
       New-Item -ItemType Directory -Path $Destenation
                                     }#end if
    Else{
    Move-Item -Path $Directory -Destination $Destenation    
        }#end Else
```

Will think a little more about how to make it look more “adult”, but for now it’s OK 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10389/4 "2024-05-16T20:36:07Z")

</div>


