Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch Run-time Displays Time with each new line  (Read 5690 times)

0 Members and 1 Guest are viewing this topic.

stew2652

    Topic Starter


    Beginner

    Thanked: 2
    • Experience: Familiar
    • OS: Windows 7
    Batch Run-time Displays Time with each new line
    « on: March 04, 2015, 10:00:03 AM »
    I am reading old topics to gain knowledge of writing scripts.  I pasted the following code from Salmon Trout two years ago into an editor and ran it:
    Arbitrary number of tokens

    @echo off

    REM Get a string to split
    set /p String="Enter a string "

    :Loop

    REM Split string into 1st token and the remainder
    for /f "tokens=1* delims=-" %%A in ("%String%") do (

       REM Echo 1st token
       echo %%A

       REM Assign remainder to %String%
       set String=%%B

       )

    REM If %String% is a blank then we are finished
    if "%String%"=="" goto Done

    REM Otherwise go round again
    goto Loop

    :Done


    Result:

    C:\>Split-hyphen.bat
    Enter a string 1-2-3-4-5-6-7-8-9
    1
    2
    3
    4
    5
    6
    7
    8
    9
    C:\>Split-hyphen.bat
    Enter a string cat-dog-horse-bird-tree
    cat
    dog
    horse
    bird
    tree
    C:\>Split-hyphen.bat
    Enter a string I ran - I fell - I got up again
    I ran
     I fell
     I got up again
    C:\>


    Coming soon: Hybrid batch/VBScript

    It displays the time and carriage return indicator with each new line (except the first):
    Code: [Select]
    Enter a string Hello-my name is...-some random text
    Hello
    The time now is is 10:53:11.85!CR!my name is...
    The time now is is 10:53:11.90!CR!some random text
    Press any key to continue . . .

    Another example of output:
    Code: [Select]
    Enter a string 1-2-3-4
    1
    The time now is is 11:49:27.89!CR!2
    The time now is is 11:49:27.92!CR!3
    The time now is is 11:49:27.95!CR!4
    Press any key to continue . . .

    Here is the original topic ID:
    http://www.computerhope.com/forum/index.php/topic,135017.0.html

    I tried changing the delimiter to # in case the hyphen was causing the problem for me.  The result was still the same.  I searched Google for this type of behavior, but did not see anything related to it in the first 2 or 3 pages.  I am using Windows 7 Enterprise.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Batch Run-time Displays Time with each new line
    « Reply #1 on: March 04, 2015, 11:56:40 AM »
    I can't replicate that problem with the code you listed.

    This variable !CR! is not even in the code so you got something screwed up.

    Salmon Trout

    • Guest
    Re: Batch Run-time Displays Time with each new line
    « Reply #2 on: March 04, 2015, 01:23:44 PM »
    I can't replicate that problem with the code you listed.

    Likewise, and I wrote the *censored* thing.

    Quote
    This variable !CR! is not even in the code so you got something screwed up.

    Must be some extra code introduced. The code to echo the time, and that !CR! thing. I believe we are not seeing the code which is actually being run.

    Also, that computer must be a stone dog if it is running the code with nothing else in the loop! Look at the time difference (30 ms) between the lines.

    I duplicated the effect...

    @echo off

    REM Get a string to split
    set /p String="Enter a string "

    :Loop

    REM Split string into 1st token and the remainder
    for /f "tokens=1* delims=-" %%A in ("%String%") do (

       REM Echo 1st token
       echo The time now is %time%!CR!%%A

       REM Assign remainder to %String%
       set String=%%B

       )

    REM If %String% is a blank then we are finished
    if "%String%"=="" goto Done

    REM Otherwise go round again
    goto Loop

    :Done



    ... and I could get 2 or 3 lines with the same 10 ms time timestamp:

    I got this on a 3 GHz Phenom II system:

    Enter a string 1-2-3-4-5-6-7-8-9-10-11-12-13-14-15
    The time now is 20:15:39.55!CR!1
    The time now is 20:15:39.55!CR!2
    The time now is 20:15:39.55!CR!3
    The time now is 20:15:39.56!CR!4
    The time now is 20:15:39.56!CR!5
    The time now is 20:15:39.57!CR!6
    The time now is 20:15:39.57!CR!7
    The time now is 20:15:39.57!CR!8
    The time now is 20:15:39.58!CR!9
    The time now is 20:15:39.58!CR!10
    The time now is 20:15:39.59!CR!11
    The time now is 20:15:39.59!CR!12
    The time now is 20:15:39.59!CR!13
    The time now is 20:15:39.60!CR!14
    The time now is 20:15:39.60!CR!15


    And on a 2.2 GHz Core 2 Duo system:

    The time now is 20:19:29.18!CR!1
    The time now is 20:19:29.19!CR!2
    The time now is 20:19:29.19!CR!3
    The time now is 20:19:29.19!CR!4
    The time now is 20:19:29.19!CR!5
    The time now is 20:19:29.19!CR!6
    The time now is 20:19:29.19!CR!7
    The time now is 20:19:29.21!CR!8
    The time now is 20:19:29.21!CR!9
    The time now is 20:19:29.21!CR!10


    My guess is the OP is running some other code in the loop (which we are not being shown) which is taking up 30 mS per line.

    Altogether, I think we are being messed with.

    stew2652

      Topic Starter


      Beginner

      Thanked: 2
      • Experience: Familiar
      • OS: Windows 7
      Re: Batch Run-time Displays Time with each new line
      « Reply #3 on: March 04, 2015, 01:56:51 PM »
      I see it now.  You are right about other code being executed  :-[ :-[  (not right about being messed with).   I had placed the code in a test bat file I have with other routines, and I just use a goto at the beginning of the file to execute the code I am testing, then exit.  There was another label named loop elsewhere in the bat file which produces the time and CR.  I have now placed the code in question in a file by itself, and it ran as expected.  Thanks to your replies.

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Batch Run-time Displays Time with each new line
      « Reply #4 on: March 04, 2015, 02:23:10 PM »
      I have made the mistake of making two labels with the same name in a very large batch file but not how you were trying to do it.  Not sure what your logic was behind putting all kinds of different batch files into one batch file and then using a GOTO.  And I guess you now know your logic was flawed.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch Run-time Displays Time with each new line
      « Reply #5 on: March 04, 2015, 03:11:09 PM »
      Reference:
      In DOS the prompt setting can be set to show time with each CRLF.
      http://www.computerhope.com/jargon/p/prompt.htm
      Which leads to a table with a list of options. One is for the time.
      Code: [Select]
      prompt $t $d$_$p$g
      Display the time and the date above your prompt when in DOS.

      Salmon Trout

      • Guest
      Re: Batch Run-time Displays Time with each new line
      « Reply #6 on: March 04, 2015, 03:50:25 PM »
      In DOS the prompt setting can be set to show time with each CRLF.

      ... but you won't get the time every line in a running batch after an @echo off line has been encountered.


      patio

      • Moderator


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: Batch Run-time Displays Time with each new line
      « Reply #7 on: March 04, 2015, 03:54:14 PM »
      Way to go Geek...
      Chime in anytime with something not valid.
      " Anyone who goes to a psychiatrist should have his head examined. "

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: Batch Run-time Displays Time with each new line
      « Reply #8 on: March 04, 2015, 04:37:58 PM »
      Way to go Geek...
      Chime in anytime with something not valid.
      I thought I was the only one who noticed he spews pointless information into threads.

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Re: Batch Run-time Displays Time with each new line
      « Reply #9 on: March 05, 2015, 02:48:48 AM »
      I am reading old topics to gain knowledge of writing scripts.

      Just some comments I'd like mention to clarify a point or two, though one of them seems clear to you now.

      1) Not all code you will see is tested, and not all code you see handles all situations - and is often only aimed at a specific task.

      2) Many times the OP does not reply and so the code you see goes no further than an initial idea, and doesn't get expanded upon or issues found and fixed.

      3) You have to use the code as it was posted to check if it works: as a critical diagnostic step.


      That point is simple in nature but the ability to test code and remove aspects which could affect the outcome is a skill for you to continue to develop, because even very skilled people will miss what is breaking a script until all the extra fluff is taken out.


      stew2652

        Topic Starter


        Beginner

        Thanked: 2
        • Experience: Familiar
        • OS: Windows 7
        Re: Batch Run-time Displays Time with each new line
        « Reply #10 on: March 13, 2015, 08:15:33 AM »
        Okay.  Thanks.  Sorry for the late response.  I just now logged in after several days of no extra time.

        Speaking of using previous code, I have seen references to dropbox where you may download code provided by D. Benham or Antonio or whomever, but I am always leery of downloading a file from the internet if it's not from a specific company's website.  I did try dropbox once, and it seemed like a lot of hassle to me compared to the following way:  why don't I just locate the forum topic page where the code is listed and copy and paste it into an editor?  That is quicker (it seems like) and it eliminates the risk of malware being introduced on the download.

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch Run-time Displays Time with each new line
        « Reply #11 on: March 13, 2015, 08:19:00 AM »
        I did try dropbox once, and it seemed like a lot of hassle to me compared to the following way:  why don't I just locate the forum topic page where the code is listed and copy and paste it into an editor?  That is quicker (it seems like) and it eliminates the risk of malware being introduced on the download.
        Whatever makes you feel warm and fuzzy.  :)

        patio

        • Moderator


        • Genius
        • Maud' Dib
        • Thanked: 1769
          • Yes
        • Experience: Beginner
        • OS: Windows 7
        Re: Batch Run-time Displays Time with each new line
        « Reply #12 on: March 13, 2015, 09:13:24 AM »
        Kinda tuff to insert Malware in a .txt file....Just Sayin...
        " Anyone who goes to a psychiatrist should have his head examined. "

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch Run-time Displays Time with each new line
        « Reply #13 on: March 13, 2015, 10:42:11 AM »
        Kinda tuff to insert Malware in a .txt file....Just Sayin...

        Its possible.  We have a threads over on DosTips.com that talk about embedding images and executables into batch files.

        foxidrive



          Specialist
        • Thanked: 268
        • Experience: Experienced
        • OS: Windows 8
        Re: Batch Run-time Displays Time with each new line
        « Reply #14 on: March 14, 2015, 02:45:45 AM »
        The point the OP made might be that the dropbox copy can be altered.

        To date I had assumed that all the links to the tools were from my dropbox, but it's very possible that someone could copy the text pasted with the link and then paste some batch code and substitute their own dropbox link to a file that could do anything, with the same batchfile name.

        The fact that the link can look like this makes it really hard to double check.

        https://www.dropbox.com/s/qldqwztmetbvklt/repl.bat