【问题标题】:Powershell to output folder based on content lastwritetimePowershell根据内容lastwritetime输出文件夹
【发布时间】:2013-07-17 18:44:12
【问题描述】:

我正在寻找创建一个 powershell 脚本,该脚本将查看文件夹的内容以查找文件或任何文件的 lasttimewrite 并输出文件夹和时间戳。用于在具有 raoming 配置文件的终端服务器环境中识别配置文件,但文件夹的 lastwrite 时间是针对文件夹本身的,而每次用户登录时都会触摸并加上时间戳。

我已经尝试了以下

Get-ChildItem -Recurse | where-object {$_.lastwritetime -gt (get-date).addDays(-1)} |  Foreach-Object { $_.Directory } | sort-object name -descending | export-csv c:\lists.csv

我已经尝试了几次迭代,这似乎有效

get-childitem -recurse | where-object {$_.name -eq "Pending"} |
where-object {$_.lastwritetime -gt (get-date).addDays(-1)} | 
where-object {$_.PSIsContainer} |
foreach {$_.Parent} | foreach {$_.name}

现在唯一的问题是输出将 lastwritetime 从“待处理”文件夹中显示出来

【问题讨论】:

  • 您是否要获取文件或文件夹?请详细说明。
  • 我只想要文件夹名称,基本上是为了识别哪些用户在多少天内访问了他们的个人资料。但是说它的user1,user2,user3文件夹lastwritetime没有更新,因为它没有被修改,但是里面的内容说user.dat文件有一个时间戳,我可以用它来说它的lastwritetime在一天之内,所以我想要检查每个文件夹 /user.dat 的 lastwritetime 并从中显示 名称和 user.dat lastwritetime
  • get-childitem -recurse | where-object {$_.name -eq '待定'} | where-object {$_.lastwritetime -gt (get-date).addDays(-1)} | where-object {-not $_.PSIsContainer} | Foreach-Object { $_.Parent }
  • 所以你想打印成对的文件夹名称和这个目录中最近修改的文件的修改时间。对吗?
  • 是的,我想打印目录中最新文件的文件夹名称和修改时间,但我有一个可以检查的特定文件,或者在我的示例中,文件夹待定我'我希望输出说 userfolder1 7/17/13... userfolder3 7/16/13... 基于文件夹(或特定文件)的标准,其 lastwritetime 大于定义的时间段我正在努力with 不是使用递归来使它永远不会运行,而是可能使用变量 $path = get-childitem | foreach {$_.name}

标签: powershell


【解决方案1】:

我不完全确定,如果我的问题是正确的,但我的小脚本显示了所有项目,在最后一天更改了谁以及更改时间+目录:

$directories = (Get-ChildItem -Path C:\test -Directory).FullName
     $directories | foreach  {if (($_.lastwritetime -gt (get-date).adddays(-1)) -or ((get-childitem -path $_ -Recurse).lastwritetime -gt (get-date).adddays(-1))) {
         $itemname = (Get-ChildItem -Path $_ -Recurse | where {$_.LastWriteTime -gt (get-date).AddDays(-1)}).BaseName
         $itemtime = (Get-ChildItem -Path $_ -Recurse | where {$_.LastWriteTime -gt (get-date).AddDays(-1)}).LastWriteTime
         $_ >> C:\timestamp.txt; $itemname >> C:\timestamp.txt; $itemtime >> C:\timestamp.txt;
                                                                                                                                                                  }
                             }

我从我的小脚本中得到了这个:

C:\test\DIR - Copy (2)
test (2)
test
test2 (2)
test2

Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43
Wednesday, 17. July 2013 17:49:43

我不知道你要检查多少文件,所以我不能说这是否会很快。

编辑:

$path = C:\test
gci $PATH | sort LastWriteTime | select -last 1

给我看这个:

Directory: C:\test


Mode                LastWriteTime     Length Name                                                                                                                                    
 ----                -------------     ------ ----                                                                                                                                    
 d----        18.07.2013     09:37            Neuer Ordner - Kopie (2)       

【讨论】:

  • 我只想检查文件夹中的特定文件或文件夹的 gt 时间戳,如果满足该要求则返回文件夹名称
  • 更新了我的回答。选择最新文件的简单事务。这就是你想要的吗?
【解决方案2】:
 $path = get-childitem | foreach {$_.name}
 $directory = $path | foreach {$_+"/"}
 get-childitem -path $directory | where-object {$_.name -eq "Pending"} |
 where-object {$_.lastwritetime -gt "12/30/2012"} | 
 where-object {$_.PSIsContainer} |
 foreach {$_.parent.name + "," + $_.lastwritetime} > 
 c:\users\opr9823\2013users.csv

这是为我做的

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 2016-08-29
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多