1

Having a hard time troubleshooting this

$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"}  

I receive an error saying Unexpected token '$x2' in expression or statement

2 Answers 2

1

You need to move $x2 = $x[0..($x.Length-2)] to the next line.

$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"}  
Sign up to request clarification or add additional context in comments.

Comments

0

I'm missing a semicolon... of course

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

should be

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

1 Comment

That works too. I also resolved the error by simply moving "$x2=..." to the next line. See the answer I provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.