使用 PowerShell 清理文件夹
我想在脚本运行后通过从当前目录中删除某些文件夹和文件(如果存在)来清理某些目录。最初,我是这样构建脚本的:
if (Test-Path Folder1) {
Remove-Item -r Folder1
}
if (Test-Path Folder2) {
Remove-Item -r Folder2
}
if (Test-Path File1) {
Remove-Item File1
}
现在我在本节中列出了相当多的项目,我想清理代码。我怎样才能这样做呢?
旁注:这些项目在脚本运行之前被清理,因为它们是上次运行留下的,以防我需要检查它们。
I'd like to clean up some directories after my script runs by deleting certain folders and files from the current directory if they exist. Originally, I structured the script like this:
if (Test-Path Folder1) {
Remove-Item -r Folder1
}
if (Test-Path Folder2) {
Remove-Item -r Folder2
}
if (Test-Path File1) {
Remove-Item File1
}
Now that I have quite a few items listed in this section, I'd like to clean up the code. How can I do so?
Side note: The items are cleaned up before the script runs, since they are left over from the previous run in case I need to examine them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者,您可以为每种类型创建两个单独的块。
Or, you could do two separate blocks for each type.
一种可能性
One possibility