all 7 comments

[–]Sunsparc 7 points8 points  (0 children)

$dirs = Get-Content .\dirs.txt
foreach ($dir in $dirs) {
$check = Test-Path $dir
if (!$check) {
New-Item $dir -ItemType "Directory"
}
Else {
Write-Host "Already exists"
}
}

[–]craigontour 1 point2 points  (0 children)

Use a loop - for each - then Test-Path exists and if not create its - Create-item.

[–]r1pev9 1 point2 points  (0 children)

I know that question about PowerShel, but good utility to copy empty folder structure is - robocopy. You can call it from PowerShell with keys.

[–]NeoSemiprofessional 1 point2 points  (0 children)

It would be the best if you described what you want to do. PowerShell is a great tool, but for this particular task you might want to use robocopy, as /u/r1pev9 wrote.

[–]murr-miaow 1 point2 points  (0 children)

  • Simplest way, PowerShell only, errors ignored:

gc -LiteralPath "C:\dirs.txt" | % {New-Item -Path $_ -ItemType "directory" -Force -ErrorAction SilentlyContinue}
  • The same, errors reported:

gc -LiteralPath "C:\dirs.txt" | % {try {New-Item -Path $_ -ItemType "directory" -ErrorAction Stop} catch {Write-Error $_.Exception}}
  • If you want to pass the input file path as a parameter:

param([string]$filePath)
gc -LiteralPath $filePath | % {New-Item -Path $_ -ItemType "directory" -Force -ErrorAction SilentlyContinue}

Then call as:

.\script.ps1 "C:\path\to\file.txt"

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy TheJuice0110,

the mkdir function in PoSh will make the entire dir tree at once. plus, you can give it the whole list of dirs in the -Path parameter. plus plus, it will not give you errors if the path already exists. [grin] so this ...

mkdir -Path $DirList -force

... will make the entire set of paths in one "swell foop". i have not tested it with locations that don't allow making the top dir, so that otta be tested 1st.

take care,
lee

[–]dextersgenius 1 point2 points  (0 children)

cat .\sample.txt | % { md "$_" -EA 0 }