locked
Create a Txt file with datetime stamp in it RRS feed

  • Question

  • I need to creata a Txt file. The only contents of the txt file is the currentdatetime stamp.   File needs to be generated daily. This file is like a flag file, so the other team can pick up the file based on the date within the file.

    How can I do this in SSIS.

    Friday, October 26, 2012 3:48 PM

Answers

  • I have an SSIS Package that loads data from a SQL Table on server A to another Table on server B.

    Once the above table is uploaded into server B, I need to create a semaphore TXT file,that contains of the currentdatetime such as 11:09:16.94 INSIDE THE FILE... That way the other team looks at this semaphore file and knows the table in server B is updated.


    if its just the semaphore containing the date then the above given solution (by me) is enough for you to implement. i can explain the code if not clear to you:

    string datestring = DateTime.Now.ToLongDateString();
    System.IO.File.WriteAllText("c:\\myfile.txt", datestring);

    in code above:

    First line creates a string variable and sets its value as the current date and time by using DateTime class funtion ToLongDateString(). There are also some other functions provided by the DateTime class to format the date and time as per your requirements. Please refer to the msdn link below for more details.

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    Second Line uses WriteAllText() function of File class in System.IO namespace to create a file named myfile.txt at the location "c:\" by inserting the value in "datestring" variable in the text file while creating it. This is a one shot function which does all 4 operations in one go. which are:

    1 - Create a file myfile.txt

    2 - Open the file.

    3 - insert datestring value as text. 

    4 - Close the file.

    You can provide any valid file name as the first parameter, which may not already exist and second parameter as the text that you want to insert into the file once its created. Another example can be:

    string datestring ="23 Jan 2012 - This is what you see when you open the file in a text editor."
    System.IO.File.WriteAllText("c:\\myfile.dat", datestring);


    Hope this helps-


    Please mark the post as answered if it answers your question

    • Marked as answer by Eileen Zhao Monday, November 5, 2012 6:58 AM
    Tuesday, October 30, 2012 4:44 PM
  • Does this code need to be in a SCRIPT TASK IN SSIS?
    yes, the code needs to be added in the Script Task to work.

    Please mark the post as answered if it answers your question

    • Marked as answer by Eileen Zhao Monday, November 5, 2012 6:57 AM
    Tuesday, October 30, 2012 8:51 PM

All replies

  • Hi,

    With just current time stamp in the text file you can use a script task to do it. try like below,

    Dim FILE_NAME As String = "C:\textfile.txt"
    Dim i As Integer
    Dim aryText(4) As String
    
    aryText(0) = "Mary WriteLine"
    aryText(1) = "Had"
    aryText(2) = "Another"
    aryText(3) = "Little"
    aryText(4) = "One"
    
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
    
    For i = 0 To 4
        objWriter.WriteLine(aryText(i))
    Next
    
    objWriter.Close()
    MsgBox("Text Appended to the File")


    Rajkumar

    Friday, October 26, 2012 3:58 PM
  • whats the code or function to insert the datetime in the file??These are just text values, looking for datetime
    Friday, October 26, 2012 4:02 PM
  • Please go through this link..

    you can find easiest way to generate the flat file with date time stamp.

    http://sqlserverandbi.blogspot.com/2012/10/create-dynamic-text-file-with-timestamp.html

    Good Luck...


    Regards, Chirag Patel (ETL Engineer)

    Friday, October 26, 2012 4:25 PM
  • Replace text with DateTime.Today.ToString().

    // Ulf

    Friday, October 26, 2012 4:58 PM
  • whats the code or function to insert the datetime in the file??These are just text values, looking for datetime

    string datestring = DateTime.Now.ToLongDateString();
    System.IO.File.WriteAllText(string.Format("c:\\myfile_{0}.txt", datestring), datestring);

    Above should be enough to create file with a time stamp within its name and as its content as well.

    The second parameter is the contents that you want to add to the file. Whatever you pass as a 2nd parameter will be inserted into thr text file.


    Please mark the post as answered if it answers your question



    • Edited by DotNetMonster Friday, October 26, 2012 6:05 PM
    • Proposed as answer by ArthurZ Friday, October 26, 2012 6:15 PM
    Friday, October 26, 2012 6:03 PM
  • Can I do this using a .bat file?
    Friday, October 26, 2012 8:01 PM
  • Can I do this using a .bat file?
    yes you can use any extension you want.

    Please mark the post as answered if it answers your question

    Saturday, October 27, 2012 2:50 PM
  • I need to do this completey in a bat file. What will be the code to out in the bat file?

    Monday, October 29, 2012 10:23 PM
  • I need to do this completey in a bat file. What will be the code to out in the bat file?

    As per you initial post all you wanted to do is to save datetime value in a file. Could you please explain what do you want to save in the bat file?

    A good description, elaborated with an example would be much appreciated. 


    Please mark the post as answered if it answers your question

    Tuesday, October 30, 2012 2:18 AM
  • you could use

    echo. |time |find "current" >> testFile%date:~-4,4%%date:~-7,2%%date:~0,2%.txt

    Tuesday, October 30, 2012 12:04 PM
  • I have an SSIS Package that loads data from a SQL Table on server A to another Table on server B.

    Once the above table is uploaded into server B, I need to create a semaphore TXT file,that contains of the currentdatetime such as 11:09:16.94 INSIDE THE FILE... That way the other team looks at this semaphore file and knows the table in server B is updated.


    • Edited by Desigal59 Tuesday, October 30, 2012 4:11 PM
    Tuesday, October 30, 2012 4:10 PM
  • I have an SSIS Package that loads data from a SQL Table on server A to another Table on server B.

    Once the above table is uploaded into server B, I need to create a semaphore TXT file,that contains of the currentdatetime such as 11:09:16.94 INSIDE THE FILE... That way the other team looks at this semaphore file and knows the table in server B is updated.


    if its just the semaphore containing the date then the above given solution (by me) is enough for you to implement. i can explain the code if not clear to you:

    string datestring = DateTime.Now.ToLongDateString();
    System.IO.File.WriteAllText("c:\\myfile.txt", datestring);

    in code above:

    First line creates a string variable and sets its value as the current date and time by using DateTime class funtion ToLongDateString(). There are also some other functions provided by the DateTime class to format the date and time as per your requirements. Please refer to the msdn link below for more details.

    http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

    Second Line uses WriteAllText() function of File class in System.IO namespace to create a file named myfile.txt at the location "c:\" by inserting the value in "datestring" variable in the text file while creating it. This is a one shot function which does all 4 operations in one go. which are:

    1 - Create a file myfile.txt

    2 - Open the file.

    3 - insert datestring value as text. 

    4 - Close the file.

    You can provide any valid file name as the first parameter, which may not already exist and second parameter as the text that you want to insert into the file once its created. Another example can be:

    string datestring ="23 Jan 2012 - This is what you see when you open the file in a text editor."
    System.IO.File.WriteAllText("c:\\myfile.dat", datestring);


    Hope this helps-


    Please mark the post as answered if it answers your question

    • Marked as answer by Eileen Zhao Monday, November 5, 2012 6:58 AM
    Tuesday, October 30, 2012 4:44 PM
  • Does this code need to be in a SCRIPT TASK IN SSIS?
    Tuesday, October 30, 2012 8:44 PM
  • Does this code need to be in a SCRIPT TASK IN SSIS?
    yes, the code needs to be added in the Script Task to work.

    Please mark the post as answered if it answers your question

    • Marked as answer by Eileen Zhao Monday, November 5, 2012 6:57 AM
    Tuesday, October 30, 2012 8:51 PM