none
VB Script equilvent of 'Ticks' functionality... RRS feed

  • Question

  • Hi,

    I am looking to duplicate some functionality in VB Script (as the system I am trying to implement the functionality in, only uses VBScript).
    The code is originally coming from C#.NET, looking like this:
    ==
    public string OverallJobNumber { get; set; }
    //do lots of other good stuff then
    string currentDateYYYYMMDD = PadWithOneZero(DateTime.Now.Year) + PadWithOneZero(DateTime.Now.Month) + PadWithOneZero(DateTime.Now.Day);
    OverallJobNumber = currentDateYYYYMMDD + "_" +  DateTime.Now.Ticks + "_" + MyRandomGenerator.Next().ToString();
    ==
    The resultant 'OverallJobNumber' is something like this: 20150327_635630451520780747_178211495_ev.pdf
    For the moment, it's this part: DateTime.Now.Ticks I am trying to replicate in VB Script.

    I have tried to re-create this in 'VB Script', below,
    Dim ticks
    'ticks = ((TodayNotString * 10000) + 621355968000000000)
    'ticks = DateDiff("s", "01/01/1970 00:00:00", Now())
     ticks = DateDiff("s", "01/01/0001 00:00:00", Now()) 
    'OverallJobNumber = currentDateYYYYMMDD & "_" & CStr(TodayNotString.Ticks) & "_" 
     OverallJobNumber = currentDateYYYYMMDD & "_" & ticks  & "_"   

    but I am getting a different string like these (depending on which code, above is commented, or uncommented):
    Test Writing to LogFile OverallJobNumber 201504010_6.21355968421042E+17__
    Test Writing to LogFile OverallJobNumber 201504013_1428920175__
    Test Writing to LogFile OverallJobNumber 201504013_450613958__

    Does anyone have an idea how to make VBScript create a Tick thing just like C Sharp can?
    It's possibly a mathematics question, but it depends what you know about the C Sharp Ticks ?

    Any other ideas?

    Tuesday, April 14, 2015 9:45 AM

Answers

  • I think this is the closest you can get in VBScript:

    WScript.Echo FormatNumber((((DateDiff("s", "01/01/1970 00:00:00", Now())*10000000)+621355968000000000)+(Timer()*100)),0,,,0)

    If you want to compare with PowerShell you can run this:

    strPSCommand = "Write-Host (Get-Date).Ticks"
    strCommand = "powershell.exe -noprofile -command " & strPSCommand
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec(strCommand)
    strPSTicks = objExec.StdOut.ReadAll
    strVBTicks = FormatNumber((((DateDiff("s", "01/01/1970 00:00:00", Now())*10000000)+621355968000000000)+(Timer()*100)),0,,,0)
    objExec.StdIn.Close
    WScript.Echo _
    	"VB: " & strVBTicks & vbNewLine & _
    	"PS: " & Replace(strPSTicks, vbLf, "")

    And if you want to get datetime from ticks you can do this in PowerShell:

    Write-Host 'VB:' (Get-Date 635646406547425000 -f 'yyyy-MM-dd HH:mm:ss.fff')
    Write-Host 'PS:' (Get-Date 635646406545344777 -f 'yyyy-MM-dd HH:mm:ss.fff')



    Tuesday, April 14, 2015 6:45 PM

