H
H
hardwellZero2015-05-22 15:17:09
PowerShell
hardwellZero, 2015-05-22 15:17:09

How to delete the necessary files/folders?

Hello PowerShell experts.
There was a question about deleting files on a remote machine.
The connection to it has already been implemented in Python, now I want to add the launch of PowerShell and the execution of the command to remove it.
Need help building a team.
Criteria:

  • Delete all files in a specific directory. For example (D://Example/)
  • Exclusion for a folder with a specific name. For example (folder)
  • Deleting only those files whose "lifetime" is more than a certain number of hours. "Time to live" = (Current date/time) - (Date/time the file was created)

I found this as an example:
$DT=New-Object DateTime
Get-ChildItem E:\Backup_Folder | Where {[DateTime]::TryParse($_,[ref]$DT)} | Where {[DateTime]::ParseExact($_,'yyyy-MM-dd',$null) -lt (Get-Date).AddDays(-60)}| Del -recurse -WhatIf

Here, according to the author, deleting only folders with names like 2009-11-21, 2010-03-05 and which are more than 40 days old.
I encountered PowerShell for the first time, help me compose a team. Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
maaGames, 2015-05-22
@maaGames

The author lies modestly.)
This script will only write that it will delete files older than 60 days from the current day.
But if you remove "-WhatIf" from the script, then it will delete it already ... If you have the rights to run unsigned scripts and have enough rights to delete files from the specified folder.

T
TomosBlack, 2015-05-30
@TomosBlack

Wrote a simple universal function.

Function Del-Old{
    Param(
        #target path. Enter one string
        $TargetPath,
        #Enter one or many (array) exclude files (just names, not full address)
        $ExcludePath = @(),
        #Enter number of life time hours objects. Oldest objects will delete
        [int]$TimeLifeHours
    )
    
    $TimeLimit = (Get-Date).AddHours(-$TimeLifeHours)
    
    $ObjectsForDel = dir -Path $TargetPath -Exclude $ExcludePat | ?{$_.CreationTime -lt $TimeLimit}
    
    foreach ($obj in $ObjectsForDel){
        del -Path $obj.FullName -Recurse
    }
}

M
Mikhail Tchervonnko, 2019-02-22
@RusMikle

the DelT program can easily handle your task.
Example:
delt e:\Example /PT /OF *.doc /NOD /RW /DTS 02/19/2019 /DTE 02/19/2019 /NOF *\folder\*
Delete all created/modified 02/19/2019 files with *.doc mask without transferring them to the recycle bin after overwriting their contents according to the 5220.22-M standard, in folders and subfolders, do not delete the folders themselves. Do not delete all files in folders.
The /RW key is most likely not needed in your case (overwriting before deleting).
If you want to first see what will be deleted without deleting itself, use the /T key.
If you want, see the /TM key with the number of hours and lifetime
.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question