删除windows指定目录下文件

AI摘要
Deepseek提供支持
# 加载 Windows 窗体程序集
Add-Type -AssemblyName System.Windows.Forms
  • 这行代码加载 System.Windows.Forms 程序集,以便使用 Windows 窗体功能,包括弹出消息框。
# 检查当前用户是否具有管理员权限
function Test-Admin {
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
    return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}
  • 这里定义了一个名为 Test-Admin 的函数,用于检查当前用户是否具有管理员权限。
  • GetCurrent() 方法获取当前用户的身份,WindowsPrincipal 用于确定该用户的角色。
  • IsInRole 方法检查用户是否属于管理员角色。
# 如果不是管理员,则以管理员身份重新启动脚本
if (-not (Test-Admin)) {
    Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}
  • 这段代码检查用户是否为管理员。如果不是,使用 Start-Process 启动一个新的 PowerShell 进程,以管理员身份重新运行该脚本。
  • -NoProfile 表示不加载用户的 PowerShell 配置文件,-ExecutionPolicy Bypass 允许脚本执行而不受执行策略的限制。
  • $PSCommandPath 代表当前脚本的路径。
# 设置目标路径
$cachePath = ""
  • 这行定义了一个变量 $cachePath,指向要清空的目录。
# 检查目录是否存在
if (Test-Path $cachePath) {
  • 使用 Test-Path 检查指定路径是否存在。
    try {
        # 删除目录下的所有文件
        Remove-Item "$cachePath\*" -Recurse -Force
  • 如果目录存在,try 块中的代码会执行,使用 Remove-Item 删除 $cachePath 目录下的所有文件和子目录。
  • -Recurse 表示递归删除子目录和文件,-Force 强制删除(即使是只读文件)。
        # 弹出完成提示框
        [System.Windows.Forms.MessageBox]::Show("已删除所有文件。", "删除完成", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
  • 如果删除成功,使用 MessageBox.Show 弹出一个消息框,显示“已删除所有文件”。
    } catch {
        [System.Windows.Forms.MessageBox]::Show("删除文件时发生错误:$_", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
  • 如果在删除过程中发生错误,catch 块会捕获异常,并弹出一个错误消息框,显示错误信息。
} else {
    [System.Windows.Forms.MessageBox]::Show("目录不存在。", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
}
  • 如果目录不存在,弹出一个警告消息框,提示用户目录不存在。

这个脚本的主要功能是检查用户权限,如果没有管理员权限则请求以管理员身份重新运行,然后删除指定目录下的所有文件,并在操作完成后弹出相应的提示框。

完整代码

# 加载 Windows 窗体程序集
Add-Type -AssemblyName System.Windows.Forms

# 检查当前用户是否具有管理员权限
function Test-Admin {
    $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object System.Security.Principal.WindowsPrincipal($identity)
    return $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
}

# 如果不是管理员,则以管理员身份重新启动脚本
if (-not (Test-Admin)) {
    Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
    exit
}

# 设置目标路径
$cachePath = ""

# 检查目录是否存在
if (Test-Path $cachePath) {
    try {
        # 删除目录下的所有文件
        Remove-Item "$cachePath\*" -Recurse -Force
        # 弹出完成提示框
        [System.Windows.Forms.MessageBox]::Show("已删除所有文件(程序安装文件,请放心!。", "删除完成", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
    } catch {
        [System.Windows.Forms.MessageBox]::Show("删除文件时发生错误:$_", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
} else {
    [System.Windows.Forms.MessageBox]::Show("目录不存在。", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Warning)
}

下载脚本

释放空间

释放空间

可下载
资源版权归原作者所有,下载即表示您同意相关条款
广告
广告图片
温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;来自:俄亥俄州·哥伦布 ,欢迎您的访问!
文章链接:https://www.lilianhua.com/delete-files-in-the-specified-directory-of-windows.html
请先登录才能参与答题
距本场结束剩 00 00 00 00
轻量应用服务器 2核2G
200M峰值带宽,适用于网站搭建、Web应用、容器环境、电商独立站等
立即前往
扫码进入
扫描二维码购买
文澜千文

文澜千文

请登录以使用此功能。

和情(LOTUS)Biscoff比利时进口焦糖饼干节日送礼铁罐红色礼盒312G 和情(LOTUS)Biscoff比利时进口焦糖饼干节日送礼铁罐红色礼盒312G
Loading...
OpenClaw

OpenClaw 龙虾

AI AGENT GATEWAY
在您的服务器部署 OpenClaw,打造专属极速 AI 助手。
零门槛一键部署环境
支持 QQ/飞书/微信 渠道
跨平台指令分发执行
立即安装