2016-01-04 45 views
0

此代碼確實從文件夾中獲取最新的文本文件。如何將Txt/Text文件導出到新文件夾?

$dir = "C:\logsnew\Application" 
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1 
$latest.name 

出這樣

C:\Users\kimi> $dir = "C:\logsnew\Application" 
C:\Users\kimi> $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1 
C:\Users\kimi> $latest.name 
4552-4084-63585921993.txt 

我願把這個最新的txt文件 「4552-4084-63585921993.txt」 新文件夾名稱 「logstop1」。

因此,我想是這樣的:

$dir = "C:\logsnew\Application" 
Get-ChildItem -Path $dir | 
    Sort-Object LastAccessTime -Descending | 
    Select-Object -First 1 | 
    Add-Content C:\logsTop1 

但這種錯誤發生:

添加內容:訪問路徑 'C:\ logsTop1' 被拒絕。

我該如何解決這個問題?

+0

添加內容添加到現有的文件。您正在尋找移動項目 – dfundako

回答

4

使用Move-Item而不是Add-Content如果要移動文件或Copy-Item如果要複製它。

Get-ChildItem -Path "C:\logsnew\Application" | Sort-Object LastAccessTime -Descending | Select-Object -First 1 | Move-Item -Destination "C:\logspath1" 
+0

@Bluescakes。感謝yooooooooooooooooooooooou so muchhhhhhhhhh !!!!!!!! – Kazu

相關問題