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, 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts

  • sameer12
    Member
    #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 : UnexpectedToken

    D: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” }
    }


    JeremyW
    Moderator
    #271543

    The 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?


    Fede_Pg
    Member
    #391830

    Thank you very much for filtering out
    if($File in $Files) [/CODE]

    This statement right needs to be taken out .

    [CODE] if($File in $Files) [/CODE]

    This statement right needs to be taken out .

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.