Creating Multiple Folders: Step-by-Step Exercises


In this exercise, we explore the use of constants, variables, concatenation, decision making, and looping as we create 10 folders in the C:\Mytest directory. This directory was created earlier. If you do not have this folder on your machine, you can either create it manually or modify both the step-by-step exercise and the one step further exercise to use a folder that exists on your machine.

  1. Open Notepad or your favorite Windows PowerShell script editor.

  2. Create a variable called $intFolders and have it hold the value of 10. The code to do this is shown here:

     $intFolders = 10

  3. Create a variable called $intPad. Do not put anything in the variable yet. This code is shown here:

     $intPad

  4. Create a variable called $i and put the number 1 in it. The code to do this is shown here:

     $i = 1

  5. Use the New-Variable cmdlet to create a variable named strPrefix. Use the value argument of the cmdlet to assign the value of "testFolder" to the variable. Use the option argument to make $strPrefix into a constant. The code to do this is shown here:

     New-Variable -Name strPrefix -Value "testFolder" -Option constant

  6. Open a do until statement. Include the opening curly bracket for the script block. This code is shown here:

     do {

  7. Begin an if else statement. The condition to be evaluated is if the variable $i is less than 10. The code that does this is shown here:

     if ($i -lt 10)

  8. Open the script block for the if statement. Assign the value of 0 to the variable $intPad. This is shown here:

     {$intPad=0

  9. Use the New-Item cmdlet to create a new folder. The new folder will be created in the C:\Mytest directory. The name of the new folder will comprise the $strPrefix constant "testFolder", the number 0 from the $intPad variable, and the number contained in the $i variable. The code that does this is shown here:

     new-item -path c:\mytest -name $strPrefix$intPad$i -type directory}

  10. Add the else clause. This code is shown here:

     else

  11. The else script block is the same as the if script block, except it does not include the 0 in the name that comes from the $intPad variable. Copy the New-Item line of code from the if statement and delete the $intPad variable from the name argument. The revised line of code is shown here:

     {new-item -path c:\mytest -name $strPrefix$i -type directory}

  12. Increment the value of the $i variable by 1. To do this, use the double plus symbol (++) operator. The code that does this is shown here:

     $i++

  13. Close the script block for the else clause and add the until statement. The condition that until will evaluate is if the $i variable is equal to the value contained in the $intFolders variable + 1. The reason for adding 1 to $intFolders is so the script will actually create the same number of folders as are contained in the $intFolders variable. Because this script uses a do until loop and the value of $i is incremented before entering the until evaluation, the value of $i is always 1 more than the number of folders created. This code is shown here:

     }until ($i -eq $intFolders+1)

  14. Save your script as yournameimage from book CreateMultipleFolders.ps1. Run your script. You should see 10 folders created in the C:\Mytest directory. If you do not, compare your results with the image from book CreateMultipleFolders.ps1 script in the scripts folder for this chapter. This concludes this step-by-step exercise.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net