header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Delete backed up files through PS
Last Post 21 Jul 2010 06:27 AM by swanlee. 10 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
swanleeUser is Offline
New Member
New Member
Posts:7
Avatar

--
19 Jul 2010 01:36 PM
    I need to delete backed up files older than 10 days and then remove the now empty folder they were in. I currently have everything working accept the part where it determines if the file has been backed up using the A attribute on the file.

    any suggestions for this part? I'm stumped

    I've uploaded what I have so far that works.

    test_-_Copy.txt

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    19 Jul 2010 02:15 PM

    Try this:

    $dir = "C:\Temp"
    $tenDaysAgo = (Get-Date).AddDays(-10)

    if (Test-Path -Path $dir)
    {
        # I'm guessing you need to check the attribute here?
        Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
            ($_.LastWriteTime -le $tenDaysAgo) -and
            (($_.Attributes -band [System.IO.FileAttributes]::Archive) -ne [System.IO.FileAttributes]::Archive)
        } | ForEach-Object {
            Write-Host "Deleting " -NoNewLine
            Write-Host $_.FullName -ForegroundColor Blue
            Remove-Item -Path $_.FullName
        }
    }
    else
    {
        Write-Host "$dir does not exist." -ForegroundColor Red
    }

    Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
        if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
            Remove-Item -Path $_.FullName -Verbose
        }
    }



    swanleeUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    20 Jul 2010 08:26 AM
    Thanx for your help I think you are on the right track however when I run your adjusted version it still only delete's files older than 10 days even if they have not been archived. For my need it needs to skip files that have not been backed up even if they are older than 10 days.

    For testing the files I'm using are 10 days old and all seem to have a file attribute of AI, while on the server where this will be run the files will be over 10 days old and if they have been backed up will have a file attribute of A.

    General purpose is to clean up some groove backup files and folders as longs as those files have been backed up to tape.

    Thank you for your help so far.



    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    20 Jul 2010 10:21 AM
    Yes, I made a mistake:

    (($_.Attributes -band [System.IO.FileAttributes]::Archive) -ne 0)

    ...should be:

    (($_.Attributes -band [System.IO.FileAttributes]::Archive) -ne [System.IO.FileAttributes]::Archive)

    I edited by original post, try that.


    swanleeUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    20 Jul 2010 12:45 PM
    Hmm getting more interesting but this version ends up not deleting anything, even the files that are older than 10 days that I have changed the file attribute to A.

    I tried adjusting the attribute field you used to Archived or just A and that seems invalid and then it just nukes them by date instead.
    I verified it kept the old date modified time on the files after changing the attribute to them to A.

    I knew this part would suck, the part you adjusted do I need to add some type of get attribute part like I did with the $dir and $date?


    0ptikGhostUser is Offline
    Basic Member
    Basic Member
    Posts:296
    Avatar

    --
    20 Jul 2010 03:02 PM

    Try it like this:

    $dir = "C:\Temp" $tenDaysAgo = (Get-Date).AddDays(-10) if (Test-Path -Path $dir) { # I'm guessing you need to check the attribute here? Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object { $_.LastWriteTime -le $tenDaysAgo } | Where-Object { $_.Attributes -band [System.IO.FileAttributes]::Archive } | ForEach-Object { Write-Host "Deleting " -NoNewLine Write-Host $_.FullName -ForegroundColor Blue Remove-Item -Path $_.FullName } } else { Write-Host "$dir does not exist." -ForegroundColor Red } Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object { if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) { Remove-Item -Path $_.FullName -Verbose } }


    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    21 Jul 2010 02:16 AM

    So, you want to delete files that:

    - Are 10 days old or older AND have been archived (have the 'A' attribute)

    Right? If so:

    $dir = "C:\Temp"
    $tenDaysAgo = (Get-Date).AddDays(-10)

    if (Test-Path -Path $dir)
    {
        Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
            ($_.LastWriteTime -le $tenDaysAgo) -and
            (($_.Attributes -band [System.IO.FileAttributes]::Archive) -eq [System.IO.FileAttributes]::Archive)
        } | ForEach-Object {
            Write-Host "Deleting " -NoNewLine
            Write-Host $_.FullName -ForegroundColor Blue
            Remove-Item -Path $_.FullName
        }
    }
    else
    {
        Write-Host "$dir does not exist." -ForegroundColor Red
    }

    Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
        if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
            Remove-Item -Path $_.FullName -Verbose
        }
    }



    swanleeUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    21 Jul 2010 05:16 AM
    Posted By GWHowarth88 on 21 Jul 2010 02:16 AM

    So, you want to delete files that:

    - Are 10 days old or older AND have been archived (have the 'A' attribute)

    Right? If so:

    $dir = "C:\Temp"
    $tenDaysAgo = (Get-Date).AddDays(-10)

    if (Test-Path -Path $dir)
    {
        Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
            ($_.LastWriteTime -le $tenDaysAgo) -and
            (($_.Attributes -band [System.IO.FileAttributes]::Archive) -eq [System.IO.FileAttributes]::Archive)
        } | ForEach-Object {
            Write-Host "Deleting " -NoNewLine
            Write-Host $_.FullName -ForegroundColor Blue
            Remove-Item -Path $_.FullName
        }
    }
    else
    {
        Write-Host "$dir does not exist." -ForegroundColor Red
    }

    Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
        if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
            Remove-Item -Path $_.FullName -Verbose
        }
    }

    This one deleted files with the A attribute but also with the AI attribute.



    swanleeUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    21 Jul 2010 05:21 AM
    The one optighost came up with leave files with the A attribute in place for some reason




    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    21 Jul 2010 06:05 AM

    So, you want to delete files that:

    - Are 10 days old or older AND have only been archived (have the 'A' attribute and nothing else)

    $dir = "C:\Temp"
    $tenDaysAgo = (Get-Date).AddDays(-10)

    if (Test-Path -Path $dir)
    {
        Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
            ($_.LastWriteTime -le $tenDaysAgo) -and
            ($_.Attributes -eq [System.IO.FileAttributes]::Archive)
        } | ForEach-Object {
            Write-Host "Deleting " -NoNewLine
            Write-Host $_.FullName -ForegroundColor Blue
            Remove-Item -Path $_.FullName
        }
    }
    else
    {
        Write-Host "$dir does not exist." -ForegroundColor Red
    }

    Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
        if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
            Remove-Item -Path $_.FullName -Verbose
        }
    }



    swanleeUser is Offline
    New Member
    New Member
    Posts:7
    Avatar

    --
    21 Jul 2010 06:27 AM
    Posted By GWHowarth88 on 21 Jul 2010 06:05 AM

    So, you want to delete files that:

    - Are 10 days old or older AND have only been archived (have the 'A' attribute and nothing else)

    $dir = "C:\Temp"
    $tenDaysAgo = (Get-Date).AddDays(-10)

    if (Test-Path -Path $dir)
    {
        Get-ChildItem -Path $dir -Filter "*.gsa" -Recurse | Where-Object {
            ($_.LastWriteTime -le $tenDaysAgo) -and
            ($_.Attributes -eq [System.IO.FileAttributes]::Archive)
        } | ForEach-Object {
            Write-Host "Deleting " -NoNewLine
            Write-Host $_.FullName -ForegroundColor Blue
            Remove-Item -Path $_.FullName
        }
    }
    else
    {
        Write-Host "$dir does not exist." -ForegroundColor Red
    }

    Get-ChildItem -Path $dir | Where-Object { $_.PsIsContainer } | ForEach-Object {
        if (($_.GetFiles().Length -eq 0) -and ($_.GetDirectories().Length -eq 0)) {
            Remove-Item -Path $_.FullName -Verbose
        }
    }


    Awesome this one worked perfectly, thanx for your help,


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer