create varible and loops

Archna Virani 1 Reputation point
2020-08-31T01:21:15.467+00:00

Create a variable $intfolders that has a value of 10
 Create a variable $intPad that has a value of 0
 Create a variable $i that has a value of 1
 Create a variable for the new folder
o New-Variable –Name strPrefix –Value “test folder” –Option constant
THE LOOP STATEMENT
 Do {
 Set If condition
o if ($i -lt 10)
 Set Command when Condition is True
o {New-Item –path C: -name $strprefix$intpad$i –type directory}
 Set Command when Condition is False
o else
o {New-Item –path C: -name $streprefix$i –type directory}
 Set $i to increment by 1
o $i++
o Close the Do Statement using }

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,509 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,551 Reputation points
    2020-08-31T15:01:05.137+00:00

    Your code will loop forever! There's no terminating condition for the "Do".

    This may be closer to what you're trying to do:

    $strPrefix = 'test folder'
    $intPad =0
    $intfolders = 10
    $i = 1
    do {
        New-Item -path C: -name $strprefix$intpad$i -type directory
        $i++
    } until ($i -eq 10)
    New-Item –path C: -name $streprefix$i -type directory
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.