PS脚本获取没有文件的文件夹

时间:2018-07-20 12:57:56

标签: powershell get-childitem

我正在尝试创建一个PS脚本,该脚本将遍历目录中的文件夹和子文件夹,然后获取没有文件或文件存在90天以上的文件夹名称。下面是文件夹结构。输出应为VendorA或VendorB或VendorC。我尝试使用gci,但未获得所需的输出。

FTP
    VendorA
        folderA
            folder D
        folderB
            Folder E
            Folder F
            Folder G
        folderC
    VendorB
        folderA
            folder D
        folderB
            Folder E
            Folder F
            Folder G
        folderC
    VendorC
        folderA
            folder D
        folderB
            Folder E
            Folder F
            Folder G
        folderC

这是我尝试获取输出的代码,但是我需要以表格格式将其保存为csv-名称,创建时间

    $gci = gci -Path F:\ftp\NEWFTP -Directory

foreach ($g in $gci)
{
   $child =  Get-ChildItem -Path $g.FullName -Recurse -File
       if ($child -eq $null)
       {
         $g.Name, ($g.CreationTime).ToShortDateString() | Out-File C:\Users\test\Documents\folders.txt -Append
       }
}

2 个答案:

答案 0 :(得分:0)

要返回文件夹x的空子文件夹,您需要:

gci -Path x -Recurse -Directory|%{ if( $null -eq (gci $_.fullname) )  {$_.fullname}} 

答案 1 :(得分:0)

您的要求对我不太清楚。

  • 将90天解释为MinAge:
  • 反之亦然,将-gt更改为-le

## Q:\Test\2018\07\20\SO_51443077.ps1

Push-Location 'F:\Ftp\NewFTP'

$Vendors= Get-ChildItem * -Directory
$MinAge = (Get-Date).AddDays(-90)

ForEach($Vendor in $Vendors){
   If ((!(Get-ChildItem -Path $Vendor.FullName -File -Recurse).Count) -Or
       (!(Get-ChildItem -Path $Vendor.FullName -File -Recurse |
            Where LastWriteTime -gt $MinAge).Count)) {
      "Folder {0} has no file(s) / (newer than {1}" -f $Vendor,$MinAge
   }
}
Pop.Location

我最近几年的测试文件夹中的样本输出

Folder Q:\Test\2017\01 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\02 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\03 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\04 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\06 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\07 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\08 has no file(s) / (newer than 2018-04-21 16:26:05
Folder Q:\Test\2017\10 has no file(s) / (newer than 2018-04-21 16:26:05