Create multiple folder if not exists

Home Forums Scripting PowerShell Create multiple folder if not exists

This topic contains 1 reply, has 2 voices, and was last updated by Avatar Ossian 3 years, 1 month ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Avatar
    hilsoe
    Participant
    #166583

    I am trying til make a PS script til create multiple folders from a .csv fil.
    It is giving me some problems

    This is my code

    $Folders = Import-Csv C:ScriptsNewFoldersNewFolders.csv

    ForEach ($Folder in $Folders)
    {
    If(!(Test-Path $Folder.Path$Folder.Folder))
    }
    New-Item -ItemType Directory -Force -Path $Folder.Path$Folder.Folder
    Write-Host Folder $Folder.Folder created in $Folder.Path ! -ForegroundColor Green
    }
    Else
    {
    Write-Host “Folder $Folder.Folder already exists in $Folder.Path !!!” -ForegroundColor Red -BackGroundColor Black
    }
    }
    [/CODE]

    And this is my error

    [CODE]
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:5 char:45
    + If(!(Test-Path $Folder.Path$Folder.Folder))
    + ~
    Missing statement block after If ( condition ).
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:9 char:3
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:14 char:1
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock
    [/CODE]

    What am I missing?[CODE]
    $Folders = Import-Csv C:ScriptsNewFoldersNewFolders.csv

    ForEach ($Folder in $Folders)
    {
    If(!(Test-Path $Folder.Path$Folder.Folder))
    }
    New-Item -ItemType Directory -Force -Path $Folder.Path$Folder.Folder
    Write-Host Folder $Folder.Folder created in $Folder.Path ! -ForegroundColor Green
    }
    Else
    {
    Write-Host “Folder $Folder.Folder already exists in $Folder.Path !!!” -ForegroundColor Red -BackGroundColor Black
    }
    }
    [/CODE]

    And this is my error

    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:5 char:45
    + If(!(Test-Path $Folder.Path$Folder.Folder))
    + ~
    Missing statement block after If ( condition ).
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:9 char:3
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:14 char:1
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock
    [/CODE]

    What am I missing?[CODE]
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:5 char:45
    + If(!(Test-Path $Folder.Path$Folder.Folder))
    + ~
    Missing statement block after If ( condition ).
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:9 char:3
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    At C:ScriptsCreateMultipleFoldersWithCheck.ps1:14 char:1
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingStatementBlock
    [/CODE]

    What am I missing?

    Avatar
    Ossian
    Moderator
    #191356

    Should be an OPEN curly bracket { after the IF – you are closing a curly bracket }

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.