This project is read-only.

Using GetFiles from Powershell

Mar 15, 2012 at 12:05 AM
Edited Mar 15, 2012 at 12:50 AM

I have written a powershell script that uses GetDirectoryListUnparsed and GetFile to download all the *.txt files in a remote directory.  I would like to rewrite the script using GetFiles and a wildcard string.  I am having trouble finding examples of using GetFiles.  Do you have any examples?

Here is the script I wrote using GetFile;

 

   # ---------------------------------------------------------------------------
   # Set the current directory to the location from which the script
   # was run and add the AlexFTPS .NET class to the current Powershell
   # session
   # ---------------------------------------------------------------------------
   $script_path    = $myinvocation.invocationname;       
   $script_folder  = split-path $script_path -parent;  
   $script_name    = split-path $script_path -leaf;   
   $current_folder = [system.io.directory]::getcurrentdirectory()
   [system.io.directory]::setcurrentdirectory($script_folder)
   Set-Location $script_folder
   add-type -path ".\AlexFTPS-1.1.0\AlexPilotti.FTPS.Client.dll"
   # ---------------------------------------------------------------------------
   # Configure the connection for the FTP server
   # ---------------------------------------------------------------------------
   #$localDirectoryName   = "//SERVERNAME/PODS/Gemini/import/Active"
   $localDirectoryName   = "C:\TEMP\PODS\Gemini\import\Active"
   $remoteDirectoryName  = "/PODS"   
   $ftpServer            = "FTPSERVERNAME"
   $username             = "USER"
   $password             = "password"
   $SSLMode              = [AlexPilotti.FTPS.Client.ESSLSupportMode]::ClearText
   $filePattern          = "*.TXT"
   # -------------------------
   # Connect to the FTP server
   # -------------------------
   write-host "Connect to $ftpServer as $username"
   $ftp  = new-object "AlexPilotti.FTPS.Client.FTPSClient"
   $credential = New-Object System.Net.NetworkCredential($username,$password)
   $ftp.Connect($ftpServer, $credential, $SSLMode)
   # -----------------------------------------------
   # Get raw directory listing from remote directory
   # -----------------------------------------------
   $ftp.SetCurrentDirectory($remoteDirectoryName)
   $rawfiles = @()
   $rawfiles += $($ftp.GetDirectoryListUnparsed($remoteDirectoryName)).split("$([char]10)")   
   # ------------------------------------------------------------------
   # Convert raw directory listing into list of files to be transferred
   # ------------------------------------------------------------------
   $remoteFiles = @()
   Foreach ($rawFile in $rawFiles)
   {  $temp = $rawFile -replace ("$([char]13)", "") # remove carriage return
      if ($temp -like $filePattern) 
         {$remoteFiles += $temp.substring($temp.LastIndexOf(" ")+1)}
   }
   # ---------------------------
   # Sort and Transfer the files
   # ---------------------------
   $tranBeforeDate = get-date
   write-host "Begin Transferring Files at $tranBeforeDate"
   $remoteFiles = $remoteFiles | Sort-Object
   $ftp.SetCurrentDirectory($remoteDirectoryName)
   foreach ($fileName in $remoteFiles) 
   {
      write-host "Transferring $fileName from $remoteDirectoryName to $localDirectoryName at $(get-date)" 
      $ftp.GetFile("$fileName", "$localDirectoryName\$fileName") 
   }
   $tranAfterDate = get-date
   $tranTotalTime = $tranAfterDate - $tranBeforeDate
   write-host "Finished transferring files at $tranAfterDate, total time was $tranTotalTime"
   # --------------------
   # Close FTP connection
   # --------------------
   $ftp.Close()
   $ftp.Dispose()

Any help would be great. ... Robin HC

Mar 20, 2012 at 7:25 PM
Edited Mar 20, 2012 at 7:26 PM

Here is the layout of the directory listing returned by the iSeries FTP server:

HQSUPPORKB      12288 11/30/10 13:48:41 *DIR       ARC00000001/
HQSUPPORKB       8192 11/26/10 13:51:46 *DIR       ARC00000002/
QOTHPRDOWN       8192 11/26/10 14:32:13 *DIR       ARC00000007/
QOTHPRDOWN       8192 11/27/10 01:09:51 *DIR       ARC00000008/
ODSREP          65536 06/08/11 04:30:50 *DIR       archive/
ODSREP         114688 06/08/11 04:30:53 *DIR       hold/
GNOWNR        8002209 03/20/12 01:00:18 *STMF      ARPNA_20120320_010009.TXT
GNOWNR        7487338 03/20/12 01:16:18 *STMF      BLIVHD_20120320_010009.TXT
GNOWNR      139214106 03/20/12 01:16:49 *STMF      BLIVLN_20120320_010009.TXT
GNOWNR        3197830 03/20/12 01:16:51 *STMF      BLIVSH_20120320_010009.TXT

Robin HC