All replies

  • Can't be easily done in VBScript but easy in PowerShell.

    [datetime]::Now.Ticks

    # filename
    [datetime]::Now.ToString('yyyyMMdd') + '_' + [datetime]::Now.Ticks + $random + '.pdf'

    An easier and more flexible method:

    '{0:yyyyMMdd}_{1}_{2}_ev.pdf' -f [datetime]::Now, [datetime]::Now.Ticks, (Get-Random)

    All of which can also be done in C# but your example uses a much more convoluted method.


    \_(ツ)_/

    Tuesday, April 14, 2015 10:05 AM
  • ticks are intervals of 100ns elapsed since 12:00:00 midnight, January 1, 0001

    You could always do a rough estimate of the ticks by calculating them from the current time.

    Also consider the reason why the original developer chose to use ticks. You might have an alternative that meets the requirements too. That's far better tha just trying to mirror what someone else has done.

    

    MCP/MCSA/MCTS/MCITP

    Tuesday, April 14, 2015 10:10 AM
  • Thanks for your reply - unfortunately the vendor software is using VB Script - I have to use VB Script to do it, there really isn't another option.

    I'm sorry if my method is convoluted, this is originally someone else's code.

    Tuesday, April 14, 2015 10:12 AM
  • If you need a unique number to identify a file it is far easier to use a GUID in VBScript.  Every GUID is unique.  There is a single command to generate a guid.

    \_(ツ)_/

    Tuesday, April 14, 2015 10:15 AM
  • Thank You for your response.
    I think I attempted what you are suggesting, below:

    ticks = DateDiff("s", "01/01/0001 00:00:00", Now()) 

    Pretty sure this resulted in:
    450613958

    I expect that's the number of seconds since 01.01.0001 00:00:00
    If I multiply it by 1000000 I get: 450613958000000 but that's still quite different from the string produced from the existing process/program/procedure..

    Its difficult to explain, but basically the existing program, and mine, will be inputting information into the same database, in different ways - therefore, I need to replicate the same behaviour as the existing process / application with my new one, doing something completely different, would corrupt the data and confuse the users, and likely cause other problem's, down the line..

    Tuesday, April 14, 2015 10:18 AM
  • VBScript can call PowerShell.

    Ticks is system dependent if you call the API.

    The developer appears to be just trying to guarantee a unique number with a half-baked and very ancient method.


    \_(ツ)_/

    Tuesday, April 14, 2015 10:20 AM
  • Thank You for your response.
    I think I attempted what you are suggesting, below:

    ticks = DateDiff("s", "01/01/0001 00:00:00", Now()) 

    Pretty sure this resulted in:
    450613958

    I expect that's the number of seconds since 01.01.0001 00:00:00
    If I multiply it by 1000000 I get: 450613958000000 but that's still quite different from the string produced from the existing process/program/procedure..

    Its difficult to explain, but basically the existing program, and mine, will be inputting information into the same database, in different ways - therefore, I need to replicate the same behaviour as the existing process / application with my new one, doing something completely different, would corrupt the data and confuse the users, and likely cause other problem's, down the line..

    The C code uses the system API.  You cannot get or derive that value easily in VBS.  Stick with the C code.

    PowerShell retruns the exact same value as the C# code.


    \_(ツ)_/


    • Edited by jrv Tuesday, April 14, 2015 10:22 AM
    Tuesday, April 14, 2015 10:21 AM
  • Thanks for your reply.

    Unfortunately, I'd like to keep database integrity - by keeping the records I generate, similar to the ones that are already in the database.
    So I do need to replicate the Ticks functionality as closely as possible..

    Tuesday, April 14, 2015 10:22 AM
  • System Ticks on most systems should be 10,000,000/second and not 1,000,000

    TO get the resolution you must use the milliseconds and event then the "TIick" being used are from the system API and cannot be constructed.


    \_(ツ)_/

    Tuesday, April 14, 2015 10:25 AM
  • Thanks for your responses.
    Is there genuinely no way of doing this in VB Script then?
    I would have thought it's possible, using the power of maths or something.

    I only have the option of using VB Script. That's why I wrote in my post:

    "as the system I am trying to implement the functionality in, only uses VBScript"

    It would be great if everything used C Sharp dot net, save me loads of time, but this is a separately vended application I am working it - it only allows VB Script.

    Of course if I could use C Sharp, or a different programming method I would.
    If the only way is to call a 'powershell' - I don't think this will work, but if you give an example, perhaps I can give this a try:

    "VBScript can call PowerShell."

    Tuesday, April 14, 2015 10:27 AM
  • VBScript can call PowerShell.

    Ticks is system dependent if you call the API.

    The developer appears to be just trying to guarantee a unique number with a half-baked and very ancient method.


    \_(ツ)_/


    Thanks for your response above.
    Actually, I'm trying to replicate the behaviour of the existing code, and write a similar job number to the database, as I intend eventually to write to the same database column. This is a fairly logical and sensible thing to do - keep to a similar data structure as already used - it will help the system work as it does now, the users will already be used to the job numbers, etc.
    If I just wanted any old unique file name sure, there is probably hundred of ways to do that - after your reply, above, some people have started to suggest them, which is a shame, because it's not what I am looking to do.
    Tuesday, April 14, 2015 10:46 AM
  • The closest you will get is to take the DateDiff of the system start and Now and calculate the seconds then multiply by 10,000,000 - this will be close but not exact.  You must check to be sure your system uses the correct tick count.

    This can be done in PowerShell like this:
    [timespan]::TicksPerSecond


    \_(ツ)_/

    Tuesday, April 14, 2015 11:05 AM
  • I think this is the closest you can get in VBScript:

    WScript.Echo FormatNumber((((DateDiff("s", "01/01/1970 00:00:00", Now())*10000000)+621355968000000000)+(Timer()*100)),0,,,0)

    If you want to compare with PowerShell you can run this:

    strPSCommand = "Write-Host (Get-Date).Ticks"
    strCommand = "powershell.exe -noprofile -command " & strPSCommand
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec(strCommand)
    strPSTicks = objExec.StdOut.ReadAll
    strVBTicks = FormatNumber((((DateDiff("s", "01/01/1970 00:00:00", Now())*10000000)+621355968000000000)+(Timer()*100)),0,,,0)
    objExec.StdIn.Close
    WScript.Echo _
    	"VB: " & strVBTicks & vbNewLine & _
    	"PS: " & Replace(strPSTicks, vbLf, "")

    And if you want to get datetime from ticks you can do this in PowerShell:

    Write-Host 'VB:' (Get-Date 635646406547425000 -f 'yyyy-MM-dd HH:mm:ss.fff')
    Write-Host 'PS:' (Get-Date 635646406545344777 -f 'yyyy-MM-dd HH:mm:ss.fff')



    Tuesday, April 14, 2015 6:45 PM
  • I deal with ticks (100-nonosecond intervals or 1x10-7 seconds) in VBScript all the time. For example see this script:

    http://rlmueller.net/Programs/PwdExpires.txt

    But in Active Directory dates are represented as ticks since 12:00 AM January 1, 1601.  In PowerShell dates are in ticks since 12:00 AM January 1, 0001. It has been a long time since I have encountered dates measured since January 1, 1970. I think it was for NT systems (and I don't remember the ticks being 100-nanosecond intervals).


    Richard Mueller - MVP Directory Services

    Wednesday, April 15, 2015 3:24 PM
    Moderator