2011-10-10 29 views
3

在Windows shell中,您可以使用類似copy c:\filename NUL的命令獲取文件的內容並將其複製到「\ Device \ Null」。 (這是回顧外部歸檔文件,而無需浪費空間或touch更新有用的。)

但使用$nullNUL\\.\NUL多(我想不出如何做相同的PowerShell和我不t想要調出一個單獨的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 

任何其他的想法如何做到這一點?

+0

這是因爲'$ null'不是Powershell中的設備/驅動器本身。猜猜'Out-Null'將不會起作用。但是我仍然沒有真正明白你想要複製到$ null的目標,你能否詳細說明一下? –

+0

某些歸檔軟件會將實際文件數據保存到另一個遠程文件系統/數據庫/磁帶,但留下標有NTFS「脫機」屬性的小型存根。當用戶獲取文件的*內容*(而不僅僅是文件屬性)時,軟件從存檔中獲取並恢復本地文件。因此......恢復所有存根/脫機文件的最簡單方法是將它們複製到某處 - 但複製到另一個文件系統需要永久(然後您必須刪除所有廢話),而將它們複製到NUL更快。 – ewall

回答

3

實際上,您只是想對文件進行讀取。如果是這樣,這將工作:

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

這可能是矯枉過正,但。你可以嘗試:

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

這只是打開文件閱讀(這可能足以讓它回來),並再次關閉它。

+0

/我打額頭......你的回答很有道理。謝謝,@craika! – ewall