Levi Lenaers
Levi Lenaers

Reputation: 11

make multiple files in multiple folders

I look here on the forums but didn't find my answer.

I'm trying to make multiple folders on my C drive, and at the same time multiple files in these folders.

The folders are no problem, I'm using an array for that. But the files are not working :).

This is my script:

$folders = @("Test1","Test2","Test3")
$files = @("Test1.txt","Test2.txt","Test3.txt")


foreach ($folder in $folders) {
  New-Item -ItemType Directory -Path c:\ -Name $folder
}

foreach ($file in $files){
  New-Item -ItemType File -Path $folder -Name $file
}

Upvotes: 0

Views: 60

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174465

Nest your loops:

$folders = @("Test1","Test2","Test3")
$files = @("Test1.txt","Test2.txt","Test3.txt")

foreach ($folder in $folders) {
  # Create folder
  $newFolder = New-Item -ItemType Directory -Path c:\ -Name $folder

  foreach ($file in $files){
    # Create the files in the folder
    $null = New-Item -ItemType File -Path $newFolder.FullName -Name $file
  }
}

Upvotes: 2

Related Questions