Powershell:表达式或语句中出现意外的Token



很难对进行故障排除

$source = $args[0]
$dest = $args[1]
$fstr = $args[2]
$rstr = $args[3]
If(!(test-path $dest))
{
New-Item -ItemType Directory -Force -Path $dest | out-null
}
If((test-path $source))
{
$files = Get-ChildItem $source *.ICMS
ForEach ($file in $files)
{
$filePath = $source + "" + $file;
$x = Get-Content $filePath
$x[0] = $x[0].Replace($fstr,$rstr)
$outfile =  $dest + "" + $file.Name $x2 = $x[0..($x.Length-2)]
$x = $x2.ForEach({ $_ + "`n"})
$x | Set-Content -NoNewline $outfile
}
}
Else {"Source directory doesn't exist"}  

我收到一个错误,说表达式或语句中的意外令牌'$x2'

您需要将$x2 = $x[0..($x.Length-2)]移动到下一行。

$source = $args[0]
$dest = $args[1]
$fstr = $args[2]
$rstr = $args[3]
If(!(test-path $dest))
{
New-Item -ItemType Directory -Force -Path $dest | out-null
}
If((test-path $source))
{
$files = Get-ChildItem $source *.ICMS
ForEach ($file in $files)
{
$filePath = $source + "" + $file;
$x = Get-Content $filePath
$x[0] = $x[0].Replace($fstr,$rstr)
$outfile =  $dest + "" + $file.Name 
$x2 = $x[0..($x.Length-2)]
$x = $x2.ForEach({ $_ + "`n"})
$x | Set-Content -NoNewline $outfile
}
}
Else {"Source directory doesn't exist"}  

我缺少一个分号。。。当然

$outfile =  $dest + "" + $file.Name

应该是

$outfile =  $dest + "" + $file.Name;

最新更新