How to output multiple file extensions stored in a .txt file on Powershell

淺唱寂寞╮ 提交于 2019-12-11 14:42:26

问题


my purpose is to output the files with the extensions that match the ones I store on a .txt file but I don't know how to make it work. The way I am trying to do now does not generate any output or output the files of the extensions that are not on the text file (Extension.txt) I indicate. How am I supposed to fix this? The content in my .txt file is:

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

My current code is as followed:

$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

回答1:


This both generated the files, outputs the files and makes sure they are in your .txt file.

I am not surprised your code wont work. You were doing it fairly poorly. Look forward to seeing you improve.

$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
}



回答2:


I just cleaned up your code.

Without knowing the content of Location.txt,
if there are repetitions in the drives, wouldn't Output contain dublettes of the files with different country?

Without your environment untested.

## 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


来源:https://stackoverflow.com/questions/51183354/how-to-output-multiple-file-extensions-stored-in-a-txt-file-on-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!