当前位置: 首页 > news >正文

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

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

jcLee95:https://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 小脚本一键搞定吧。

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

相关工具代码

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

  • 《【运维】PowerShell编程 目录文件相关方法的封装与案例详解》

  • 《[运维技术]PowerShell中实现一个最基本的日志器logger》

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!"$itemif (IsArchiveFile($item)) {write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove:"write-host -ForegroundColor 'Green' "  "$itemdeleteFileByPath($item)}else {write-host -ForegroundColor 'Gray' " - But not a archive file"}}# 对于文件}elseif ([Path]::is_filename($item)) {write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$itemif (IsArchiveFile($item)) {write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove!"write-host -ForegroundColor 'Green' "  "$itemdeleteFileByPath($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 $isWimreturn $isAechiveFile
}

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

function deleteFileByPath([string]$file_path) {try{Remove-Item -Path $file_pathwrite-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 $isWimreturn $isAechiveFile
}function deleteFileByPath([string]$file_path) {try {Remove-Item -Path $file_pathwrite-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!"$itemif (IsArchiveFile($item)) {write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove:"write-host -ForegroundColor 'Green' "  "$itemdeleteFileByPath($item)}else {write-host -ForegroundColor 'Gray' " - But not a archive file"}}# 对于文件}elseif ([Path]::is_filename($item)) {write-host -ForegroundColor 'DarkGreen' "=> This is a file!"$itemif (IsArchiveFile($item)) {write-host -ForegroundColor 'Yellow' " * This is a archive file, and it will be remove!"write-host -ForegroundColor 'Green' "  "$itemdeleteFileByPath($item)}else {write-host -ForegroundColor 'Gray' " - But not a archive file"}}}
}main($PRODUCTION_BASE_DIR)
http://www.lryc.cn/news/13649.html

相关文章:

  • 问题记录:mac系统偏好设置不展示mysql
  • 网络计划--时间参数的计算和优化
  • 1.2.7存储结构-磁盘管理:磁盘移臂调度算法、先来先服务(FCFS)、最短寻道时间优先(SSTF)、扫描算法(SCAN)、循环扫描(CSCAN)
  • 2022年AI顶级论文 —生成模型之年(上)
  • Linux下程序调试的方法【GDB】GDB相关命令和基础操作(命令收藏)
  • 使用frp配置内网机器访问
  • 简述7个流行的强化学习算法及代码实现!
  • 朗润国际期货招商:地方政府工作报告中对于促进消费
  • 前端性能优化的一些技巧(90% chatGpt生成)
  • [软件工程导论(第六版)]第8章 维护(复习笔记)
  • Python - 绘制人体生物节律
  • 【NVMEM子系统】二、NVMEM驱动框架
  • 小波神经网络(WNN)的实现(Python,附源码及数据集)
  • 商标干货!所有企业都值得收藏!
  • 4次迭代,让我的 Client 优化 100倍!泄漏一个 人人可用的极品方案!
  • 并查集(高级数据结构)-蓝桥杯
  • 你是真的“C”——C语言详解求两个正数最小公倍数的3种境界
  • 【java】Spring Cloud --Feign Client超时时间配置以及单独给某接口设置超时时间方法
  • spark代码
  • 利用OpenCV的函数equalizeHist()对图像作直方图均衡化处理
  • 星河智联Android开发
  • 【C++】关联式容器——map和set的使用
  • Promise的实现原理
  • 【MFC】数据库操作——ODBC(20)
  • 旺店通与金蝶云星空对接集成采购入库单接口
  • Linux基础-学会使用命令帮助
  • MyBatis 之四(动态SQL之 if、trim、where、set、foreach 标签)
  • PAT (Advanced Level) Practice 1006 Sign In and Sign Out
  • Android入门第64天-MVVM下瀑布流界面的完美实现-使用RecyclerView
  • Windows PowerShell中成功进入conda虚拟环境