检查文件夹是否存在时出错

时间:2015-05-27 17:29:10

标签: powershell

我正在尝试检查文件夹是否存在。如果确实如此,如果不创建则不执行任何操作。我一直收到这个错误:

Missing expression after unary operator '!'.
At C:\powershell\Test.ps1:15 char:5
+ if(! <<<<Test-Path - path C:\Today\$DateFormat\))
+Category Info      : ParserError: (!:String) [],   ParentContainsErrorRecordException
+ FullyQualifiedErrorID     : MissingExpressionAfterOperator

这是我的完整语法:

#Declaring this variable as it will be used multiple times
$DateFormat = Get-Date -f 'MM.dd.yy'

#Checking if main folder already exists
if(!Test-Path -path C:\Today\$DateFormat\))
{
New-Item C:\Today\$DateFormat\ -type directory
}#
if(!Test-Path -path C:\Today\$DateFormat\))

1 个答案:

答案 0 :(得分:4)

第一个!运算符(下面两个插入符号之间)有一个起始括号丢失:

if(!Test-Path -path C:\Today\$DateFormat\))
   ^^

if语句更改为这样,并且它将起作用:

if(!(Test-Path -path C:\Today\$DateFormat\))