none
How to code this batch file? RRS feed

  • Question

  • I would like to know on what wrong is following batch coding.


    @echo off

    if "%current_time%" lss "16:00" (
       # Now is 8PM, but when this batch file run, code can still be processed here.
    )

     Does anyone have any suggestions?
    Thanks in advance for any suggestions

    Thanks in advance for any suggestions

    Thursday, January 4, 2018 3:58 PM

Answers

  • The start command is built into cmd.exe, so of course that's why it doesn't work in PowerShell. Instead, just run the program you want to run using the call/invocation operator (&).


    if ( (Get-Date).Hour -lt 16 ) {
      & "C:\Google\Chrome\Application\chrome.exe" "http://www.cnn.com"
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by oemMicrosoft Thursday, January 4, 2018 11:42 PM
    Thursday, January 4, 2018 4:57 PM
    Moderator

All replies

  • There is no such variable as %current_time%

    Batch files cannot do time comparisons.

    The time in batch is %time%


    \_(ツ)_/


    • Edited by jrv Thursday, January 4, 2018 4:22 PM
    Thursday, January 4, 2018 4:22 PM
  • Don't use batch. Use PowerShell.


    if ( (Get-Date).Hour -lt 16 ) {
      ...
    }
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, January 4, 2018 4:22 PM
    Moderator
  • If Powershell is used, I would like to know on how to open Chrome with specific web site, which work on batch file.

    if ( (Get-Date).Hour -lt 16 ) {
      start /b /max "IE" "C:\Google\Chrome\Application\chrome.exe" "http://www.cnn.com"
    }

    Do you have any suggestions?
    Thanks, to everyone very much for any suggestions (^v^)


    Thanks in advance for any suggestions


    Thursday, January 4, 2018 4:53 PM
  • The start command is built into cmd.exe, so of course that's why it doesn't work in PowerShell. Instead, just run the program you want to run using the call/invocation operator (&).


    if ( (Get-Date).Hour -lt 16 ) {
      & "C:\Google\Chrome\Application\chrome.exe" "http://www.cnn.com"
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by oemMicrosoft Thursday, January 4, 2018 11:42 PM
    Thursday, January 4, 2018 4:57 PM
    Moderator
  • Thanks, to everyone very much for suggestions (^v^)

    Thanks in advance for any suggestions

    Thursday, January 4, 2018 11:42 PM