Announcement

Collapse
No announcement yet.

Unexpected token 'in' in expression or statement.

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unexpected token 'in' in expression or statement.

    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:\ReplicationViaFTPServer\DeleteBackupsOlderthen. 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" }
    }
    Last edited by JeremyW; 19th July 2017, 16:56.

  • #2
    The issue is exactly what it says. 'in' is invalid for an If statement.
    Code:
     
     if($File in $Files)
    If statements need to have a comparison operator. What are you trying to do with this If statement?
    Regards,
    Jeremy

    Network Consultant/Engineer
    Baltimore - Washington area and beyond
    www.gma-cpa.com

    Comment


    • #3
      Thank you very much for filtering out
      Code:
       if($File in $Files)
      This statement right needs to be taken out .

      Comment

      Working...
      X