如何在Powershell上输出存储在.txt文件中的多个文件扩展名 [英] How to output multiple file extensions stored in a .txt file on Powershell

查看:168
本文介绍了如何在Powershell上输出存储在.txt文件中的多个文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我当前的代码如下:

$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

推荐答案

我刚刚清理了您的代码.

I just cleaned up your code.

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

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?

未经测试的环境.

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

这篇关于如何在Powershell上输出存储在.txt文件中的多个文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