In: Computer Science
I would like to see a PowerShell script(for purely inspirational work to use as an inspiration for helping to develop my skills as a hobby.
*** This should all be done in a loop **
Before beginning, You will need a simple text file with three weeks of scores between Michigan and Michigan State. Example:
UM21, MSU 14
UM28, MSU 35,
UM14, MSU 3
1) Then I need a loop that reads in the attached text file
2) Read the scores by school and determine if MSU or Michigan won or lost
3) - if the wolverines win, have user put the score into the win file (win.txt)
** UM win over MSU55-10 **
- if the wolverines encounter a loss, put the score in the loss file (loss.txt)
** wolverines lose to spartans 24-17 **
NOTE: the permission must be changed on the file in order to write to it
*** the file permission switching must be done in separate script ***
4) please zip contents of wolverines and sparty directories
The powershell script is given below(the important lines are commented):-
foreach($line in Get-Content C:\Users\user\Documents\scores.txt) {
#Splitting each line of the file to scores of UM and MSU
$arr = $line.split(",")
$len1 = $arr[0].length - 2
$len2 = $arr[1].length - 5
#Extracting the actual score values from the scores of UM and
MSU
$value1 = $arr[0].Substring(2,$len1)
$value2 = $arr[1].Substring(5,$len2)
# As the values are in String,converting them to integer for
comparision purposes
$integer1 = [convert]::ToInt32($value1,10)
$integer2 = [convert]::ToInt32($value2,10)
if($integer1 -gt $integer2){
#Writing the output to the win.txt file
Write-Output ('** UM win over MSU ' + $($integer1) + '-' +
$($integer2) + ' **') | Out-File -Append -FilePath
C:\Users\user\Documents\win.txt
}
else{
#Writing the output to the lose.txt file
Write-Output ('** wolverines lose to spartans ' + $($integer1) +
'-' + $($integer2) + ' **') >>
C:\Users\user\Documents\lose.txt
}
#Re-initializing the output for using again in the loop
$arr = @()
$value1 = 0
$value2 = 0
$integer1 = 0
$integer2 = 0
}
#You can use the below command to zip the text files into an
archive but as it is available in Powershell 5.0 which I don't
have
#Compress-Archive -Path C:\Users\user\Documents\*.txt -Update
-DestinationPath archive.zip