none
Create an exception for deleting files RRS feed

  • Question

  • Hello everyone!

    I need some help with the script below.
    I used the variable $put so it lists the files from the last 30 days.

    And right after, I need to remove all files, except for files that are within the $PUT period.

    Can someone help me?

    Thank you!

    #Variable created to store script event logs
    $LogsEvent = "C:\Script\log.txt"
    
    
    echo "Perform backup copy of Zabbix database to backup server"; >> $LogsEvent
    #Comand below performs the Backup copy and the -Recurse parameter "Force" copying files already in the directory
    Copy-Item -Path \\1SRVBACKUP\backup\* -Destination E:\zabbix_backup\ -Recurse >> $LogsEvent
    echo "Copy of backup performed successfully"; >> $LogsEvent
    
    #Variable will read the backup directory and check the files of the last 29 days
    $put = Get-ChildItem -Path E:\zabbix_backup\| Sort-Object -Property LastWriteTime | Select-Object -Last 29 >> $LogsEvent
    
    #Command will delete everything inside the directory except for the files of the last 29 Days.
    echo "Files longer than 29 days will be removed from the directory"
    Remove-Item -Exclude $put -Path  E:\zabbix_backup\* -Recurse >> $LogsEvent
    echo "Verification performed successfully"
    
    $data=Get-Date
    
    echo " Script Rounds$data " >> $LogsEvent
    
    echo "#############################################" >> $LogsEvent


    Analista de Suporte N I


    Analista de Suporte N I

    Friday, April 20, 2018 2:40 PM

Answers

  • $put = Get-ChildItem -PathE:\zabbix_backup\* | Where{ $_.LastWriteTIme -lt [datetime]::Today.AddDAys(-30) }


    \_(ツ)_/

    Friday, April 20, 2018 2:48 PM