locked
Script runs without error but doesn't copy file? RRS feed

  • Question

  • Hello All,

          I'm able to ftp, and unzip a file, now I need to move it to  a network drive, it runs without errors but no files turn up in the drive? What am I doing wrong? Please help:

    # Here are the variables to change to your needs:
    $ftpPath = 'ftp://ftp.financialgo.net/'
    $ftpUser = 'aUser'
    $ftpPass = 'aPassword'
    $localPath = 'D:\Test1\'
    # end of changes
    
    # get the file listing of the FTP folder
    function Get-FtpDir ($url, $credentials)
    {
      $request = [Net.FtpWebRequest]::Create($url)
      if ($credentials) { $request.Credentials = $credentials }
      $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
      $response = $request.GetResponse()
      $reader   = New-Object IO.StreamReader $response.GetResponseStream() 
      $reader.ReadToEnd()
      $reader.Close()
      $response.Close()
    }
    
    # delete a FTP file
    function Remove-FtpFile ($url, $credentials)
    {
      $request = [System.Net.FtpWebRequest]::create($url)
      if ($credentials) { $ftprequest.Credentials =  $credentials }
      $request.Method = [System.Net.WebRequestMethods+Ftp]::DeleteFile
      $request.GetResponse()
      $request.Close()
    }
    # MAP PATH TO QDLS\CLEAR CK\COMPPEXT
    $password = "aPassword" | ConvertTo-SecureString -asPlainText -Force
    $username = "ISUSNET\aUser"
    $remotePath = "Z:\"
    
    
    $credential = New-Object System.Management.Automation.PSCredential($username,$password)
    New-PSDrive -Name target -PSProvider FileSystem -Root \\GFT-PROD\ofc -Credential $credential | Out-Null 
    # Needed for the download
    $webclient = New-Object System.Net.WebClient 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($ftpUser,$ftpPass)  
    $webclient.BaseAddress = $ftpPath
    
    Get-FTPDir $ftpPath $webclient.Credentials |
      ? { $_ -like '*.zip' } |
      % {
    	  $webClient.DownloadFile($remotePath, $localPath)
        # DownloadFile does not provide a success state, so we have to check for the file name
        #if (Test-Path $remotePath) { Remove-FtpFile $ftpPath+$_ $webclient.Credentials }
      }
      
    # Now remove oldest files but three
    Get-ChildItem $localPath -filter *.zip |
      sort CreationTime -Descending |
      select -Skip 3 |
      Remove-Item
    

    Tuesday, October 31, 2017 6:39 PM

Answers

  • Your remote path is set to a local drive and localpath is set to null.


    \_(ツ)_/

    Tuesday, October 31, 2017 6:51 PM

All replies

  • Your remote path is set to a local drive and localpath is set to null.


    \_(ツ)_/

    Tuesday, October 31, 2017 6:51 PM
  • This is the easiest way to download an FTP file:

    $wc = New-Object System.Net.WebClient
    $wc.DownloadFile('ftp://speedtest.tele2.net/1KB.zip','z:\testftp.zip')

    This is a test site so you can run those lines to see how they work.

    Your function does not return a URL it just returns a name.


    \_(ツ)_/

    Tuesday, October 31, 2017 6:56 PM