RogerSik
RogerSik

Reputation: 45

Create Directory with ls and Create-Item

I wanted to create in another directory the same folders of another directory (which contains about 234 folders).

My ideas was

Ls -Name | New-Item -ItemType Directory -Path D:\Test\

I get then multiple times the error that the D:\Test path already exsits.

Upvotes: 0

Views: 84

Answers (1)

vonPryz
vonPryz

Reputation: 24091

This doesn't work, as the directory creation part New-Item -ItemType never uses the value from pipeline.

What's more, you don't filter the output from ls, so any non-directory files are processed too.

# Get a list of folders only
gci | ? { $_.psIsContainer } | % { 
  # create a dir. Uses join-path and $_.name to combine new root dir with directory's name
  new-item -itemtype directory -path $(join-path "d:\myroot" $_.name)
}

Upvotes: 1

Related Questions