Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:11:50+00:00 2026-06-08T20:11:50+00:00

I have created a batch file that regularly gets triggered from an automated job.

  • 0

I have created a batch file that regularly gets triggered from an automated job.

Within the batch file, I want to check if time is between 12AM and 8AM, set a variable with a particular value, otherwise, set it to the default.

Any tips I how might do this in a batch file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:11:52+00:00Added an answer on June 8, 2026 at 8:11 pm
    for /f "delims=:." %%T in ("%time%") do if %%T lss 08 (set var=someValue) else (set var=default)
    

    Edit – zb226 is correct – the above is more complicated than need be. Much simpler to do

    if %time:~0,2% lss 08 (set var=someValue) else (set var=default)
    

    The leading 0 in 08 is critical. The reason is that numbers preceded by 0 are treated as octal notation, and 8 and 9 are not valid octal digits. The IF statement will do a string comparison if it sees an invalid octal number on either side. So if the current hour is 09, then 09 lss 8 is TRUE because 0 sorts before 8. Changing to 09 lss 08 gives the correct answer.

    Either solution above sets the value to default if it is exactly 8:00:00.00 AM. If you really want the “particular value” through 8:00:00.00 AM and default any time after, then it is a bit more complex. In that case I would revert back to the FOR solution.

    for /f "tokens=1-4 delims=:." %%A in ("%time%") do (
      set var=default
      if %%A lss 08 set var=someValue
      if %%A%%B%%C%%D equ 08000000 set var=someValue
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a really small batch file that I want to be able to
I have a batch file that was created for an Oracle database that I'm
I have created a batch file which automatically copy a .sql file to the
i have a batch(backup.bat)file.in that i have written a command to create a backup
Using Windows XP, I created a batch-file that takes two files as arguments, and
I have created the below batch file but no success. @ECHO OFF @ECHO Backup
I have a service that sometimes calls a batch file. The batch file takes
I have a batch file that I am executing in a server side javascript
I have created a batch file to output a folder content into a list
I have a simple batch file that executes BTSTask ImportBindings on a previously exported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.