删除windows指定目录下文件
由
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
文章链接:https://www.lilianhua.com/delete-files-in-the-specified-directory-of-windows.html
English (US)
Español (ES)
Português (PT)
Français (CA)
Español (MX)
Español (VE)
Español (CO)
Español (AR)
Português (BR)
Quechua (PE)
Guaraní (PY)
简体中文 (ZH)
繁體中文 (HK)
日本語 (JP)
한국어 (KR)
हिन्दी (HI)
Pilipino (PH)
ไทย (TH)
Tiếng Việt (VN)
Bahasa Melayu (MY)
Bahasa Indonesia (ID)
বাংলা (BD)
اردو (PK)
සිංහල (LK)
ភាសាខ្មែរ (KH)
English (UK)
Français (FR)
Deutsch (DE)
Italiano (IT)
Русский (RU)
Nederlands (NL)
Türkçe (TR)
Polski (PL)
Svenska (SE)
Norsk (NO)
Dansk (DK)
Suomi (FI)
Ελληνικά (GR)
Čeština (CZ)
Magyar (HU)
Română (RO)
Български (BG)
Српски (RS)
Українська (UA)
