Skip to main content
added 1 character in body; edited tags
Source Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986

I have a for loop that needs to loop the number of times there are folders in a folder.

what i have is:

$Thickness = Get-ChildItem $PSScriptRoot |
             Where-Object {$_.PSIsContainer} |         #Gets the names of folders in a folder
             ForEach-Object {$_.Name}

for($Counter=0;$Counter -lt $Thickness.Count;$Counter++){      
    if(!(Test-Path -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years")){    #Test if there is a folder called "Previous years" in each folder
           new-Item -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years" -ItemType Directory #If folder "Previous Years" Does not exist, create one  
    } 
}

What happened when i run this is that it creates a folder called all the values in the array with the Value of Counter in Brackets

What i get nowWhat i get now

I have a for loop that needs to loop the number of times there are folders in a folder.

what i have is:

$Thickness = Get-ChildItem $PSScriptRoot |
             Where-Object {$_.PSIsContainer} |         #Gets the names of folders in a folder
             ForEach-Object {$_.Name}

for($Counter=0;$Counter -lt $Thickness.Count;$Counter++){      
    if(!(Test-Path -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years")){    #Test if there is a folder called "Previous years" in each folder
           new-Item -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years" -ItemType Directory #If folder "Previous Years" Does not exist, create one  
    } 
}

What happened when i run this is that it creates a folder called all the values in the array with the Value of Counter in Brackets

What i get now

I have a for loop that needs to loop the number of times there are folders in a folder.

what i have is:

$Thickness = Get-ChildItem $PSScriptRoot |
             Where-Object {$_.PSIsContainer} |         #Gets the names of folders in a folder
             ForEach-Object {$_.Name}

for($Counter=0;$Counter -lt $Thickness.Count;$Counter++){      
    if(!(Test-Path -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years")){    #Test if there is a folder called "Previous years" in each folder
           new-Item -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years" -ItemType Directory #If folder "Previous Years" Does not exist, create one  
    } 
}

What happened when i run this is that it creates a folder called all the values in the array with the Value of Counter in Brackets

What i get now

edited tags
Link
mklement0
  • 451.7k
  • 68
  • 726
  • 986
Post Closed as "Duplicate" by mklement0 powershell
Source Link

When i call an array, it gives me the whole array and not just the part i want to call

I have a for loop that needs to loop the number of times there are folders in a folder.

what i have is:

$Thickness = Get-ChildItem $PSScriptRoot |
             Where-Object {$_.PSIsContainer} |         #Gets the names of folders in a folder
             ForEach-Object {$_.Name}

for($Counter=0;$Counter -lt $Thickness.Count;$Counter++){      
    if(!(Test-Path -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years")){    #Test if there is a folder called "Previous years" in each folder
           new-Item -Path "$PSScriptRoot\$Thickness[$Counter]\Previous Years" -ItemType Directory #If folder "Previous Years" Does not exist, create one  
    } 
}

What happened when i run this is that it creates a folder called all the values in the array with the Value of Counter in Brackets

What i get now