PowerShell 复制到 $null
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,您只想读取该文件。如果是这样,这会起作用:
但这可能有点矫枉过正。您可以尝试:
这只是打开文件进行读取(这可能足以将其恢复)并再次关闭它。
Effectively, you just want to do a read on the file. If so, this will work:
It's probably overkill though. You could try:
This just opens the file for reading (which may be enough to bring it back) and closes it again.