简体   繁体   English

使用powershell从特定日期删除文件夹内的文件

[英]Deleting files inside the folder from specific date using powershell

My folder structure looks like this:我的文件夹结构如下所示:

AK_Data |--->2020-01-22-------|----->a.txt
        |                     |----->b.txt
        |                     |------>c.tf.err  
        |
        |--->2021-01-15-------|----->d.txt
        |                     |----->b.txt
        |                     |------>x.tf.err
        | 
        |--->2022-01-08-------|----->dv.txt
        |                     |----->pq.bat
        |
        |--->2022-05-08-------|----->xyz.pdf

AK_Data is the first folder which consists of multiple subfolders like (2020-01-22,2021-01-15.....) and these each subfolders contains 1000 of their different format files. AK_Data是第一个包含多个subfolders ,例如(2020-01-22,2021-01-15.....) ,每个子文件夹包含1000不同格式的文件。

I need to go into each folder and delete all the files except those files having ".tf.err" in their names.我需要进入每个文件夹并删除所有文件,但名称中".tf.err"的文件除外。 I have used $specificdate='2022-01-01' means the files of the folder less than 2022-01-01 only needs to be deleted.我使用$specificdate='2022-01-01'意味着只需要删除小于2022-01-01的文件夹的文件。 So my expected output is:所以我的预期输出是:

AK_Data |--->2020-01-22-------|------>c.tf.err 
        |                     
        |                      
        |
        |--->2021-01-15-------|------>x.tf.err
        |                     
        |                     
        | 
        |--->2022-01-08-------|----->dv.txt  (this folder will be untouched since>2022-01-01)
        |                     |----->pq.bat
        |
        |--->2022-05-08-------|----->xyz.pdf (this folder will be untouched since>2022-01-01)

I only need to delete the files inside the folder, not the folder.我只需要删除文件夹内的文件,而不是文件夹。

The powershell I used for this is:我用于此的 powershell 是:

cls
$specificdate='2022-01-01'
$destination = 'Y:\Data\Retail\ABC\Development\ak\AK_Data'
$files = Get-ChildItem $destination

$exte='\.(bz2)|(.bat)|(.err)|(~)'
   
foreach ($f in $files){
$outfile = $f.FullName -notmatch $exte
if ($outfile){
if ($f.Name -lt $specificdate){
$allfiles=Get-ChildItem $f.FullName -Exclude *.tf.err* -Name | remove-item -whatif

}
}
}

It is not deleting the files.它不是删除文件。

I would do this by first iterating the source folder path for directories with a name that can be converted to a datetime less than the datetime in variabe $specificDate .我会通过首先迭代目录的源文件夹路径来做到这一点,该目录的名称可以转换为小于变量$specificDate中的日期时间的日期时间。

Then use Get-ChildItem again inside these folders to find and remove files that do not have .tf.err in their name:然后在这些文件夹中再次使用Get-ChildItem来查找并删除名称中没有.tf.err的文件:

$specificdate = [datetime]::ParseExact('2022-01-01','yyyy-MM-dd', $null)
$sourceFolder = 'Y:\Data\Retail\ABC\Development\ak\AK_Data\*'

Get-ChildItem -Path $sourceFolder -Directory | 
Where-Object { [datetime]::ParseExact($_.Name,'yyyy-MM-dd', $null) -lt $specificdate } |
ForEach-Object {
    Write-Host "Removing files from folder $($_.Name).."
    Get-ChildItem -Path $_.FullName -File |
    Where-Object { $_.Name -notlike '*.tf.err*' } |
    Remove-Item -WhatIf
}

Again here, I added the -WhatIf switch so you can first see what WOULD happen.再次在这里,我添加了-WhatIf开关,这样您就可以首先看到发生什么。 If you're OK with that, remove -WhatIf and run the code again to actually delete the files如果您同意,请删除-WhatIf并再次运行代码以实际删除文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM