# Moving older folders to archive

**URL:** <https://forums.powershell.org/t/moving-older-folders-to-archive/10388>\
**Category:** PowerShell Help\
**Created:** [March 26, 2018, 3:26am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10388 "2018-03-26T03:26:48Z")\
**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, 3:26am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10388/1 "2018-03-26T03:26:48Z")

</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
[/PRE]

Any help would be greatly appreciated
```

---

<div class="post-metadata">

**Author:** ![emiltanev](https://avatars.discourse-cdn.com/v4/letter/e/5e9695/32.png) [@emiltanev](https://forums.powershell.org/u/emiltanev)\
**Post date:** [March 27, 2018, 4:08am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10388/2 "2018-03-27T04:08:33Z")

</div>

There are a few points here:

1. (Get-Date).AddDays(-31) - the bracket
2. Elseif(!(Test-Path $Destenation)){
3. $Directory is a collection, so you should use a foreach (e.g. $item.FullName)
4. You could try combining the conditions with -and ($directory -ine $null)
5. You may want to filter or use -File in line 4 above, depending on the directory content.

---

<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 28, 2018, 12:45am UTC](https://forums.powershell.org/t/moving-older-folders-to-archive/10388/3 "2018-03-28T00:45:36Z")

</div>

Hi Emil, thanks for your advise

I was thinking of do it like that

```
Param(
[String]$Source = 'C:\Vitrea\Patients',
[String]$Destenation = 'D:\PatientsArchive',
[String]$Date = (Get-Date).AddDays(-31),
[String]$Directory = (Get-ChildItem $Source -Recurse | Where {$_.CreationTime -lt $Date})

      )#End Param
    
    if( -not (Test-Path $Destenation)){
       New-Item -ItemType Directory -Path $Destenation
                                       }#end if
    Else{
    Move-Item -Path $Directory -Destination $Destenation    
        }#end Else
```

I’m not quite sure how foreach must looks in this script. I will put my suggestion down below because you are right: $Directory is a folder that contains subfolders which contains DICOM-Images. Also thank you for all your tips about brackets and conditions.

```
Param(
[String]$Source = 'C:\Vitrea\Patients',
[String]$Destenation = 'D:\PatientsArchive',
[String]$Date = (Get-Date).AddDays(-31),
[String]$Directory = (Get-ChildItem $Source -Recurse | Where {$_.CreationTime -lt $Date})

      )#End Param
    
    if( -not (Test-Path $Destenation)){
       New-Item -ItemType Directory -Path $Destenation
                                       }#end if
    Else{
        ForEach ($Folder in $Directory) {
        Move-Item -Path $Folder -Destination $Destenation
                                         }#ForEach    
        }#end Else
```

---

<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/10388/4 "2024-05-16T20:36:03Z")

</div>


