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

  • السؤال

  • 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}} 


    19/صفر/1441 12:34 م

جميع الردود

  • 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.

    19/صفر/1441 01: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.


    19/صفر/1441 01: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
                } } 
    }

    19/صفر/1441 02:05 م
  • Please explain above.

    19/صفر/1441 02: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?

    22/صفر/1441 08: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.

    22/صفر/1441 01: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


    23/صفر/1441 02: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.

    11/ربيع الأول/1441 01:36 م
    المشرف