2014-02-07 46 views
0

我想更改文件夾lastWriteTime而不修改此目錄中文件的lastWriteTime。在Powershell中修改LastWriteTime目錄

我一直想這樣的:

$a = Get-Date "02/09/2013 4:59 PM" 
$d = "C:\Users\user\Documents\CV" 
$d.LastWriteTime = $a 

,但它並沒有工作,因爲d有沒有LastWriteTime屬性。 有什麼辦法可以改變這個屬性?我必須修改lastTimeWrite屬性,只有「CV」目錄 - 不應該觸摸「CV」中的文件。

回答

3

一種方法是將它轉換爲[system.io.directoryinfo]

$a = Get-Date "02/09/2013 4:59 PM" 
$d = [system.io.directoryinfo]"C:\Users\user\Documents\CV" 
$d.LastWriteTime = $a 

或使用get-item cmdlet的

$a = Get-Date "02/09/2013 4:59 PM" 
$d = get-item C:\Users\user\Documents\CV 
$d.LastWriteTime = $a 
+0

@mjolinor我看到它像一個錯誤。我已經tryed這是V2。 0,我很好奇在v3.0中測試它,現在我不能... –

+0

@CB。 :謝謝,第一個工作:) –

+0

@mjolinor在本機V2的作品像一個魅力,後來我會在V3上測試它,並給這裏反饋 –

-1
Function Touch-File 
#Touches a file or folder if the file does not exist it creates a new empty file 
    { 
    $file = $args[0] 
    $date = $args[1] 

    if($file -eq $null) { 
     throw "No filename supplied" 
    } 

    if($date -eq $null) { 
     throw "No date supplied" 
    } 

    if(Test-Path $file) 
    { 
    #Optionally select the date stamp you want to update 

     (Get-Item $file).LastWriteTime = $date 
     (Get-Item $file).CreationTime = $date 
     (Get-Item $file).LastAccessTime =$date 
    } 
    else 
    { 
     echo $null > $file 
    } 
}