Powershell 文件获取

Powershell Files fetch

正在寻求一些帮助来创建 PowerShell 脚本。

我有一个文件夹,里面有很多文件,我只需要其中包含以下两个内容的文件:

  1. 必须有任何与文件 file1 相同的匹配字符串模式(文件 1 的内容是 -IND 23042528525INDE 573626236DSE3523623 它可以更多像这样的字符串)

  2. 文件中的日期也介于 0315202203312022 之间,格式为 mmddyyyy。 文件可能很旧,所以与创建时间无关。

然后将结果保存在包含满足上述条件的文件路径的csv中。

目前我正在使用下面的命令,它只给我满足条件 1 的文件。

$table = Get-Content C:\Users\username\Downloads\ISIN.txt
Get-ChildItem `
    -Path E:\data\PROD\server\InOut\Backup\*.txt `
    -Recurse |
    Select-String -Pattern ($table)|
    Export-Csv C:\Users\username\Downloads\File_Name.csv -NoTypeInformation

要测试文件是否包含一系列关键字中的某个关键字,您可以使用正则表达式。如果您还想在该文件中找到至少一个 'MMddyyyy' 格式的有效日期,您需要做一些额外的工作。

试试下面的方法:

# read the keywords from the file. Ensure special characters are escaped and join them with '|' (regex 'OR')
$keywords  = (Get-Content -Path 'C:\Users\username\Downloads\ISIN.txt' | ForEach-Object {[regex]::Escape($_)}) -join '|'
# create a regex to capture the date pattern (8 consecutive digits)
$dateRegex = [regex]'\b(\d{8})\b'  # \b means word boundary
# and a datetime variable to test if a found date is valid
$testDate   = Get-Date
# set two variables to the start and end date of your range (dates only, times set to 00:00:00)
$rangeStart = (Get-Date).AddDays(1).Date   # tomorrow
$rangeEnd   = [DateTime]::new($rangeStart.Year, $rangeStart.Month, 1).AddMonths(1).AddDays(-1)  # end of the month

# find all .txt files and loop through. Capture the output in variable $result
$result = Get-ChildItem -Path 'E:\data\PROD\server\InOut\Backup'-Filter '*.txt'-File -Recurse |
ForEach-Object {
    $content = Get-Content -Path $_.FullName -Raw
    # first check if any of the keywords can be found
    if ($content -match $keywords) {
        # now check if a valid date pattern 'MMddyyyy' can be found as well
        $dateFound = $false
        $match = $dateRegex.Match($content)
        while ($match.Success -and !$dateFound) {
            # we found a matching pattern. Test if this is a valid date and if so
            # set the $dateFound flag to $true and exit the while loop
            if ([datetime]::TryParseExact($match.Groups[1].Value, 
                                          'MMddyyyy',[CultureInfo]::InvariantCulture, 
                                          [System.Globalization.DateTimeStyles]::None, 
                                          [ref]$testDate)) {
                # check if the found date is in the set range
                # this tests INCLUDING the start and end dates
                $dateFound = ($testDate -ge $rangeStart -and $testDate -le $rangeEnd)
            }
            $match = $match.NextMatch()
        }
        # finally, if we also successfully found a date pattern, output the file
        if ($dateFound) { $_.FullName }
        elseif ($content -match '\bUNKNOWN\b') {
            # here you output again, because unknown was found instead of a valid date in range
            $_.FullName
        }
    }
}

# result is now either empty or a list of file fullnames
$result | set-content -Path 'C:\Users\username\Downloads\MatchedFiles.txt'