title: (Powershell) Loop to delete files from an FTP Location
body: <p>Good morning! </p>
  
  <p>I have made it to the last (and rather pivotal) stage in my script, which is looping to delete files from a directory. I'm not going to pretend I'm knowledgeable at Powershell (far from it), so I'm sort-of chopping up blocks of code I find on the net, improvising and hoping it works.</p>
  
  <p>I'm hoping someone can decipher what I'm trying to do here and see what I'm doing wrong! </p>
  
  <pre><code>#   Clear FTP Directory
  $DelLoop=1
  $server = "www.newsbase.com"
  $dir = "/usr/local/tomcat/webapps/newsbasearchive/monitors/asiaelec/"
  "open $server
  user Canttell Youthis
  binary  
  cd $dir    
  " +(
  For ($DelLoop=1; $DelLoop -le 5; 5)
      {
          $FileList[$DelLoop] | %{ "delete ""$_""`n" }
          $DelLoop++
      })| ftp -i -in
  </code></pre>
  
  <p>I know that the 'Open Connection' portion works, it's just the loop. It just keeps complaining about misplaced operators, and when I fix those, it doesn't throw up any errors - but it doesn't do anything either. </p>
  
  <p>I spent the best part of 4 hours researching this yesterday, and I'm hoping one of you guys can help me.</p>
  
  <p>Thanks in advance!</p>
  
  <p>ADDENDUM:</p>
  
  <p>Here is more of the code, as requested: </p>
  
  <pre><code>#   Clear existing .htm file to avoid duplication
  Get-ChildItem -path ".\" -recurse -include index.jsp | ForEach-Object {
  Clear-Content "index.jsp"
  }
  
  #   Set first part of .JSP Body
  $HTMLPart1="&lt;/br&gt;&lt;tr&gt;&lt;td colspan=9 align=center&gt;&lt;p style=""font-family:Arial""&gt;Here are the links to the last 3 AsiaElec PDFs:&lt;/br&gt;&lt;ul&gt;"
  
  #   Recurse through directory, looking for 3 most recent .PDF files 3 times
  $Directory="C:\PDFs"
  $HTMLLinePrefix="&lt;li&gt;&lt;a style=""font-family:Arial""href="""
  $HTMLLineSuffix="&lt;/a&gt;&lt;/li&gt;"
  $HTMLLine=@(1,2,3,4)
  $Loop=1
  $PDF=@(1,2,3,4)
  Get-ChildItem -path $Directory -recurse -include *.pdf | sort-object -Property LastWriteTime -Descending | select-object -First 3 | ForEach-Object {
          $PDF[$Loop]=$_.name
          $HTMLLine[$Loop]=$HTMLLinePrefix + $_.name + """&gt;" + $_.name + $HTMLLineSuffix
          $Loop++
      }
  
  #   Final .JSP File Assembly
  Get-Content "header.html" &gt;&gt; "index.jsp"
  $HTMLPart1 &gt;&gt; "index.jsp"
  $LineParse=""
  $Loop2=1
  For ($Loop2=1; $Loop2 -le 3; 3)
      {
          $HTMLLine[$Loop2] &gt;&gt; "index.jsp"
          $Loop2++
      }
  Get-Content "tail.html" &gt;&gt; "index.jsp"
  
  #   Prepare File List
  $FileList=@(1,2,3,4,5)
  $FileList[2]=$PDF[2]
  $FileList[3]=$PDF[3]
  $FileList[4]="index.jsp"
  
  #   Clear FTP Directory
  $DelLoop=1
  $server = "www.newsbase.com"
  $dir = "/usr/local/tomcat/webapps/newsbasearchive/monitors/asiaelec/"
  "open $server
  user derek bland1ne
  binary  
  cd $dir    
  " +(
  For ($DelLoop=1; $DelLoop -le 5; 5)
      {
          $FileList[$DelLoop] | %{ "delete ""$_""`n" }
          $DelLoop++
      })| ftp -i -in
  </code></pre>
  
  <p>This isn't all of it, but I believe it contains all the relevant info.</p>