PowerShell script to create many directories
Here is a one-liner in PowerShell to create 100 directories under your current working directory:
0 .. 99 | %{ $dir = ("DIR-{0:0000}" -f $_ ) ; if (! (Test-Path -path $dir ) ) { New-Item $dir -type directory } }
This becomes more readable when we wrap the lines like so:
0 .. 99 | %{
$dir = ("DIR-{0:0000}" -f $_ )
if (! (Test-Path -path $dir ) ) {
New-Item $dir -type directory
}
}
1) We first create a list of 100 numbers.
2) For each number in the list, we loop through a set of intructions.
3) We assign the $dir variable to be "DIR-" followed by a zero-padded number corresponding, starting with DIR-0000.
4) We then check to see if the directory exists. If the directory does not exist, create the directory.
5) After DIR-0000, we proceed to create DIR-0001 if it does not exist and repeat for DIR-0002, DIR-0003, etc.
6) We stop after we create DIR-0099 if it does not exist.
