need help with vb Script for checking if file exists using script task in ssis

Posted on 2011-10-13
Last Modified: 2013-12-16
Hi i am writing a script task in ssis. I need assistance in writing the script task. i googled it as i am newbie to scripting, i need help. So here is the code i got. but the thing is I dont want to hard code the file name because there are four files that exists with different numbers on it with the timestamp on it for that particular day. Can I do that at the point of file location in this script without hardcoding the file. i check this code but its poping up a window. I don't want to see any pop window instead send an email to concerned using send mail task in ssis. Suggest me how can I go about it and how do i know if files doesnt exist. if doesn't exist send an email that says doesnt exist .. Please help me in writing

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO; 

namespace ST_d4b3389a51734caa85bb7d3b9a843c90.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


    public void Main()

      //USe your folder and file name here to check if exists

      if (File.Exists(@"D:\\YOURFILEFOLDER\\Test.txt"))       -- [b]I need to check if four files are added dynamically or using wild card like * to imply that four files exists...[/b]
        Dts.Variables["FileExtFlag"].Value = 1;
        MessageBox.Show(Dts.Variables["FileExtFlag"].Value.ToString()); //comment out this line, just for testing the value of variable
        Dts.Variables["FileExtFlag"].Value = 0;
        MessageBox.Show(Dts.Variables["FileExtFlag"].Value.ToString()); //comment out , just to check value
      // TODO: Add your code here
      Dts.TaskResult = (int)ScriptResults.Success;

Open in new window

Question by:srionline2k6
    LVL 32

    Expert Comment

    from the ".csproj" in namespace it seems it is c# code.



    Author Comment

    Where I need to look my question. I haven't got any replies yet Let mw know where can i get replies
    LVL 32

    Accepted Solution

    you could try

    if (File.Exists(@"D:\YOURFILEFOLDER\Test1.txt") ||
        File.Exists(@"D:\YOURFILEFOLDER\Test2.txt) ||  
        File.Exists(@"D:\YOURFILEFOLDER\Test3.txt) ||  
        File.Exists("D:\\YOURFILEFOLDER\\Test4.txt) ) 

    Open in new window

    note when using the @ you can use single backspace \ in path. when you omit the @ you need the \\ casue backspace is escape character.

    if you need more help you should use the request attention button (below initial question) to reschedule your question and get more answers as i am not much experienced in c#.

    LVL 32

    Assisted Solution

    for more info on File.Exists see

    wildcards are not supported with that statement.

    search with wildcards could be done like

    string[] files = Directory.GetFiles(@"d:\yourfilefolder", "test*.txt"); 
    if (files.Length > 0) 
          //file exist 

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now