Hey Scripting Guy! The Scripting Wife Learns About Looping

Hey Scripting Guy! The Scripting Wife Learns About Looping

  • Comments 1
  • Likes

Bookmark and Share

 

This week we are following the Scripting Wife as she pushes ahead with her preparations for the 2010 Scripting Games. You can catch up with her progress by reviewing the articles in the archive.

It is the weekend. In two days, the 2010 Scripting Games kick off. Next week, to be honest, will be a bit stressful. However, between then and now are two days to relax, to mellow out, and to get some reading done. My copy of The Three Musketeers arrived, and I am looking forward to snuggling up with a good book for a couple of days before the stampede of the Games. I had just stretched out on the recliner, and opened to page 1 when the Scripting Wife bolted into the room.

“Do you know what day it is today?” she anxiously queried.

“Yes, it is Saturday—my day off. It is your day off too, so I suggest you pick up a book and find a nice quiet spot to read. By the way, this spot is already taken,” I replied.

“Well, you can cancel your reservation for that spot, and cancel your day off, because I need you to come in here and show me how to make looping work. I am trying to follow your Hey, Scripting Guy! post, but I keep getting lost. I could really use some help. Do you know that the 2010 Scripting Games begin in two days?” she asked.

“Ummm, yeah. I think I heard that,” I answered sarcastically. “But, I am busy. I would like to finish this book.”

“Finish? You have not even started,” she said with a smile.

“Yeah, I guess you are right. Let’s get started,” I said as I got up out of the chair and followed her to her computer.

“Let’s build a for statement. The for statement is used to do something a certain number of times. The for statement is made up of three parts, each separated by semicolons. The first section tells the command where to begin counting. You can begin at 0, 1, or 259; it does not matter. You use a variable and assign the beginning value. It looks like $i = 1,” I explained. “Does this make sense so far?”

“Yes. Please continue.”

“Alrighty then. The second part of the for statement tells the command how far it will count. If you want to count to 10, the command would be $i –le 10. The –le portion means less than or equal to 10. The last part of the command determines how we will arrive at our upper number. Most of the time you will increase your number by one each time the script loops; therefore, we use $i++. The ++ operator means that we will take the current number and add one to it. If we used -- it would mean that we wanted to decrease the current number by one,” I said. “Are you with me so far?”

“I think so. It might help if I could see it in action.”

“Open the Windows PowerShell ISE, and type for ($i = 1; $i –le 5; $i++) on the first line. This means that you will begin counting with the number one. You will continue counting as long as the variable $i is less than or equal to five and you will increase the value of $i by one each time through the loop,” I explained.

“Okay. I have got it. Now what?”

“You need to decide what you are going to do with the variable $i. For this example, let’s simply print out the value of the variable $i each time the script makes a loop. This part of the for statement goes inside a pair of curly brackets. You will type { $i }. The code that is inside the curly brackets is called a script block, and it can be all on one line or split among two or even three lines,” I said.

“Okay. I have typed it. Do I need to save it before I run?”

“You can, but you do not need to. Why don’t you just run it for now, and let’s see what happens,” I said.

“Woohoo! It worked. I think. At least it did not give me any bogus errors. Is this right?” she asked.

Image of results of for statement

 

“But my dear, I do have a question. What good is this?” she asked.

“Well, suppose you want to create five files named file1 through file5. You could append the name file in front of your $i variable. You would do it like this “file$i”, I said.

“So this goes inside the curly brackets?”

“Yes, that is correct.”

“Okay. Here it is,” she said.

Image of for statement returning sequentially numbered file names

 

“Well that is pretty cool,” she agreed. “But it does not really create any files, does it?”

“No. You are right. To create the files, we need to use the New-Item cmdlet and specify a path to store the file, and the itemtype as file. Your command is New-Item –path c:\fso –name “File$i.txt” –itemtype file,” I said.

“Okay. I think I have it. I am going to go ahead and run it,” she said.

Image of creation of files themselves

 

“Go into your C:\fso folder and see if the files are there,” I said.

“Cool. They are.”

Image of newly created files n C:\fso

 

“Why don’t you save that as an example, so you have something to go by if you get stuck? I am going to go back and start on my book,” I said.

 

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • I see you are using $i to store your variable. Does this have to be $i, or could it be something like $file.

    Just Curious,

    Thanks,