none
Beginner: How to get text into a DateAdd command? RRS feed

  • General discussion

  • The last programming I did was 30 years ago. (The first was for an IBM 1401 with 4K of storage!) I dabbled with Excel VBA some years ago. I am an experienced user of a macro-writing program, Macro Express Pro. I've done no VB Scripting and it's unlikely that I'll now master more than the very basics.

    But as a poor second best I would like to get into a sort of plagiarising 'copy/paste mode'. Allowing me to use fragments of others' work to my own ends. To achieve that I'd much appreciate a couple of practical examples to get me started please:

    1. How do I get a text string (e.g. '20170921-235900' representing a time late tonight, UK GMT) into a variable on which I can operate with various script commands?

    2. Once I've (somehow, after much browsing here and elsewhere!) successfully added say '0123' to that, representing one hour and 23 minutes, how do I get the result '20170922-002200' out in text form, say a line of a Notepad file (not MsgBox).

    Terry, East Grinstead, UK

    • Changed type Bill_Stewart Tuesday, November 7, 2017 9:59 PM
    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:01 PM This is not "teach me programming basics" forum
    Thursday, September 21, 2017 2:31 PM

All replies

  • Get a date string? From where? User input? A text file?

    We would much recommend PowerShell if you are starting in scripting. It has date objects which are easily manipulable.

    But you need to start somewhere. There are links at the top of this forum to beginner resources.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 21, 2017 2:35 PM
  • Hi Terry,

    well ... I can't help you with VBS.

    If you want something you can patch snippets together from different people by searching on google, PowerShell may be your friend however (Which basically is the shiny next generation of scripting compared to ancient VBS anyway).

    Let's try your example with PowerShell:

    $dateOld = Get-Content dateold.txt
    $date = [DateTime]::ParseExact($dateOld, 'yyyyMMdd-HHmmss', $null)
    $date.AddMinutes(143).ToString('yyyyMMdd-HHmmss') | Set-Content datenew.txt

    Of course, with PowerShell you can do so much more and working with files (especially plain text) is not always the best way to go about it, but as a first step example it should work for you.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, September 21, 2017 2:45 PM
  • As Bill said in powershell it is easy to manipulate DateTime

    $ts = New-TimeSpan  -Hours 1 -Minutes 23

    $NewDateTime = (get-date) + $ts

    $NewDateTime | out-file c:\temp\newdatetime.txt
    Thursday, September 21, 2017 2:53 PM
  • Thanks all, appreciate the fast replies.

    My reason for looking at VBS is that it's one of the very few 'external scripts' supported by ME Pro.

    External Script
    The External Script command provides a powerful addition to Macro Express, providing a new layer of customization to the program. This command allows a macro to run other script languages as a part of the overall macro. Select from JScript, VBScript or HTA/HTML script languages.

    So maybe I chose my forum too hastily if your focus here is on Powershell.

    But perhaps I could achieve my aim in Powershell, without using the External Script command. Instead maybe I could do it by this alternative method, as I did here, for VBS. In a macro I had just a single Program Launch command, starting wsript.exe with this as its parameters:
    C:\Users\terry\Dropbox\Scripts\SimpleMessage-Longer.vbs

    The script SimpleMessage-Longer.vbs contained just one line:
    Wscript.echo "My very first script. With extra content to test display."
    and it displayed that in a message box.

    So the question that occurs to me next is whether there is an equivalent to wscript.exe for Powershell, which would accept a Powershell script like the two suggested?

    Even better, maybe I could do it entirely with Powershell. But how do I even start experimenting please? With either of those two scripts from simbrook2 and Fred in my text editor, how do I proceed next? Save it to what? Then...?

    Bill: I'm assuming my starting date/time input would already be text on the clipboard. The 'delta' could be the same, or entered by the user after prompting by either the script or my macro.

    Terry, East Grinstead, UK

    EDIT: After few more minutes of googling, in ME Pro I tried C:\Windows\System32\cscript.exe as an 'equivalent' and the used PS files saved with the extension .ps1. The good news was that it didn't crash nor give an error. But all I saw was a window flash up very briefly.

    Then, ignoring ME Pro, I tried simply opening the two PS script files with cscript.exe and got the same. No doubt for an emabarrasingly obvious reason!

    Terry, East Grinstead, UK








    • Edited by terrypinnell Thursday, September 21, 2017 3:54 PM
    Thursday, September 21, 2017 3:35 PM
  • Your questions seem to be revealing a number of misunderstandings that are not possible to easily correct in a forum. A web forum is simply not interactive enough for beginner training; sorry.

    I would suggest starting with the learning link at the top of this forum. Everyone has to start somewhere.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 21, 2017 3:42 PM
  • Part of the issue here is that you do not know much about programming or programming systems.  External executing facilities that call VBScript cannot call PowerShell or any other system because they are using WSH and the SCRRUN.OBJ ActiveX control so you must use VBScript.

    To learn VBScript you will have to go to Amazon or other source for books to find  one of the few remaining books. VBScript is pretty much obsolete.

    I suggest posting in the MEPro forum for assistance if they are still in business or maybe that have an upgrade that is more up-to-date.


    \_(ツ)_/



    • Edited by jrv Thursday, September 21, 2017 3:53 PM
    Thursday, September 21, 2017 3:43 PM
  • Thursday, September 21, 2017 3:51 PM
  • For reference material: https://msdn.microsoft.com/library/d1wf56tt.aspx


    \_(ツ)_/

    Thursday, September 21, 2017 3:52 PM
  • Convert a string date and add days:

    strDateTime = "20170921-235900"
    strDate = Split(strDateTime,"-")(0)
    y = Mid(strDate,1,4)
    m = Mid(strDate,5,2)
    d = Mid(strDate,7,2)
    dDate = DateSerial(y,m,d)
    dNewDate = DateAdd("d",10,dDate)
    MsgBox dNewDate

    To understand how to use this you will need to study the reference material and post usage issues in the MEPro forum.


    \_(ツ)_/

    Thursday, September 21, 2017 4:13 PM
  • You cannot run PS1 files with CScript.  I recommend that you read the resource material posted and get oriented as to what "programming" is and what "programming" languages are before pursuing this further.


    \_(ツ)_/

    Thursday, September 21, 2017 4:27 PM
  • Many thanks jrv. I'll now do some futher studying on how to turn that into either:

    1. A simple interactive script that prompts me for strDateTime and the HHMM adjustment required.

    2. Uses text input from the clipboard containing both variables. e.g. '20170921-235900,0123', and delivers the result to the clipboard.

    Terry, East Grinstead, UK

    Thursday, September 21, 2017 4:29 PM