Powershell 文件获取

发布于 2025-01-14 18:21:26 字数 716 浏览 2 评论 0原文

我正在寻找一些帮助来创建 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

Am looking for some help to create a PowerShell script.

I have a folder where I have lots of files, I need only those file that has below two content inside it:

  1. must have any matching string pattern as same as in file file1 (the content of file 1 is -IND 23042528525 or INDE 573626236 or DSE3523623 it can be more strings like this)

  2. also have date inside the file in between 03152022 and 03312022 in the format mmddyyyy.
    file could be old so nothing to do with creation time.

then save the result in csv containing the path of the file which fulfill above to conditions.

Currently am using the below command that only gives me the file which fulfilling the 1 condition.

$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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2025-01-21 18:21:26

要测试文件是否包含一系列关键字中的某个关键字,您可以使用正则表达式。如果您还想在该文件中查找至少一个格式为“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'

To test if a file contains a certain keyword from a range of keywords, you can use regex for that. If you also want to find at least one valid date in format 'MMddyyyy' in that file, you need to do some extra work.

Try below:

# 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'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文