FTP check existence of any file
Hi All,
My requirement is that there is a folder on our FTP site and I need to check if it has any files. If there is at least one file, the task should be successful otherwise,
it should fail. I wrote the below code to implement this but it's falling with the error that "object reference is not set to an instance of an object" Can anyone please help me with this? Thanks in advance!
Public
Sub Main()
' STEP 1
Dim ftpFileNameListXML
As New StringBuilder
ftpFileNameListXML.AppendLine("<filelist>")
' STEP 2
Dim ftpcm As ConnectionManager = Dts.Connections("FTP")
Dim ftp As FtpClientConnection = _
New FtpClientConnection(ftpcm.AcquireConnection(Nothing))
Dim ftpFileNames()
As String
Dim ftpFolderNames()
As String
' STEP 3
ftp.Connect()
ftp.SetWorkingDirectory(Dts.Variables("FtpWorkingDirectory").Value.ToString())
ftp.GetListing(ftpFolderNames, ftpFileNames)
ftp.Close()
IIf(ftpFileNames.GetUpperBound(0)= 0, Dts.TaskResult = ScriptResults.Failure, Dts.TaskResult = ScriptResults.Success)
End Sub
September 18th, 2011 11:01am
Just check the length of ftpFileNames()
It's empty if there are no files in the ftp working directory.Please mark the post as answered if it answers your question | My SSIS Blog:
http://microsoft-ssis.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
September 18th, 2011 1:31pm
I am using the below code but as soon as I hit ok, it's siplaying a red cross mark on the script task. Whne I placed the cursor, it says "The
binary code for the script is not found..."
IIf(Len(ftpFileNames()) = 0, Dts.TaskResult = ScriptResults.Failure, Dts.TaskResult = ScriptResults.Success)
I am not a programmer so I apologize for my ignorance.
September 18th, 2011 3:22pm
haven't checked it, but should be something like this:
If (fileNames.Length > 0) Then
Dts.TaskResult = ScriptResults.Success
Else
Dts.Events.FireError(0, "FTP Script Task", "Error: no files", String.Empty, 0)
Dts.TaskResult = ScriptResults.Failure
End If
Please mark the post as answered if it answers your question | My SSIS Blog:
http://microsoft-ssis.blogspot.com
Free Windows Admin Tool Kit Click here and download it now
September 18th, 2011 3:27pm
When there are files in the folder on FTP the task runs successfully but when there are no files, it is giving the following error:
Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
Here is the code am using:
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
namespace ST_37d79583fb13448e9dc0055ab9d44385.csproj
{
[System.AddIn.AddIn("ScriptMain", Version =
"1.0", Publisher =
"", Description = "")]
public partial
class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
public void Main()
{
// Get the ftp connection from the Connection Managers collection
ConnectionManager ftpServer = Dts.Connections["FTPConnection"];
// Create a FTP connection object and us the credentials of connection manager
FtpClientConnection myFtpConnection =
new FtpClientConnection(ftpServer.AcquireConnection(null));
// Open the connection
myFtpConnection.Connect();
// Set work folder with the value of the variable
myFtpConnection.SetWorkingDirectory(Dts.Variables["FtpWorkingDirectory"].Value.ToString());
String[] fileNames;
String[] folderNames;
// Get a directory listing and fill the StringArray variables
myFtpConnection.GetListing(out folderNames,
out fileNames);
if(fileNames.Length > 0)
{
Dts.TaskResult = (int)ScriptResults.Success;
}
else
{
Dts.Events.FireError(0, "Check Files",
"Error: no files", String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
// Close connection
myFtpConnection.Close();
}
}
}
September 18th, 2011 4:08pm
Can anyone please help? I must be missing somehting basic but can't figure out what. Appreciate your help in advance!
Free Windows Admin Tool Kit Click here and download it now
September 19th, 2011 9:43am
Check for null... if (filenames == null)Please mark the post as answered if it answers your question | My SSIS Blog:
http://microsoft-ssis.blogspot.com
September 19th, 2011 11:39am
YOU ARE THE MAN!!!
Free Windows Admin Tool Kit Click here and download it now
September 19th, 2011 11:58am