I am using a SSIS package with an Execute SQL Task connected to Script Task. The values from Execute SQL Task are stored in a variable and in the Script Task I use the following code to give XML file.
Public Sub Main()
If My.Computer.FileSystem.FileExists("C:\SAMPLE.xml") Then
Dts.TaskResult = ScriptResults.Failure
Else
My.Computer.FileSystem.WriteAllText("C:\SAMPLE.xml", Dts.Variables(0).Value.ToString, False)
Dts.TaskResult = ScriptResults.Success
''
End If
End Sub
I don't want to hardcode the path to XML. So I created two new variables FileName and FilePath with package as scope. How do I edit my VB.Net code to actually use these two variables.I tried this but did not work:
Sub Main()
If My.Computer.FileSystem.FileExists(Dts.Variables("FileDest").Value.ToString()) Else
My.Computer.FileSystem.WriteAllText(Dts.Variables("FileDest").Value.ToString(), Dts.Variables(0).Value.ToString, False)Dts.TaskResult = ScriptResults.Success
''
End If
End Sub
Ideally I want to use two variables one for name and one for path but when I tried with a single variable which combines both, it didn't work.
I suspect that part of your problem is using a numeric index into the Variables collection. Once you added the FileDest variable, there's no guarantee that whatever variable was being used to store the Execute SQL Task results is still the zeroeth one in the collection.
Assuming the variable with the Execute SQL Task results is named XmlQueryResults, the following should do the trick:
Public Sub Main()
Dim filePath As String = Dts.Variables("FileDest").Value
Dim xmlToWrite As String = Dts.Variables("XmlQueryResults").Value
If My.Computer.FileSystem.FileExists(filePath) Then
Dts.TaskResult = ScriptResults.Failure
Else
My.Computer.FileSystem.WriteAllText(filePath, xmlToWrite, False)
Dts.TaskResult = ScriptResults.Success
End If
End Sub
(Don't forget to add the FileDest variable to the ReadOnlyVariables in the Script Task Editor script tab.)
Related
n the closing procedure of a program (just before running Application.exit) I need to run an external program to pass a parameter and exit the calling program.
The program called (FilmDB_Update.exe) has the task of overwriting the main program or a dll library.
I tried to use the "process.start" technique, but apparently, the calling program remains in use and does not allow me to overwrite it.
This is the code that I write:
Private Sub AggiornaPgm()
Dim ws_file As String = "FilmDB_Update.exe"
Dim ws_proc_param As String = """" + ws_working_path + """ " + ws_temp_path
Dim ws_fullPath As String = Path.Combine(ws_temp_path, ws_file)
If File.Exists(ws_fullPath) Then
File.Copy(ws_fullPath, ws_file, True)
End If
Dim proc As New System.Diagnostics.Process()
proc = Process.Start(ws_file, ws_proc_param)
End Sub
I wanted to try using the shell command, but I can not pass the parameters to the called program.
Does any of you have any other ideas about it?
Thank you
Marcello
As Ahmed suggested, I added the test for the calling process to the program called.
p = Process.GetProcessesByName(ws_calling_pgm)
While p.Count > 0
Threading.Thread.Sleep(3000)
p = Process.GetProcessesByName(ws_calling_pgm)
End While
p = Nothing
When I exit the While loop, the calling process is terminated. I do not understand why, despite the process no longer exists, the main program is still in use.
I want to check if sub-folder exists or not. If exists, move on. If not exists go to next task.
My sub-folder is "C:\Folder1\Folder2\Folder3" I want to check if Folder3 is exists or not.
I worked on it. Create 2 variables
1> FolderPath = C:\Folder1\Folder2
2> FolderExists = Boolean = False
Script Task
ReadOnlyVariable = #FolderPAth
ReadWriteVariable = #FolderExists
Following script I add in edit script
Dim DirExists As String
DirExists = Dir(CStr(Dts.Variables("Folder3").Value))
If DirExists <> "" Then
Dts.Variables("Folder3").Value = True
Else
Dts.Variables("Folder3").Value = False
End If
Can some one correct me please.
Based on your comment it doesn't seem like you will care if it is c# of VB so here are steps from beginning to end on how to test Existence of a folder and use it in constrained precedence.
Define 2 Package Level Variables: FolderPath string, FolderExists boolean
Add Script Task and configure for C# and add FolderPath as a ReadOnlyVariable and FolderExists as a ReadWriteVariable
Click Edit to Edit the script
Scroll to "#region Namespaces" near the top and add using System.IO;
Scroll to the definition of the Main() sub and add the first line after "TODO" below so that the routine becomes:
public void Main()
{
// TODO: Add your code here
Dts.Variables["User::FolderExists"].Value = Directory.Exists(Dts.Variables["User::FolderPath"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
The script task is complete and you should now be able to use the FolderExists variable as the expression for constrained precedence.
Add your next step in the package and connect with the green success arrow then double click the arrow and set the Constraint Options to evaluate for Expression and Constraint and the expression simply as the FolderExists variable.
this solution was fully tested and is operational
Okay, so I am working on a small scripting language using a VB Console Application.
I want the user to input "say('something')" and it calls the function I made named "say", is there a way to call the function and still use the following code:
Module Module1
Sub say(sayline)
Console.WriteLine(sayline)
End Sub
Sub Main()
Dim cmd As String
Console.WriteLine(">")
Do
Console.Write("")
cmd = Console.ReadLine()
If cmd IsNot Nothing Then cmd
Loop While cmd IsNot Nothing
End Sub
End Module
No, you cannot just call a method from user's string. You need to interpret the entered data.
First, you need to split your method name and arguments so that entered "say('something')" will transform to say and something. Remember that user can enter wrong data and you need to check if this call is correct - it's all about syntactic and lexical analysis. I hope you understand how to do this because it is pretty difficult.
Then, you need to check if you have a method called say. In case of plain and simple structure, switch construction will be enough. If your have such method, then pass something argument to this method. Else, output something like "unknown method".
If you wanted to call the method say upon typing the word say(something) and display the word something, then you can just have a certain condition that if the user types the word say within the input then call say method else, do whatever you want to do under else portion. Parse the input and omit the word say from the input and display it then.
You can have your code this way for example. (I just copied your code and added some codes to meet what you wanted... in my understanding)
Module Module1
Sub say(ByVal sayline)
Console.WriteLine(sayline)
End Sub
Sub Main()
Dim cmd As String
Do
Console.Write("> ")
cmd = Console.ReadLine()
Try
If cmd IsNot Nothing And cmd.Substring(0, 3).ToUpper().Equals("SAY") Then
say(parseInput(cmd))
End If
Catch ex As Exception
Console.WriteLine("message here")
End Try
Loop While cmd IsNot Nothing
End Sub
Function parseInput(ByVal cmd As String) As String
Dim input As String = ""
For index As Integer = 3 To cmd.Length - 1
If Char.IsLetter(cmd) Then
input += cmd.Substring(index, 1)
Else
input = input
End If
Next
Return input
End Function
End Module
I am trying to write code that opens the saveFileDialog only if the variable 'file' is not empty and generates an error message if 'file' is empty. The following code will pop up a error message box when 'file' is empty but will proceed to open the saveFileDialog anyway.
Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file As String)
Try
Dim cloudFilesProvider = New CloudFilesProvider(cloudId)
If file = "" Then
cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)
End If
Catch
If file = "" Then
MessageBox.Show("Please select file to view")
End If
End Try
End Sub
Can you please direct me.
I think you have the wrong equality operator in your first if-statement.
In humand words, you are telling the compiler:
If file is equal to "" then do ... (If file = "" Then)
What you are trying to say is: if file is NOT equal to "" (If file <> "" Then).
As a second note: Unless an exception occurs, your second if statement will never be called, because the catch block will never be reached. You should put the second clause in a else or else if clause.
I've removed the try catch block for simplicity (and because I'm not really good at the vb.net syntax).
All in all your code should look like this:
Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file As String)
Dim cloudFilesProvider = New CloudFilesProvider(cloudId)
' if file is not emtpy, proceed
If file <> "" Then
cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)
' else if file is empty, show message box
else if file = "" Then
MessageBox.Show("Please select file to view")
End If
End Sub
I am trying to develop a package in SSIS 2005 and part of my process is to check if a file on the network is empty or not. If it is not empty, I need to pass a status of successful, otherwise, I need to pass a status of unsuccessful. I think I need a script task, but am not sure how to go about it. Any help is appreciated.
Create a connection to the flat file in the Connection Managers panel.
Under the Control flow tab, add a Data Flow Task.
Double click the Data flow task and add a Flat File Source and Row Count item.
In the Row Count properties, create a RowCount variable.
In the Control Flow tab, create control flow connections based on the result of the #RowCount.
There are two ways to do it:
If file empty means size = 0 you can create a Script Task to do the check:
http://msdn.microsoft.com/en-us/library/ms345166.aspx
If My.Computer.FileSystem.FileExists("c:\myfile.txt") Then
Dim myFileInfo As System.IO.FileInfo
myFileInfo = My.Computer.FileSystem.GetFileInfo("c:\myfile.txt")
If myFileInfo.Length = 0 Then
Dts.Variables["Status"].Value = 0
End If
End If
Otherwise, if file empty means no rows (flat file) you can use the a Row Count transformation after you reads the file. You can set a variable from the Row Count using the 'VariableName' property in Row Count editor and use it as a status.
Add a simple Script Task with the following code(C#) should do the trick:
String FilePath = (string)Dts.Variables["User::FilePath"].Value;
var length = new System.IO.FileInfo(FilePath).Length;
if (length == 0)
Dts.TaskResult = (int)ScriptResults.Success;
else
Dts.TaskResult = (int)ScriptResults.Failure;
This option will run a lot quicker than the accepted answer as it doesn't need to read the whole file, if you are cycling through a folder of files and some of them are large, in my case ~800mb, the accepted answer would take ages to run, this solution runs in seconds.
Yes, a Script task will do the job here. Add a using System.IO statement to the top of the script, then something along the lines of the following in the Main method will check the contents of the file.
public void Main()
{
String FilePath = Dts.Variables["User::FilePath"].Value.ToString();
String strContents;
StreamReader sReader;
sReader = File.OpenText(FilePath);
strContents = sReader.ReadToEnd();
sReader.Close();
if (strContents.Length==0)
MessageBox.Show("Empty file");
Dts.TaskResult = (int)ScriptResults.Success;
}
Edit: VB.Net version for 2005...
Public Sub Main()
Dim FilePath As String = Dts.Variables("User::FilePath").Value.ToString()
Dim strContents As String
Dim sReader As StreamReader
sReader = File.OpenText(FilePath)
strContents = sReader.ReadToEnd()
sReader.Close()
If strContents.Length = 0 Then
MessageBox.Show("Empty file")
End If
Dts.TaskResult = ScriptResults.Success
End Sub