powershell - 如何删除 PowerShell 说不存在的文件?

标签 powershell delete-file

我将我的 Google Drive 添加到我的 OneDrive 并在其中包含一个包含无效名称 (con.mp3) 的文件。当我尝试删除文件(及其所在的目录)时,我收到“无效的文件句柄”。所以我尝试以管理员身份使用 PowerShell 删除它。

这是显示文件的目录列表,以及 Remove-Item 的结果和 del .

PS> dir

    Directory: C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/13/2018  11:49 AM         117069 con.mp3


PS> Remove-Item * -Force
Remove-Item : An object at the specified path C:\Users\Patrick\OneDrive\Google
Drive\CW\CW.15WPM.VeryShortWords\con.mp3 does not exist.
At line:1 char:1
+ Remove-Item * -Force
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveItemCommand

PS> del *.*
del : An object at the specified path C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3 does
not exist.
At line:1 char:1
+ del *.*
+ ~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveItemCommand

如何删除此文件,以便删除目录?我尝试从 Google Drive 中删除它,但它没有同步到我的电脑。

最佳答案

保留字con不应用作路径/文件名(或其中的一部分)。

您必须使用 -LiteralPath参数并最终以 \\?\ 为前缀
删除时。

所以尝试:

Remove-Item -LiteralPath "\\?\C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3" -Force

如果这不起作用,您可以在 cmd 窗口中尝试:
Del "\\?\C:\Users\Patrick\OneDrive\Google Drive\CW\CW.15WPM.VeryShortWords\con.mp3"

如果这没有帮助,请阅读:
https://support.microsoft.com/en-us/help/320081/you-cannot-delete-a-file-or-a-folder-on-an-ntfs-file-system-volume

关于powershell - 如何删除 PowerShell 说不存在的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50858832/

相关文章:

c# - 删除文件失败,因为现有进程持有

powershell - Powershell将电子邮件发送给Excel电子表格中的收件人

azure - 无法识别“New-AzSqlServerFirewallRule” - Azure Dev Ops Pipeline 的 Azure PowerShell 问题

git - 从所有提交中删除旧的别名和电子邮件

xcode - 如何删除 Xcode 项目中所有未引用的文件?

c++ - 在 Windows 中删除文件时 CMD 和 PowerShell 有什么区别?

excel - 在没有 Office 的服务器上对 Excel 文件执行 PowerShell 操作(打开和使用密码另存为)

javascript - 是否可以通过 Javascript 终止 SharePoint 工作流?

PowerShell Switch 语句问题

java - 无法在JAVA中删除文件