Regex 安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了我一些错误,但考虑到我没有经验,我将学习更多关于这一点,并慢慢提高我的技能,再次感谢你的帮
Regex 安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了我一些错误,但考虑到我没有经验,我将学习更多关于这一点,并慢慢提高我的技能,再次感谢你的帮,regex,powershell,ms-word,Regex,Powershell,Ms Word,安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了我一些错误,但考虑到我没有经验,我将学习更多关于这一点,并慢慢提高我的技能,再次感谢你的帮助。安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了一些错误,但考虑到我没有经验-我将学习更多关于这一点,并慢慢建立我的技能-
安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了我一些错误,但考虑到我没有经验,我将学习更多关于这一点,并慢慢提高我的技能,再次感谢你的帮助。安德鲁,谢谢你的慷慨帮助。它确实找到了最高的数字,并把它放到了屏幕上。我很高兴——它做了我需要的事情——从文件中找到了最高的数字——这对我来说已经足够好了。它确实给了一些错误,但考虑到我没有经验-我将学习更多关于这一点,并慢慢建立我的技能-再次感谢你的帮助。
$list = gci "C:\Users\WP\Desktop\SearchFiles" -Include *.docx -Force -recurse
foreach ($foo in $list) {
$objWord = New-Object -ComObject word.application
$objWord.Visible = $False
$objDoc = $objWord.Documents.Open("$foo")
$objSelection = $objWord.Selection
$Pat1 = [regex]'[A-Z]{3}-[0-9]{4}' # Find the regex match 3 letters followed by 4 numbers eg HGW - 1024
$findtext= "$Pat1"
$highestNumber =
# Find the highest occurrence of this pattern found in the documents searched - output to text file or on screen
Sort-Object | # This may also be wrong -I added it for when I find the pattern
Select-Object -Last 1 -ExpandProperty Name
<# The below may not be needed - ?
$ReplaceText = ""
$ReplaceAll = 2
$FindContinue = 1
$MatchFuzzy = $False
$MatchCase = $False
$MatchPhrase = $false
$MatchWholeWord = $True
$MatchWildcards = $True
$MatchSoundsLike = $False
$MatchAllWordForms = $False
$Forward = $True
$Wrap = $FindContinue
$Format = $False
$objSelection.Find.execute(
$FindText,
$MatchCase,
$MatchWholeWord,
$MatchWildcards,
$MatchSoundsLike,
$MatchAllWordForms,
$Forward,
$Wrap,
$Format,
$ReplaceText,
$ReplaceAll
}
}
#>
[regex]::matches($objSelection, '(?<=[A-Z]{3}\s*-\s*)\d{4}') `
| Select -ExpandProperty captures `
| sort value -Descending `
| Select -First 1 -ExpandProperty value `
| Add-Content outfile.txt
# This library is needed to extact zip archives. A .docx is a zip archive
# .NET 4.5 or later is requried
Add-Type -AssemblyName System.IO.Compression.FileSystem
# This function gets plain text from a word document
# adapted from http://stackoverflow.com/a/19503654/284111
# It is not ideal, but good enough
function Extract-Text([string]$fileName) {
#Generate random temporary file name for text extaction from .docx
$tempFileName = [Guid]::NewGuid().Guid
#Extract document xml into a variable ($text)
$entry = [System.IO.Compression.ZipFile]::OpenRead($fileName).GetEntry("word/document.xml")
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry,$tempFileName)
$text = [System.IO.File]::ReadAllText($tempFileName)
Remove-Item $tempFileName
#Remove actual xml tags and leave the text behind
$text = $text -replace '</w:r></w:p></w:tc><w:tc>', " "
$text = $text -replace '</w:r></w:p>', "`r`n"
$text = $text -replace "<[^>]*>",""
return $text
}
$fileList = Get-ChildItem "C:\Users\WP\Desktop\SearchFiles" -Include *.docx -Force -recurse
# Adapted from http://stackoverflow.com/a/36023783/284111
$fileList |
Foreach-Object {[regex]::matches((Extract-Text $_), '(?<=[A-Za-z]{3}\s*(?:-|–)\s*)\d{4}')} |
Select-Object -ExpandProperty captures |
Sort-Object value -Descending |
Select-Object -First 1 -ExpandProperty value