PowerShell 复制到 $null

发布于 2024-12-09 06:02:11 字数 1477 浏览 0 评论 0原文

在 Windows shell 中,您可以使用 copy c:\filename NUL 等命令获取文件的内容并将其复制到“\Device\Null”。 (这对于调用外部存档的文件而不浪费空间或使用 touch 进行更新非常有用。)

但我不知道如何在 PowerShell 中使用 $null 执行相同的操作、NUL\\.\NUL 等(我不想调用单独的 CMD.EXE 进程来为每个文件执行此操作)。

PS C:\> Copy-Item -Path  .\filename -Destination NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  -Path  .\filename -Destination NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> Copy-Item .\filename NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  .\filename NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> copy .\filename '\\.\NUL'
Copy-Item : Cannot process path '\\.\NUL' because the the target represents a reserved device name.
At line:1 char:5
+ copy <<<<  .\filename '\\.\NUL'
    + CategoryInfo          : WriteError: (\\.\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

还有其他想法如何做到这一点吗?

In Windows shell you can fetch the contents of a file and copy it to "\Device\Null" with a command like copy c:\filename NUL. (This is useful for recalling externally-archived files without wasting space or updating with touch.)

But I can't figure out how to do the same in PowerShell using $null, NUL, \\.\NUL and more (and I don't want to call out a separate CMD.EXE process to do this for every file).

PS C:\> Copy-Item -Path  .\filename -Destination NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  -Path  .\filename -Destination NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> Copy-Item .\filename NUL
Copy-Item : Cannot process path 'C:\NUL' because the the target represents a reserved device name.
At line:1 char:10
+ Copy-Item <<<<  .\filename NUL
    + CategoryInfo          : WriteError: (C:\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:\> copy .\filename '\\.\NUL'
Copy-Item : Cannot process path '\\.\NUL' because the the target represents a reserved device name.
At line:1 char:5
+ copy <<<<  .\filename '\\.\NUL'
    + CategoryInfo          : WriteError: (\\.\NUL:String) [Copy-Item], IOException
    + FullyQualifiedErrorId : CopyError,Microsoft.PowerShell.Commands.CopyItemCommand

Any other ideas how to do this?

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

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

发布评论

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

评论(1

思慕 2024-12-16 06:02:11

实际上,您只想读取该文件。如果是这样,这会起作用:

Get-ChildItem .\filename | Get-Content | Out-Null

但这可能有点矫枉过正。您可以尝试:

$File=[system.io.file]::OpenRead(".\filename")
$File.Close()

这只是打开文件进行读取(这可能足以将其恢复)并再次关闭它。

Effectively, you just want to do a read on the file. If so, this will work:

Get-ChildItem .\filename | Get-Content | Out-Null

It's probably overkill though. You could try:

$File=[system.io.file]::OpenRead(".\filename")
$File.Close()

This just opens the file for reading (which may be enough to bring it back) and closes it again.

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