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 8184909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:40:02+00:00 2026-06-07T01:40:02+00:00

I need to write a script, in.bat, where the user inputs a 3 digits

  • 0

I need to write a script, in.bat, where

  • the user inputs a 3 digits number (as a string) i.
  • the script has 4 predefined groups of numbers, number from 1 to 4
  • the script tests membership for i in the groups, and return the index of the group containing i.

I’m not familiar with declaring and initializing any kinds of data structure (e.g. list, arrays and the like) for batch files, so can someone help me on this?

Pseudo code:

::Returns 1,2,3,4,5 Depending on testNum passed
group1= <822-824,829,845,851,859,864,867>
group2= <826-828,830-839,843-844,847-850,852-854,860-862,883>
group3= <855-858,861,863,865>
group4= <877-882,884>
if %1 is member of group1 
return 1
if %1 is member of group2
return 2
if %1 is member of group3
return 3
if %1 is member of group4
return 4

Thank you!

  • 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-07T01:40:03+00:00Added an answer on June 7, 2026 at 1:40 am

    Batch does not have any formal complex data structures like arrays, lists, or objects. But you can emulate them. Here is an efficient solution that defines the groups with nearly the format as in your question.

    @echo off
    setlocal enableDelayedExpansion
    
    ::Here is a small loop to test the routine
    for %%N in (822,823,883,835,856,863,880,884) do (
      call :assignGroup %%N
      echo %%N is in group !group!
    )
    exit /b
    
    :assignGroup  CaseNumber
    :: The returning value is contained in variable GROUP
    set group=0
    for %%A in (
      "822-824,829,845,851,859,864,867"
      "826-828,830-839,843-844,847-850,852-854,860-862,883"
      "855-858,861,863,865"
      "877-882,884"
    ) do (
      set /a group+=1
      for %%B in (%%~A) do (
        for /f "tokens=1,2 delims=-" %%C in ("%%B") do (
          if "%%C"=="%~1" exit /b
          if "%~1" gtr "%%C" if "%~1" leq "%%D" exit /b
        )
      )
    )
    ::no group found so undefine the var
    set "group="
    exit /b
    

    The above solution is fine for occasional calls. But if you were going to call the routine many thousands of times, then it would be better to initialize an array of valid values with assigned group numbers. Then each test becomes a direct read of the value, instead of having to call a routine. However, it is possible to abuse this technique. Assign enough values and each variable assignment gets slower and slower. You might also spend more time setting up the array than actually testing values.

    Note there is no significance to the characters []. in the variable names. They could be stripped out of the variable names and the code would function the same. They are there only to aid in the understanding the intent of the variables.

    @echo off
    setlocal enableDelayedExpansion
    
    ::initialize a sparse "array" that assigns a group to each valid case #
    set group=0
    for %%A in (
      "822-824,829,845,851,859,864,867"
      "826-828,830-839,843-844,847-850,852-854,860-862,883"
      "855-858,861,863,865"
      "877-882,884"
    ) do (
      set /a group+=1
      for %%B in (%%~A) do (
        for /f "tokens=1,2 delims=-" %%C in ("%%B") do (
          if "%%D"=="" (
            set case[%%C].group=!group!
          ) else for /l %%N in (%%C 1 %%D) do (
            set case[%%N].group=!group!
          )
        )
      )
    )
    
    ::Now test some values
    for %%N in (822,823,883,835,856,863,880,884,900) do (
      if defined case[%%N].group (
        echo %%N is in !case[%%N].group!
      ) else (
        echo %%N is not in a group
      )
    )
    exit /b
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a script in Matlab, which will read some data from
I need to write a script that disables SQL Server Express 2008 from running.
I need to write a script to insert data selected from one table into
I need to write a script in VBA so that if column B has
I need to write a .bat script that executes different sets of commands based
I need to write a script that will automatically rename a number of files
I need to write a script that will output data from a PostgreSQL database
I need to write a script that will prompt a database user in SQL+
I need to write a script that reads all the file names from a
I need to write a script which will post username and password onto a

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.