none
Delete files from Level 3 Directories but not Level 4 RRS feed

  • Question

  • Hi Everyone, 

    I had help creating the below code, but now I've added a sub-folder and need (more) help updating the code to skip level 4 directories (these will always be name Archive).  So the new folder structure is like this:

    D:\HBFTP\LocalUser\123
    D:\HBFTP\LocalUser\123\Archive

    I want the code to delete files from D:\123 (which it already does), but for the D:\123\Archive folder to either skip it entirely or allow me to specify another skip value.

        $HBDir = 'D:\HBFTP\LocalUser\'
    
        Get-ChildItem $HBDir -Recurse -Directory |
            foreach{
            Get-ChildItem $_.FullName -File |
            sort -Property LastWriteTime -Descending |
            select -Skip 14 |
            Remove-Item} 

    Thanks,

    Kevin

    Friday, June 30, 2017 11:08 AM

Answers

All replies

  • I would suggest using the -Exclude parameter of Get-ChildItem.

    -- Bill Stewart [Bill_Stewart]

    Friday, June 30, 2017 1:05 PM
    Moderator
  • Bill-

    Yes that did it!  New code below:

        $HBDir = 'D:\HBFTP\LocalUser\'
    
        Get-ChildItem $HBDir -Recurse -Directory -Exclude 'Archive' |
            foreach{
            Get-ChildItem $_.FullName -File |
            sort -Property LastWriteTime -Descending |
            select -Skip 14 |
            Remove-Item}
    


    Friday, June 30, 2017 2:03 PM