I have two files. One is structured questions, the other is an answer key.
SourceQuiz.txt sample:
1)This is the first question.
a) option 1
b) option 2
c) option 3
d) option 4
2)This is the second question
a) option 1
b) option 2
c) option 3
d) option 4
3)This is the third question.
a) option 1
b) option 2
c) option 3
d) option 4
4)This is the fourth question.
a) option 1
b) option 2
c) option 3
d) option 4
etc etc
AnswerKey.txt sample:
a
b
d
d
I need FinalQuiz.txt file to be in this format: https://docs.moodle.org/311/en/Aiken_format
What is the correct answer to this question?
A. Is it this one?
B. Maybe this answer?
C. Possibly this one?
D. Must be this one!
ANSWER: D
The fact that the source questions have ) rather than . is immaterial. Moodle interprets them fine.
So I need to:
- Strip the number and right parenthesis from the beginning of each question
- Trim the leading spaces from the beginning of each question's options
- Capitalize the first letter of each line
- then look for every line beginning with D and insert a new line with the string
ANSWER: xwhere x is the value in the line corresponding to the question in AnswerKey.txt
So i know i can:
Get-Content $SourceQuiz.txt | Foreach {$_.TrimEnd()}
and i know i can:
Get-Content C:\Users\Administrator\Desktop\123.txt | ForEach-Object {
if ($_) {
$_.Substring(0, 1).ToUpper() + $_.Substring(1)
} else {
$_
}
} > output.txt
And i know i can use if ($_.StarsWIth("D") in a foreach loop to find lines beginning with D.
What I dont know how to do is nest foreach loops in one another to do these either all at once or in a specific order, nor how to add the value of the right line in AnswerKey.txt (in our example above Question 1 would be ANSWER: A, Question 2 would be ANSWER: B, Question 3 would be ANSWER: D, Question 4 would be ANSWER D.
I could probably get this done if i make multiple scripts, but im not sure. ive been banging my head over this for a bit.
Edit for clarification. Here is source once again:
1)This is the first question.
a) option 1
b) option 2
c) option 3
d) option 4
2)This is the second question
a) option 1
b) option 2
c) option 3
d) option 4
3)This is the third question.
a) option 1
b) option 2
c) option 3
d) option 4
4)This is the fourth question.
a) option 1
b) option 2
c) option 3
d) option 4
The result I'm getting:
This is the first question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: D
This is the second question
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: C
This is the third question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: A
This is the fourth question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: B
The result needed:
1)This is the first question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: D
2)This is the second question
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: C
3)This is the third question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: A
4)This is the fourth question.
a) option 1
b) option 2
c) option 3
d) option 4
ANSWER: B
The numbers are missing. These are the indicators the importer looks for to define the end of one question and the beginning of the next.