locked
Check if Directory Exists or Does not Exist RRS feed

  • Question

  • We have a package that drops files into a network directory. There are times when the network will be down for maintenance, so the directory will not be accessible. I need a way check if the directory is accessible. If it is then the routine is carried out, if it's not then a notification e-mail is sent. Below is the C# Script that I have so far where I have the expression as @[User::FileCk] = TRUE. If it does then it continues processing but I cannot get it to identify when it's not available Below is the code that I'm using.

                string DirLoc = Dts.Variables["User::FolderDir"].Value.ToString();
                //if (Directory.Exists(DirLoc))
                    if (!Directory.Exists(DirLoc))
    
                    {
                    Dts.Variables["User::FileCK"].Value = true;
                }
    If someone could help point me in the right direction on what I'm doing wrong, I'd appreciate it. Thanks.

    Wednesday, May 9, 2018 11:52 AM

Answers

  • So, I've been playing around with it and I modified the C# script as below.

                string DirLoc = Dts.Variables["User::FolderDir"].Value.ToString();
    
                if(Directory.Exists(DirLoc))
                {
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
                else
                {
                    Dts.TaskResult = (int)ScriptResults.Failure;
    
    
                }
    I then have the Precedence constraint set to a value of "Success" if it does exist and another Precedence constraint set to "Failure" if it does not. This seems to be working from the tests that I've tried. Can anyone see a negative of doing it this way? 

    • Proposed as answer by ahpitre Wednesday, May 9, 2018 2:32 PM
    • Unproposed as answer by ahpitre Wednesday, May 9, 2018 2:33 PM
    • Marked as answer by DanHaf Wednesday, May 9, 2018 5:21 PM
    Wednesday, May 9, 2018 1:59 PM

All replies

  • You can use DIR function for this purpose

    As you do now, just populate boolean variable with the result inside script task

    then use it in conditional precedence constraint to determine whether to go ahead with rest of the processing

    more details here

    http://ssis101.blogspot.ae/2008/08/ssis-directory-exists.html


    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    Wednesday, May 9, 2018 12:17 PM
  • Thank you for the response. Is that is VS? I was hoping to find something in C#.
    Wednesday, May 9, 2018 1:41 PM
  • Thank you for the response. Is that is VS? I was hoping to find something in C#.

    its almost simsilar

    like this

    DirectoryInfo Dir = new DirectoryInfo(Dts.Variables("User:FolderDir").Value); if(!Dir.Exists)
    Dts.Variables("User::FileCK").Value = False;

    ...



    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    ----------------------------
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

    • Proposed as answer by ahpitre Wednesday, May 9, 2018 2:33 PM
    Wednesday, May 9, 2018 1:56 PM
  • So, I've been playing around with it and I modified the C# script as below.

                string DirLoc = Dts.Variables["User::FolderDir"].Value.ToString();
    
                if(Directory.Exists(DirLoc))
                {
                    Dts.TaskResult = (int)ScriptResults.Success;
                }
                else
                {
                    Dts.TaskResult = (int)ScriptResults.Failure;
    
    
                }
    I then have the Precedence constraint set to a value of "Success" if it does exist and another Precedence constraint set to "Failure" if it does not. This seems to be working from the tests that I've tried. Can anyone see a negative of doing it this way? 

    • Proposed as answer by ahpitre Wednesday, May 9, 2018 2:32 PM
    • Unproposed as answer by ahpitre Wednesday, May 9, 2018 2:33 PM
    • Marked as answer by DanHaf Wednesday, May 9, 2018 5:21 PM
    Wednesday, May 9, 2018 1:59 PM
  • I think this is the best solution as you can allow your entire process to continue or exit gracefully despite of errors (E.g. Directory is not found, etc.).

    Adrian Hernandez

    Wednesday, May 9, 2018 2:32 PM
  • I marked mine as the answer too in case anyone is in need of this same function down the road.
    Wednesday, May 9, 2018 5:22 PM