none
Find all files within a folder that has been modified in X Days RRS feed

  • Dotaz

  • Hi,

    Wonder if you can help me,

    I am looking at finding the way to find the way to get if any file's and subfolders within a folder if they haven't been modified within X Days, then to copy the item to another location

    So we have Folder A > Subfolder A > File A> 
    File A has has been modified within the last 2 days then do not move Folder A

    This has files and Subfolders and Files within this. If any child file or folder hasn't been amended, then its needed to copy whole parent and all child items to another location.

    This is what i have come up with but it copies the files and leaves the child items that have been modified/

    get-childitem -Path "FileLocation" -Recurse -file | where-object {$_.LastWriteTime -lt (get-date).AddDays(-300)} | move-item -destination "Destinationlocation" " -force | ForEach-Object{ if(Test-Path $Destination$($_.Name)){Remove-Item $_ -Force}} 


    pátek 18. října 2019 12:34

Všechny reakce

  • Hi Claire,

    You should remove '-File' from Get-ChildItem command, because it lists only files not directories. Removing '-File' property will list all files and folders.

    pátek 18. října 2019 13:13
  • Yes i tried this.

    In this example:

    Folder A > Subfolder A > File A -  Change the contents of File A. This would only modify the data modified on Subfolder A and not Folder A

    so won't help. i need to see any file within each of the subfolders and if they have been modified then skip the parent folder.


    pátek 18. října 2019 13:20
  • Correct, then you can add more condition using Get-Item to check the Parent folder:

    if (Get-Item "FileLocation" | where-object { $_.LastWriteTime -lt (get-date).AddDays(-300) }) {
        Move-item -Path "FileLocation" -destination "Destinationlocation" -force | 
            ForEach-Object { 
                if (Test-Path $Destination$($_.Name)) {
                    Remove-Item $_ -Force
                } } 
    }
    else {
        get-childitem -Path "FileLocation" -Recurse -file | 
            where-object { $_.LastWriteTime -lt (get-date).AddDays(-300) } | 
            move-item -destination "Destinationlocation" -force | 
            ForEach-Object { 
                if (Test-Path $Destination$($_.Name)) {
                    Remove-Item $_ -Force
                } } 
    }

    pátek 18. října 2019 14:05
  • Please explain above.

    pátek 18. října 2019 14:26
  • Correct, then you can add more condition using Get-Item to check the Parent folder:

    if (Get-Item "FileLocation" | where-object { $_.LastWriteTime -lt (get-date).AddDays(-300) }) {
        Move-item -Path "FileLocation" -destination "Destinationlocation" -force | 
            ForEach-Object { 
                if (Test-Path $Destination$($_.Name)) {
                    Remove-Item $_ -Force
                } } 
    }
    else {
        get-childitem -Path "FileLocation" -Recurse -file | 
            where-object { $_.LastWriteTime -lt (get-date).AddDays(-300) } | 
            move-item -destination "Destinationlocation" -force | 
            ForEach-Object { 
                if (Test-Path $Destination$($_.Name)) {
                    Remove-Item $_ -Force
                } } 
    }

    Can you explain here?

    I think i need to set a variable of the datemodified of each folder and then compare against each childitem and then skip it?

    pondělí 21. října 2019 8:53
  • Hi Claire, Sorry for delay in reply. In above script block, first we are checking the parent folder using Get-Item. If that condition is false then go for else part to check sub folders and files using Get-ChildItem.

    According to your question if any parent folder or their sub folders and files has not been modified in X days then move the parent folder to another location.

    Correct me if I am wrong.

    pondělí 21. října 2019 13:13
  • Try below:

    $destination = <PATH OF THE DESTINATION>
    
    
    $FoldersToBeMoved = GCI <PATH OF THE SOURCE FOLDER i.e. FOLDER A> -File -Recurse  |
                                    ?{$_.LastWriteTime -gt (Get-date).AddDays(-300)} | 
                                            Select name, @{n="Path"; e={($_.FullName -replace "$($_.Name)", "")}}
    
     
    $FoldersToBeMoved | Move-Item -Destination $destination -Force


    úterý 22. října 2019 14:17
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    pátek 8. listopadu 2019 13:36
    Moderátor