如何在Powershell上输出存储在.txt文件中的多个文件扩展名

时间:2018-07-05 04:22:09

标签: powershell powershell-v2.0 file-extension scanning

我的目的是输出扩展名与我存储在.txt文件中的扩展名匹配的文件,但我不知道如何使其工作。我现在尝试执行的方式不会生成任何输出,也不会输出我指出的文本文件(Extension.txt)上没有的扩展名文件。我应该如何解决呢? 我的.txt文件中的内容是:

*.xlsx,*.xlsm,*.xlsb,*.xltx,*.xltm,*.xls,*.xml

我当前的代码如下:

$fileHeaders = @('country','cDrive','dDrive')
$extensions  = ${C:temp:Extension.txt}

$LocContent = Import-Csv "C:\temp\Location.txt" -Header $fileHeaders

$NumberOfDays = Read-Host 'Within how many days the files created would you like to output?'
$SizeOfFile = Read-Host 'Above what size of the files would you like to output (in kb or mb)?'

$Output = ForEach($Row in $LocContent){
    if (($Row.country -ne $null) -and ($Row.cDrive -ne $null) -and ($Row.dDrive -ne $null)){
        Get-ChildItem $Row.cDrive,$Row.dDrive -Force -Include -Recurse |
$extensions             Where-Object LastWriteTime -gt (Get-Date).AddDays(-$NumberOfDays) |
                 Where-Object {$_.length/$SizeOfFile -gt 1} | 
                    Select-Object -Property @{N='File Basename';E={$_.BaseName}}, 
                        @{N='File Extension';E={$_.Extension}},
                        @{N='size in MB';E={$_.Length/1024kb}},
                        Directory,
                        CreationTime, 
                        LastWriteTime, 
                        @{N="Location";E={$Row.country}}         
}

$Output | Format-Table -Auto
$Output | Out-Gridview
$Output | Export-Csv '\NewData.csv' -NoTypeInformation

2 个答案:

答案 0 :(得分:0)

这都会生成文件,输出文件并确保它们在您的.txt文件中。

我对您的代码无法正常工作并不感到惊讶。你做得很差。期待您的进步。

$extensions = ((Get-Content C:\temp\Extensions.txt) -join ',') -split ',' -replace '\*',''

Foreach($ext in $extensions){
    Get-ChildItem "C:\temp" -Recurse | select Name, FullName, CreationTime, Extension | Where-Object {$_.Extension -like $ext} | export-csv C:\Files.csv -NoTypeInformation -append
}

答案 1 :(得分:0)

我刚刚清理了您的代码。

不知道Location.txt的内容,
如果驱动器中有重复项,Output会不会包含不同国家/地区的文件的双音符?

未经测试的环境。

## Q:\Test\2018\07\05\SO_51183354.ps1
$fileHeaders = @('country','cDrive','dDrive')
$Extensions = (Get-Content 'C:\temp\Extension.txt') -replace '\*' -split ','

$LocContent = Import-Csv "C:\temp\Location.txt" -Header $fileHeaders |
    Where-Object {($_.country -ne $null) -and 
                  ($_.cDrive  -ne $null) -and 
                  ($_.dDrive  -ne $null) }

$NumberOfDays = Read-Host 'Max file age in days?'
$SizeOfFile   = Read-Host 'Min file size (in kb or mb)?'
$FileAge = (Get-Date).AddDays(-$NumberOfDays)

$Output = ForEach($Row in $LocContent){
  Get-ChildItem $Row.cDrive,$Row.dDrive -Force -Recurse |
    Where-Object {($_.Extension -in $Extensions)  -and
                  ($_.LastWriteTime -gt $FileAge) -and
                  ($_.Length -gt $SizeOfFile) } | 
      Select-Object -Property `
          @{N='File Basename' ;E={$_.BaseName}}, 
          @{N='File Extension';E={$_.Extension}},
          @{N='size in MB'    ;E={$_.Length/1MB}},
               Directory,
               CreationTime, 
               LastWriteTime, 
          @{N="Location"      ;E={$Row.country}}         
}

$Output | Format-Table -Auto
$Output | Out-Gridview
$Output | Export-Csv '\NewData.csv' -NoTypeInformation