在PowerShell中,如何判断目录是否删除”。 OP会成功吗?

发布于 2025-01-22 18:07:29 字数 885 浏览 0 评论 0 原文

我正在编写一个powershell脚本,如果可能的话,如果可能的子任务(我无法控制,除了调用它之外)无法正确删除它,偶尔会删除偶尔的外部目录。

发现目录(具有很多文件和递归层次结构)可能会被另一个(大型第三方)程序使用或可能不会使用。通常,我正在Windows Explorer中这样做:我点击了“删除”键,我得到了一个“使用”对话框,耸耸肩并继续前进,没有造成伤害。 (这是一个很大的过程,通常会自行清理。)

not 想要做的是调用 remove -item -recursive ,并清除一半目录然后发现正在使用一些随机文件。我什至不确定这是使用文件问题;也许是因为目录(或子目录)是某个过程的当前目录,或者出于某种原因,目录(或子目录)本身是开放的(我在这一点上只是在做东西),或者某些完全不同且神秘的原因。

我希望复制当前的手动过程。

谁能向我指向正确的方向?基本上,我正在寻找删除directory-if-you-can,但do-do-do thything-at-all-you-can't


(编辑)

请注意,如果目录仍在某种意义上说,我不想删除目录中目录中的任何内容。这将使用目录破坏大型过程。如果大型过程已经完成并继续前进,我可以删除目录。


(编辑)

这个?

递归地称呼吗? (不确定它是否适用于分子,但我可以尝试。)

I'm writing a PowerShell script that will, if possible, delete an occasional extraneous directory if a subtask (over which I have no control, apart from invoking it) fails to properly delete it.

The catch is that the directory (which has a lot of files and recursive hierarchy) may or may not be in use by another (large, 3rd party) program. Normally, I'm doing this in Windows Explorer: I hit the "delete" key, I get a "in use" dialog, shrug and move on, no harm done. (It's a big process and it usually cleans up after itself.)

What I don't want to do is call Remove-Item -Recursive and have it purge half a directory and then discover that some random file is in use. Nor am I even sure it's a file-in-use issue; maybe it's because the directory (or a subdirectory) is some process's current directory or, for some reason, the directory (or subdirectory) itself is open (I'm just making stuff up at this point), or some entirely different and mysterious cause.

I'm hoping to duplicate the current manual process.

Can anyone point me in the right direction? Basicaly, I'm looking for Remove-Directory-If-You-Can-But-Don't-Do-Anything-At-All-If-You-Can't.


(Edit)

Note that I don't want to remove anything in the directory if the directory is still "in use", in some sense. That will corrupt the large process using the directory. If the large process has finished and moved on, I can delete the directory.


(Edit)

This? https://github.com/pldmgg/misc-powershell/blob/master/MyFunctions/PowerShellCore_Compatible/Get-FileLockProcess.ps1

Called recursively? (Not sure if it'll work for subdirs, but I can experiment.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

童话 2025-01-29 18:07:29

可以尝试首先将物品移开,并恢复如果出现任何问题,这些物品已成功移动。

如果您将事物移至“临时文件”路径中的文件夹中,则不必删除它们 - 系统将在您的流程发布新文件夹上的句柄后的某个时候将其删除。

简单的情况:

$tempRoot = Split-Path (New-TemporaryFile).FullName
$tempDir  = New-Item "$tempRoot\$(New-Guid)" -Type Directory

Move-Item "$HOME\Downloads\SomeFolder\" $tempDir

如果在移动的文件夹中拿着文件句柄,它将失败,并且不会发生移动。

删除多个文件和文件夹:

$tempRoot = Split-Path (New-TemporaryFile).FullName
$tempDir  = New-Item "$tempRoot\$(New-Guid)" -Type Directory

try {
    Move-Item "$HOME\Downloads\*" $tempDir.FullName -ErrorAction Stop
    Remove-Variable tempDir
} catch {
    Move-Item "$($tempDir.FullName)\*" "$HOME\Downloads"
}

MOVE-ETEM 将如上所述移动整个文件夹,因此无需递归。

假设您要移动的文件/文件夹与TEMP文件夹相同的驱动器,则移动操作应该很快。文件本身不必在磁盘上物理移动。操作系统只是修改表中的条目。移动整个文件夹只需要一个更新(其中每个文件中的每个文件都不需要一个)。

如果以上不满足您的需求,请查看本文:如何用powershell管理打开文件手柄

Could try moving the items away somewhere safe first, and restore items that were successfully moved if anything goes wrong.

If you move things to a folder in the path for temp files, you don't have to delete them - the system will delete them sometime after your process releases the handle on the new folder.

Simple case:

$tempRoot = Split-Path (New-TemporaryFile).FullName
$tempDir  = New-Item "$tempRoot\$(New-Guid)" -Type Directory

Move-Item "$HOME\Downloads\SomeFolder\" $tempDir

If anything is holding a file handle in the folder being moved, it will fail and the move won't occur.

Deleting several files and folders:

$tempRoot = Split-Path (New-TemporaryFile).FullName
$tempDir  = New-Item "$tempRoot\$(New-Guid)" -Type Directory

try {
    Move-Item "$HOME\Downloads\*" $tempDir.FullName -ErrorAction Stop
    Remove-Variable tempDir
} catch {
    Move-Item "$($tempDir.FullName)\*" "$HOME\Downloads"
}

Move-Item will move whole folders as given above, so no need for recursion.

The move operation should be quick, assuming the files/folders you're moving are on the same drive as the temp folder. The files themselves don't have to be physically moved on disk. The OS just modifies an entry in a table. Moving an entire folder only requires one update (not one for each file within it).

If the above doesn't satisfy your needs, maybe check out this article: How to Manage Open File Handles with PowerShell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文