2016-09-08 49 views
-1

我想直到做一個PS腳本直到從.csv fil創建多個文件夾。 這是給我一些問題創建多個文件夾,如果不存在

這是我的代碼

$Folders = Import-Csv C:\Scripts\NewFolders\NewFolders.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 
    } 
} 

這是我的錯誤

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

我缺少什麼?

+0

後立即您如果語句您有一個結束}而不是開口{ – Eilidh

+0

更改不正確}到{對於初學者:) – Eilidh

回答

-1
$Folders = Import-Csv C:\Scripts\NewFolders\NewFolders.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 
    } 
} 

您正在關閉if statement是打開它的地方,這裏是您的固定腳本。

+0

'$文件夾=進口-CSV C:\腳本\的CSV \ NewFolders.csv 的ForEach($文件夾中$文件夾) {$ 路徑名= $ Folder.'Path ' $文件夾名稱= $ Folder.'Folder' \t如果(!(測試的路徑$路徑名\ $文件夾名稱)) \t \t { \t \t New-Item -Path $ Pathname \ $ Foldername -ItemType Directory --Force \t \t寫主機「在$路徑名中創建的$文件夾名稱!」 -ForegroundColor綠色 \t \t} \t否則 \t \t { \t \t寫主機 「文件夾$文件夾名稱已經存在於$路徑名!」 --ForegroundColor Red -BackGroundColor Black \t \t} }' 我使用此代碼修復了腳本。但感謝您的幫助。 – Webnoob

相關問題