【问题标题】:How do I write a Powershell script that checks when the last time a file was added to a folder?如何编写一个 Powershell 脚本来检查上次将文件添加到文件夹的时间?
【发布时间】:2019-10-27 03:48:36
【问题描述】:

我目前正在编写一个脚本,用于检查目录中的每个文件夹最后一次将文件写入每个文件夹的时间。我无法弄清楚如何获取文件最后一次写入文件夹的时间,而不是仅检索文件夹的创建日期。

我尝试过使用 Poweshell 的递归方法,但不知道如何正确设置它。现在,脚本成功将每个文件夹的名称打印到 Excel 电子表格中,并且还打印了每个文件夹的最后写入时间,这是不正确的信息。

$row = 2

$column = 1

Get-ChildItem "C:\Users\Sylveon\Desktop\Test"| ForEach-Object {     

    #FolderName
    $sheet.Cells.Item($row,$column) = $_.Name
    $column++
    #LastBackup
    $sheet.Cells.Item($row,$column) = $_.LastWriteTime
    $column++
    #Increment to next Row and reset Column
    $row++
    $column = 1
}

脚本的当前状态会将每个文件夹名称打印到报告中,但会给出文件夹的创建日期,而不是最后一次将文件写入该文件夹的时间。

【问题讨论】:

  • 您确定项目自创建后就已添加到目录中?我有很多文件夹具有非常旧的 lastwritetime 值,因为它们从未被使用过
  • 我发布了一个答案,解决了如何获取编辑文件夹中项目的最新日期,但我很确定这不是一个完整的答案。完成后,您究竟希望 Excel 电子表格是什么样子 - 给定目录中每个目录的日期? - 还是您只是使用电子表格查看脚本返回的内容?
  • 我想我明白你的目的,检查我编辑的答案:)

标签: powershell loops recursion foreach report


【解决方案1】:

如果您只查看每个文件夹中的第一级文件,则可以使用嵌套循环:

$row = 2
$column = 1

$folders = Get-ChildItem $directorypath
ForEach ($folder in $folders) { 

    # start off with LastEdited set to the last write time of the folder itself
    $LastEdited = $folder.LastWriteTime
    $folderPath = $directoryPath + '\' + $folder.Name
    # this 'dynamically' sets each folder's path

    $files = Get-Childitem $folderPath
    ForEach ($file in $files) {
        if ((Get-Date $file.LastWriteTime) -gt (Get-Date $LastEdited)) {
            $LastEdited = $file.LastWriteTime
        }
    }

    $sheet.Cells.Item($row,$column) = $folder.Name
    $column++
    $sheet.Cells.Item($row,$column) = $LastEdited
    $row++
    $column = 1
}

【讨论】:

    【解决方案2】:

    以下内容应该可以获取当前目录中任何文件的最新编辑日期。

    Get-ChildItem | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 -ExpandProperty "LastWriteTime"
    

    Get-ChildItem 获取目录中的项目

    Sort-Object -Property LastWriteTime -Descending 按写入时间排序,最新在前

    Select-Object -first 1 -ExpandProperty "LastWriteTime" 获取列表中的第一个,然后获取其写入时间


    我这样做是为了获取您想要获取的数据。如果目录为空,最后一行给我们一个空字符串,这对 Excel 来说可能是最安全的,但您也可以默认为空字符串以外的其他内容,例如目录的创建日期:

    $ChildDirs = Get-ChildItem | Where-Object { $_ -is [System.IO.DirectoryInfo] }
    $EditNames = $ChildDirs | ForEach-Object Name
    $EditTimes = $EditNames | ForEach-Object { @( (Get-ChildItem $_ | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 LastWriteTime), '' -ne $null)[0] }
    
    for($i=0; $i -lt $ChildDirs.Length; $i++) {
        Write-Output $EditNames[$i]
        Write-Output $EditTimes[$i]
    }
    
    

    要为您正在做的事情实施此操作,如果我正确理解您的问题,请尝试以下操作:

    $ChildDirs = Get-ChildItem | Where-Object { $_ -is [System.IO.DirectoryInfo] }
    $EditNames = $ChildDirs | ForEach-Object Name
    $EditTimes = $EditNames | ForEach-Object { @( (Get-ChildItem $_ | Sort-Object -Property LastWriteTime -Descending | Select-Object -first 1 LastWriteTime), '' -ne $null)[0] }
    
    for($i=0; $i -lt $ChildDirs.Length; $i++) {
        #FolderName
        $sheet.Cells.Item($row, $column) = $EditNames[$i]
        $column++
        #LastBackup
        $sheet.Cells.Item($row, $column) = $EditTimes[$i]
        $row++
        $column = 1
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      相关资源
      最近更新 更多