Forum Discussion

YanSavinsky's avatar
YanSavinsky
New Contributor III
19 days ago
Solved

Files manipulation on SFTP Location

I need to move some files around the various folders on our SFTP server and I am trying to do it via the Extender BR. Basically I need to move the files from the staging folder into various other folders based on the file names.

The rule below compiles successfully. When executed, I get the exception on the second pass ("Chase" related) but no files are being moved anywhere.

I am stumped at the moment and would appreciate any suggestions.

Thank you in advance.

Yan

 

Public Class MainClass
		Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As Object, ByVal args As ExtenderArgs) As Object
		Try
			
			'Set up SFTP connection session options
			Dim sessionOptions As New sessionOptions
			With sessionOptions
				.Protocol = Protocol.Sftp
			        .HostName = "xxxxxxxxxx.xxxxx.com"				
				.UserName = "XXXXXXX"
				.Password = "xxxXXxxxXXXXXx"
				.SshHostKeyFingerprint = "ssh-rsaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpNuL5H42WiDJY="
			End With
			
			'Use Session
			Using session As New Session
			
			'Connect
			session.Open(sessionOptions)
			
			Dim transferOptions As New TransferOptions
			transferOptions.TransferMode = TransferMode.Binary
			Dim baiDirLocation As String = session.HomePath & "/test/TXM/BAI/"
			Dim remoteFiles As RemoteDirectoryInfo = session.ListDirectory(baiDirLocation)
			Dim results As TransferOperationResult
			Dim fileNumber As Integer = 0
			
			For Each x As RemoteFileInfo In remoteFiles.Files
				
				Dim newName As String = Nothing
				
				If x.Name.Contains("Mizuho") Then
					results = session.PutFiles(baiDirLocation & x.Name, baiDirLocation & "Mizuho/" & x.Name, False, TransferOptions)

					If Not results.IsSuccess Then
						Throw New Exception("The file transfer from SFTP to OneStream harvest failed 1.")
					End If

				ElseIf x.Name.Contains("Chase") Then
					results = session.PutFiles(baiDirLocation & x.Name, baiDirLocation & "Chase/" & x.Name, False, TransferOptions)

					If Not results.IsSuccess Then
						Throw New Exception("The file transfer from SFTP to OneStream harvest failed 2.")
					End If
					'Nothing
				End If
				
					

					
				
					'Move or Remove Files or Move SFTP files to archive
					'Session.RemoveFiles(glDirLocation & "S4iiisubdata_US50.txt")

				fileNumber += 1
				
			Next
			'Next Ends the Loop
			End Using
			
			'Setup email distribution list When files are present
			Dim distributionList As New List(Of String)
			distributionList.Add("yyy.yyyyyyy@yyyyy.com,xxx.xxxxxxx@xxxxx.com")
			
			'Calls email Sub Routine
			'EmailNotification(batchinfo, si, distributionList)
			
			Return Nothing
			Catch ex As Exception
				Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
				
		End Try
		End Function
		
		
End Class

 

  • Hi, Daniel. 

    First of all, thank you for reaching out. The exception was that the target specified in the PutFiles function did not exist.

    What I figured out eventually is that "Put" and "Get" FTP commands are for moving files between the FTP location and remote location ( your local network folder, etc.). What I need to do is move the files within the FTP folder structure and for that I ended up using "MoveFile"

    		For Each x As RemoteFileInfo In remoteFiles.Files
    				
    				Dim newName As String = Nothing
    				
    				If x.Name.Contains("BalTran") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Chase/" & x.Name)
    
    				ElseIf x.Name.Contains("BoA") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "BoA/" & x.Name)
    
    				ElseIf x.Name.Contains("Citi") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Citi/" & x.Name)					
    
    				ElseIf x.Name.Contains("Mizuho CAD") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Mizuho CAD/" & x.Name)	
    					
    				ElseIf x.Name.Contains("Mizuho") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Mizuho/" & x.Name)	
    					
    				ElseIf x.Name.Contains("MUFG") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "MUFG/" & x.Name)						
    					
    				ElseIf x.Name.Contains("SMBC III") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "SMBC III/" & x.Name)					
    
    				ElseIf x.Name.Contains("SMBC ITAI") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "SMBC ITAI/" & x.Name)					
    
    					
    				End If
    				
    					
    				fileNumber += 1
    				
    			Next

     

4 Replies

  • YanSavinsky's avatar
    YanSavinsky
    New Contributor III

    Hi, Daniel. 

    First of all, thank you for reaching out. The exception was that the target specified in the PutFiles function did not exist.

    What I figured out eventually is that "Put" and "Get" FTP commands are for moving files between the FTP location and remote location ( your local network folder, etc.). What I need to do is move the files within the FTP folder structure and for that I ended up using "MoveFile"

    		For Each x As RemoteFileInfo In remoteFiles.Files
    				
    				Dim newName As String = Nothing
    				
    				If x.Name.Contains("BalTran") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Chase/" & x.Name)
    
    				ElseIf x.Name.Contains("BoA") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "BoA/" & x.Name)
    
    				ElseIf x.Name.Contains("Citi") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Citi/" & x.Name)					
    
    				ElseIf x.Name.Contains("Mizuho CAD") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Mizuho CAD/" & x.Name)	
    					
    				ElseIf x.Name.Contains("Mizuho") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "Mizuho/" & x.Name)	
    					
    				ElseIf x.Name.Contains("MUFG") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "MUFG/" & x.Name)						
    					
    				ElseIf x.Name.Contains("SMBC III") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "SMBC III/" & x.Name)					
    
    				ElseIf x.Name.Contains("SMBC ITAI") Then
    
    					session.MoveFile(baiStagingLocation & x.Name, baiFolderLocation & "SMBC ITAI/" & x.Name)					
    
    					
    				End If
    				
    					
    				fileNumber += 1
    				
    			Next

     

    • DanielWillis's avatar
      DanielWillis
      Valued Contributor

      Glad you figured it out and thanks for sharing the solution for future travellers. Might I suggest you remove your email address from your original post to save yourself from some spam?