使用 PowerShell 清理文件夹

发布于 2024-08-29 23:03:11 字数 328 浏览 6 评论 0原文

我想在脚本运行后通过从当前目录中删除某些文件夹和文件(如果存在)来清理某些目录。最初,我是这样构建脚本的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扭转时空 2024-09-05 23:03:11
# if you want to avoid errors on missed paths
# (because even ignored errors are added to $Error)
# (or you want to -ErrorAction Stop if an item is not removed)
@(
    'Directory1'
    'Directory2'
    'File1'
) |
Where-Object { Test-Path $_ } |
ForEach-Object { Remove-Item $_ -Recurse -Force -ErrorAction Stop }
# if you want to avoid errors on missed paths
# (because even ignored errors are added to $Error)
# (or you want to -ErrorAction Stop if an item is not removed)
@(
    'Directory1'
    'Directory2'
    'File1'
) |
Where-Object { Test-Path $_ } |
ForEach-Object { Remove-Item $_ -Recurse -Force -ErrorAction Stop }
提笔落墨 2024-09-05 23:03:11
Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        %{
            if ($_.PSIsContainer) {
                rm -rec $_ #  For directories, do the delete recursively
            } else {
                rm $_ #  for files, just delete the item
            }
        }

或者,您可以为每种类型创建两个单独的块。

Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        ?{ $_.PSIsContainer } |
            rm -rec

Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        ?{ -not ($_.PSIsContainer) } |
            rm
Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        %{
            if ($_.PSIsContainer) {
                rm -rec $_ #  For directories, do the delete recursively
            } else {
                rm $_ #  for files, just delete the item
            }
        }

Or, you could do two separate blocks for each type.

Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        ?{ $_.PSIsContainer } |
            rm -rec

Folder1, Folder2, File1, Folder3 |
    ?{ test-path $_ } |
        ?{ -not ($_.PSIsContainer) } |
            rm
彻夜缠绵 2024-09-05 23:03:11

一种可能性

function ql {$args}

ql Folder1 Folder2 Folder3 File3 |
    ForEach {
        if(Test-Path $_) {
            Remove-Item $_
        }
    }

One possibility

function ql {$args}

ql Folder1 Folder2 Folder3 File3 |
    ForEach {
        if(Test-Path $_) {
            Remove-Item $_
        }
    }
够钟 2024-09-05 23:03:11
# if you do not mind to have a few ignored errors
Remove-Item -Recurse -Force -ErrorAction 0 @(
    'Directory1'
    'Directory2'
    'File1'
)
# if you do not mind to have a few ignored errors
Remove-Item -Recurse -Force -ErrorAction 0 @(
    'Directory1'
    'Directory2'
    'File1'
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文