Hi @Suzana Eree ,
here the script with the do...until
. I got lost in the different versions of the scripts in this threads, So I am still using my last version.
The script below will count the words in file and run the Create-Words
again and again until the word count is reached. For instance 200 words in the file means the Create-Word
function is executed 200 times. At the end the result will be saved in the new file.
# Define file name
$fileName = "./Junk/1/Wordlist.txt"
$newfilename = "./Junk/1/NewWordlist.txt"
# Get content of file
$content = Get-Content -Path $fileName -Raw -Encoding UTF8
$words = ((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n","")
$words.count
#Define function
Function Create-Words {
$script:word1 = Get-Random -InputObject $words # get first random word from word list
$script:word2 = Get-Random -InputObject $words # get second random word from word list
Write-Host "First word: " $script:word1 -ForegroundColor Green # write output word1
Write-Host "Second word: " $script:word2 -ForegroundColor Magenta # write output of word2
}
# Execute function (x times / depending on the word count)
# Get the number of words in file
$replaceCount = $words.count
# Reset counter
$i = 0
# Start the "Do...Until"
do {
Create-Words
# Replace random word1 with random word2 and increase the counter
$content = $content.Replace("$word1", "$word2")
$i++
} until ($i -eq $replaceCount)
# Write the new file
$content | Out-File -FilePath "$newfilename" -force
----------
(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)
Regards
Andreas Baumgarten