简记:清理指定后缀名文件的 powerhsell 小脚本

清理指定后缀名文件的 powerhsell 小脚本

jcLee95https://blog.csdn.net/qq_28550263?spm=1001.2101.3001.5343
邮箱 :291148484@163.com
本文地址https://blog.csdn.net/qq_28550263/article/details/129121074

1.介绍

2.目录结构

3.主函数部分

4.正则匹配压缩文件后缀名 - 筛选压缩文件

5.由路径删除文件,失败时提示

6.完整 clear_zip_7Z.ps1 代码


1.介绍

在运维值班每天都需要从系统导出这种数据,压缩好放在工作电脑上,解压用脚本做汇总。但是长期都没删除各个日期下的压缩包。几年下来,有上千个目录,也不知道哪些目录中有没有删除的压缩包。一个一个手删太累了,不妨做个 powershell 小脚本一键搞定吧。

这个脚本同样适合于删除目录中其它后缀名的文件。

相关工具代码

后文用到了两个工具模块,这两个模块在我之前的两篇博客中有完整介绍和源代码:

2.目录结构

项目结构

在这里插入图片描述

日志结构

在这里插入图片描述
在这里插入图片描述

3.主函数部分

为了以后方便指定仅针对某个年份,或者修改为其它需要考虑年份的脚本,可以不要直接读取日志根目录中所有文件,而是逐个遍历所有年份下的文件夹:

function main($BASE_DIR) {
    $logger.Trace("Start compute...")
    $logger.Trace($BASE_DIR)
    $items = [Path]::get_items($BASE_DIR)
    foreach ($item in $items) {  
        Write-host -ForegroundColor 'Gray' "=========================================================================================================="
        # 对于目录
        $logger.Info("Current folder is: " + $item)
        if ([Path]::is_dirname($item)) {
            write-host -ForegroundColor 'DarkBlue' "=====> This is a folder!"
            foreach ($item in [Path]::get_descendants($item) ) {
                write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$item
                if (IsArchiveFile($item)) {
                    write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove:"
                    write-host -ForegroundColor 'Green' "  "$item
                    deleteFileByPath($item)
                }
                else {
                    write-host -ForegroundColor 'Gray' " - But not a archive file"
                }
            }
            # 对于文件
        }
        elseif ([Path]::is_filename($item)) {
            write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$item
            if (IsArchiveFile($item)) {
                write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove!"
                write-host -ForegroundColor 'Green' "  "$item
                deleteFileByPath($item)
            }
            else {
                write-host -ForegroundColor 'Gray' " - But not a archive file"
            }
          
        }
    }
}

4.正则匹配压缩文件后缀名 - 筛选压缩文件

function IsArchiveFile([string]$file_path) {
    $is7z = $file_path -match "\.7z$"
    $isZip = $file_path -match "\.zip$"
    $isTar = $file_path -match "\.tar$"
    $isWim = $file_path -match "\.wim$"
    $isAechiveFile = $is7z -or $isZip -or $isTar -or $isWim
    return $isAechiveFile
}

5.由路径删除文件,失败时提示

function deleteFileByPath([string]$file_path) {
    try{
        Remove-Item -Path $file_path
        write-host -ForegroundColor 'Blue' "Ok, this file has been removed!"
        return $True
    }catch{
        Write-Warning "An error occurred While delete file:"
        Write-Host $_
        return $False
    }
}

6.完整 clear_zip_7Z.ps1 代码

<#
@Author: Jack Lee;
@Email: 291148484@163.com;
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
using module .\utils\jcpath.psm1
using module .\utils\jclogger.psm1

$PRODUCTION_BASE_DIR = [Path]::get_dirpath([Path]::get_dirpath($MyInvocation.MyCommand.source))
$logger = [Logger]::new([Path]::Join($PRODUCTION_BASE_DIR, 'logs'));

function IsArchiveFile([string]$file_path) {
    $is7z = $file_path -match "\.7z$"
    $isZip = $file_path -match "\.zip$"
    $isTar = $file_path -match "\.tar$"
    $isWim = $file_path -match "\.wim$"
    $isAechiveFile = $is7z -or $isZip -or $isTar -or $isWim
    return $isAechiveFile
}

function deleteFileByPath([string]$file_path) {
    try {
        Remove-Item -Path $file_path
        write-host -ForegroundColor 'Blue' "Ok, this file has been removed!"
        return $True
    }
    catch {
        Write-Warning "An error occurred While delete file:"
        Write-Host $_
        return $False
    }
}

function main($BASE_DIR) {
    $logger.Trace("Start compute...")
    $logger.Trace($BASE_DIR)
    $items = [Path]::get_items($BASE_DIR)
    foreach ($item in $items ) {  
        Write-host -ForegroundColor 'Gray' "=========================================================================================================="
        # 对于目录
        $logger.Info("Current folder is: " + $item)
        if ([Path]::is_dirname($item)) {
            write-host -ForegroundColor 'DarkBlue' "=====> This is a folder!"
            foreach ($item in [Path]::get_descendants($item) ) {
                write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$item
                if (IsArchiveFile($item)) {
                    write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove:"
                    write-host -ForegroundColor 'Green' "  "$item
                    deleteFileByPath($item)
                }
                else {
                    write-host -ForegroundColor 'Gray' " - But not a archive file"
                }
            }
            # 对于文件
        }
        elseif ([Path]::is_filename($item)) {
            write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$item
            if (IsArchiveFile($item)) {
                write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove!"
                write-host -ForegroundColor 'Green' "  "$item
                deleteFileByPath($item)
            }
            else {
                write-host -ForegroundColor 'Gray' " - But not a archive file"
            }
          
        }
    }
}

main($PRODUCTION_BASE_DIR)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在TypeScript中移除数组中的指定项,有几种常见的方法可以使用。 一种方法是使用`splice`方法,它可以删除数组中的指定项。`splice`方法接受两个参数,第一个参数是要删除的项的索引,第二个参数是要删除的项的数量。例如,如果要删除数组中的第三项,可以使用`splice`方法如下所示: ```typescript array.splice(2, 1); ``` 这将从数组中删除索引为2的项(即第三项)。 另一种方法是使用`filter`方法。`filter`方法可以创建一个新的数组,其中包含符合指定条件的项。您可以使用`filter`方法过滤掉要删除的项。例如,如果要删除值为`"apple"`的项,可以使用`filter`方法如下所示: ```typescript array = array.filter(item => item !== "apple"); ``` 这将创建一个新的数组,其中不包含值为`"apple"`的项,并将其赋值给原始数组。 还有其他一些方法可以实现相同的效果,这取决于具体的情况和您的需求。以上是两种常见的方法供参考。引用引用了一个相关的问题和讨论,您可以在该链接中找到更多关于在TypeScript中删除数组项的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [如何在TypeScript中删除数组项?](https://blog.csdn.net/asdfgh0077/article/details/107040642)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [TypeScript简记(二)](https://blog.csdn.net/Maca_Baca/article/details/126320687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jcLee95

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值