Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1060

How to search a utf8 string in word files using powershell [duplicate]

$
0
0

I created a PowerShell script with assistance from GitHub Copilot. It works well with ASCII characters, but when I try to search for UTF-8 characters, it doesn’t return any results. For example, when I set the $searchWord variable to "YANI" the script performs as expected; however, when I change it to "KOLİ" it fails to find a match. How can I ensure that the script searches using UTF-8 encoding when working with Word files?

# Define the directory to search and the word to search for$OutputEncoding = [Console]::OutputEncoding = [Text.UTF8Encoding]::UTF8$directoryPath = "D:\BAKIM_ARIZA_TAKIP_FORMU\2024\AGUSTOS_AYI"$searchWord = "KOLİ"# Load the Word application$word = New-Object -ComObject Word.Application$word.Visible = $false# Get all .docx files in the directory$docxFiles = Get-ChildItem -Path $directoryPath -Filter *.docforeach ($file in $docxFiles) {    # Open the document    $document = $word.Documents.Open($file.FullName)    # Search for the word    $found = $false    foreach ($range in $document.StoryRanges) {        if ($range.Text -match [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::UTF8.GetBytes($searchWord))) {            $found = $true            break        }    }    # Output the file name if the word is found    if ($found) {        Write-Output "Found '$searchWord' in file: $($file.FullName)"    }    # Close the document    $document.Close()}# Quit the Word application$word.Quit()

Viewing all articles
Browse latest Browse all 1060

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>