Unexpected token ‘in’ in expression or statement.
Home › Forums › Scripting › PowerShell › Unexpected token ‘in’ in expression or statement.
This topic contains 2 replies, has 3 voices, and was last updated by Fede_Pg 1 year, 7 months ago.
-
AuthorPosts
-
July 19, 2017 at 8:12 am #167112
Hi Experts !
I am finding the error while running the powershell script , please help to fix it.
D:ReplicationViaFTPServer>DeleteBackupsOlderthen. bat
D:ReplicationViaFTPServer>powershell .DeleteBackupsOlderthen.ps1
Unexpected token ‘in’ in expression or statement.
At D:ReplicationViaFTPServerDeleteBackupsOlderthen. ps1:19 char:12
+ if($File in < <<< $Files)
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedTokenD:ReplicationViaFTPServer>
Script
Code:# POWER SHELL TO DELETE LOGS FILES FROM SERVER OLDER THEN X DAYS
# DEFINE PARAMETERS
# GET CURRENT DATE
$Now = Get-Date
# DEFINE AMOUNT OF DAYS
$Days=”1″
# Define folder where files are loacted
$Targetfolder =’D:ReplicationViaFTPServer’
#define extention
$Extention =”*trn”
# define last writed time parameters based on $days
$LastWrite = $Now.AddDays(-$Days)
#get files based on last write filter and specific folder #
$File = Get-Childitem $TargetFolder-Include $Extention -Recurse | Where {$_.LastWriteTime
-le
“$LastWrite”}
foreach($File in $Files)
{
if($File in $Files)
{
if($File -ne $NULL)
{
Write-host “Deleting File $File” -Foregroundcolor “DarkRed”
Remove-Item $File.FullName | out-null
} else {
Write-Host “No more files to delete!” -foregroundcolor “Green” }
}July 19, 2017 at 9:59 am #271543The issue is exactly what it says. ‘in’ is invalid for an If statement.
if($File in $Files) [/CODE]
If statements need to have a comparison operator. What are you trying to do with this If statement?[CODE]
if($File in $Files) [/CODE]If statements need to have a comparison operator. What are you trying to do with this If statement?
-
AuthorPosts
You must be logged in to reply to this topic.