• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

File existence


I have this vb code in a script task that I created to check if a certain file exist. The only problem I have is to to configure the code to check if the filename has certain characters in it.

For example

check and see if any file in the directory has a filename like "_sv.txt".
Public Class ScriptMain
 
	' The execution engine calls this method when the task executes.
	' To access the object model, use the Dts object. Connections, variables, events,
	' and logging features are available as static members of the Dts class.
	' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
	' 
	' To open Code and Text Editor Help, press F1.
	' To open Object Browser, press Ctrl+Alt+J.
 
	Public Sub Main()
        Dim fileLoc, fileName As String
        If Dts.Variables.Contains("User::ExecDir") = True AndAlso _
        Dts.Variables.Contains("User::FTPFile") = True Then
            fileLoc. = CStr(Dts.Variables("User::ExecDir").Value)
            fileName = CStr(Dts.Variables.Item("User::FTPFile").Value)
            'System.Windows.Forms.MessageBox.Show(FileDir:+fileLoc+FileName: + fileName)
            If File.Exists(fileLoc + fileName) Then
                Dts.Variables.Item("User::FileExists").Value = True
                'System.Windows.Forms.MessageBox.Show(File exists)
            Else
                Dts.Variables.Item("User::FileExists").Value = False
                'System.Windows.Forms.MessageBox.Show(File not exists)
            End If
            Dts.TaskResult = Dts.Results.Success
        Else
            Dts.TaskResult = Dts.Results.Failure
        End If
    End Sub
 
End Class

Open in new window

0
jorbroni
Asked:
jorbroni
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
How about:
Public Class ScriptMain
 
        ' The execution engine calls this method when the task executes.
        ' To access the object model, use the Dts object. Connections, variables, events,
        ' and logging features are available as static members of the Dts class.
        ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        ' 
        ' To open Code and Text Editor Help, press F1.
        ' To open Object Browser, press Ctrl+Alt+J.
 
        Public Sub Main()
        Dim fileLoc, fileName As String
        If Dts.Variables.Contains("User::ExecDir") = True AndAlso _
        Dts.Variables.Contains("User::FTPFile") = True Then
            fileLoc. = CStr(Dts.Variables("User::ExecDir").Value)
            fileName = CStr(Dts.Variables.Item("User::FTPFile").Value)
            'System.Windows.Forms.MessageBox.Show(FileDir:+fileLoc+FileName: + fileName)
            If File.Exists(fileLoc + fileName) AndAlso (fileLoc + fileName).EndsWith("_sv.txt") Then
                Dts.Variables.Item("User::FileExists").Value = True
                'System.Windows.Forms.MessageBox.Show(File exists)
            Else
                Dts.Variables.Item("User::FileExists").Value = False
                'System.Windows.Forms.MessageBox.Show(File not exists)
            End If
            Dts.TaskResult = Dts.Results.Success
        Else
            Dts.TaskResult = Dts.Results.Failure
        End If
    End Sub
 
End Class

Open in new window

0
 
käµfm³d 👽Commented:
Sorry, didn't test before post. Use this instead--syntax was incorrect in last post.
Public Class ScriptMain
 
        ' The execution engine calls this method when the task executes.
        ' To access the object model, use the Dts object. Connections, variables, events,
        ' and logging features are available as static members of the Dts class.
        ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        ' 
        ' To open Code and Text Editor Help, press F1.
        ' To open Object Browser, press Ctrl+Alt+J.
 
        Public Sub Main()
        Dim fileLoc, fileName As String
        If Dts.Variables.Contains("User::ExecDir") = True AndAlso _
        Dts.Variables.Contains("User::FTPFile") = True Then
            fileLoc. = CStr(Dts.Variables("User::ExecDir").Value)
            fileName = CStr(Dts.Variables.Item("User::FTPFile").Value)
            'System.Windows.Forms.MessageBox.Show(FileDir:+fileLoc+FileName: + fileName)
            If File.Exists(fileLoc + fileName) AndAlso String.Concat(fileLoc,  fileName).EndsWith("_sv.txt") Then
                Dts.Variables.Item("User::FileExists").Value = True
                'System.Windows.Forms.MessageBox.Show(File exists)
            Else
                Dts.Variables.Item("User::FileExists").Value = False
                'System.Windows.Forms.MessageBox.Show(File not exists)
            End If
            Dts.TaskResult = Dts.Results.Success
        Else
            Dts.TaskResult = Dts.Results.Failure
        End If
    End Sub
 
End Class

Open in new window

0
 
käµfm³d 👽Commented:
Actually, you don't even need the String.Concat...   filename.EndsWith("_sv.txt") should suffice.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jorbroniAuthor Commented:

The problem I see is fileName is link to a package variable that I have hard coded with "_sv.txt". I guess what I want it to do is look into the directory (fileLoc) and give me a count of how many files that ends with "_sv.txt" (fileName). If the count is greater than 1 then set User::FileExists = True Else False.

Sorry if I am be confusing. I lack very little vb.net scripting experience
0
 
PedroCGDCommented:
use the function string.CONTAINS
helped?
regards,
Pedro

www.pedrocgd.blogspot.com
0
 
jorbroniAuthor Commented:

I actually figured it out. Below is the code that I came up with.
Imports System
Imports System.Data
Imports System.Math
Imports System.IO
Imports Microsoft.SqlServer.Dts.Runtime
 
Public Class ScriptMain
 
 
    Public Sub Main()
        Try
 
            Dim fileLoc, fileName As String
            Dim d As String
            Dim f As String
            Dim fileCount As Int32
            fileCount = 0
 
            fileLoc = CStr(Dts.Variables("User::ExecDir").Value)
            fileName = CStr(Dts.Variables.Item("User::FTPFile").Value)
 
            For Each f In Directory.GetFiles(fileLoc, fileName)
 
                fileCount = fileCount + 1
 
            Next
 
 
            If fileCount > 0 Then
                Dts.Variables.Item("User::FileExists").Value = True
            Else
                Dts.Variables.Item("User::FileExists").Value = False
            End If
        Catch ex As Exception
 
        End Try
 
        Dts.TaskResult = Dts.Results.Success
    End Sub
 
End Class

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now