2017-03-10 4 views
2

J'ai ce code de tâche de script SSIS copié à partir d'un site. J'essaie d'éviter l'erreur FTP a échoué lorsque le dossier est vide. N'a pas besoin d'un fichier de nom spécifique. Le code ci-dessous est pour un nom de fichier spécifique. Comment faire du nom de fichier un caractère générique?Tâche de script SSIS - comment accepter les fichiers génériques dans VB.net

Public Sub Main() 

    Dim StrFolderArrary As String() 
    Dim StrFileArray As String() 
    Dim fileName As String 
    Dim RemoteDirectory As String 


    RemoteDirectory = Dts.Variables("User::RemoteFolder").Value.ToString() 

    Dim cm As ConnectionManager = Dts.Connections("FTPConnection") 'FTP connection manager name 
    Dim ftp As FtpClientConnection = New FtpClientConnection(cm.AcquireConnection(Nothing)) 

    ftp.Connect() 'Connecting to FTP Server 


    ftp.SetWorkingDirectory(RemoteDirectory) 'Provide the Directory on which you are working on FTP Server 

    ftp.GetListing(StrFolderArrary, StrFileArray) 'Get all the files and Folders List 

    'If there is no file in the folder, strFile Arry will contain nothing, so close the connection. 

    If StrFileArray Is Nothing Then 

     MessageBox.Show(Dts.Variables("User::Flag").Value.ToString()) 
     ftp.Close() 
     Dts.Variables("User::Flag").Value = 0 


     'If Files are there, Loop through the StrFileArray arrary and insert into table 

    Else 



     For Each fileName In StrFileArray 

      MessageBox.Show(fileName) 
      If fileName = Dts.Variables("User::FileName").Value.ToString() Then 
       Dts.Variables("User::Flag").Value = 1 
       MessageBox.Show(Dts.Variables("User::Flag").Value.ToString()) 
      End If 
     Next 

     ftp.Close() 

    End If 
    ' Add your code here 
    ' 
    Dts.TaskResult = ScriptResults.Success 
End Sub 

Répondre

1

D'abord, il est préférable d'ajouter la validation suivante au If statment:

If StrFileArray Is Nothing OrElse _ 
    StrFileArray.length = 0 Then 

Filtrer à l'aide Linq (doivent importer System.Linq)

If StrFileArray.Where(Function(x) x.equals(Dts.Variables("User::FileName").Value)).ToList().Count() > 0 Then 

    Dts.Variables("User::Flag").Value = 1 

End If 

MISE À JOUR `

Après avoir lu votre commentaire

If StrFileArray.Where(Function(x) x.StartsWith("Abc") AndAlso x.EndsWith(".txt")).ToList().Count() > 0 Then 

    Dts.Variables("User::Flag").Value = 1 

End If 
+1

N'a pas fonctionné, même résultat. – Jason312

+0

Edited ma réponse je manquais la partie principale :) – Hadi

+0

@ Jason312 attendant votre réponse – Hadi