【问题标题】:Modifying LastWriteTime directory in Powershell在 Powershell 中修改 LastWriteTime 目录
【发布时间】:2014-02-07 14:37:53
【问题描述】:

我想更改文件夹 lastWriteTime 而不修改此目录中文件的 lastWriteTime。

我一直在尝试这个:

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

但它不起作用,因为d 没有 LastWriteTime 属性。 有什么办法可以改变这个属性?我必须只修改 lastTimeWrite 属性的“CV”目录 - 不应触及“CV”中的文件。

【问题讨论】:

    标签: powershell directory windows-7-x64 propertychanged


    【解决方案1】:

    一种方法是将其转换为[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
    

    【讨论】:

    • @mjolinor 我认为它像一个错误.. 我试过这是 v2.0,我很想在 v3.0 中测试它,现在我不能......跨度>
    • @mjolinor 在原生 V2 上效果很好,稍后我将在 V3 上对其进行测试并在此反馈
    • @CB 不确定发生了什么变化,但我关闭了我正在使用的会话,打开了一个新会话,现在它可以工作了。在我试图更改的目录中可能会弄乱它。
    • @mjolinor 可以!在 v2 上也适用于被更改的目录。
    • @mjolinor fyi,我使用 v4 并使用 (get-item $filename).lastwritetime=(get-date "some date") 没有任何问题
    【解决方案2】:
    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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多