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

  • Home
  • SEARCH
  • 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 8933957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:40:19+00:00 2026-06-15T09:40:19+00:00

I am trying to create a batch file that will loop a random number

  • 0

I am trying to create a batch file that will loop a random number of times between 2 and 10 and pull a random line from a text file each time and display it.

set var1=%RANDOM%
set /a var2=(var1*9/32768)+2
for /l %%x in (1,1,%var2%) do (
set "lines=0" 
for /f "tokens=*" %%a in (c:\myfile.txt) do set /a "lines+=1" 
set /a "skip=%var1% %% lines" 
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%")
for /f "skip=%skip% tokens=*" %%a in (c:\myfile.txt) do set "item=%%a"&goto display

:display
echo %item%
)

This is two different pieces of code that each work on their own, but I am having trouble putting them together.

The code below will display a random line from the file every time I run it.

set "lines=0" 
for /f "tokens=*" %%a in (c:\myfile.txt) do set /a "lines+=1" 
set /a "skip=%random% %% lines" 
if %skip% lss 1 (set "skip=") else (set "skip=skip=%skip%") 
for /f "%skip% tokens=*" %%a in (c:\myfile.txt) do set "item=%%a"&goto display 
:display 
echo %item% 

And this code will display 1 between 2 and 10 times.

@echo off
set var1=%RANDOM%
set /a var2=(var1*9/32768)+2
for /l %%x in (1,1,%var2%) do (
echo 1
)

I’m probably just overlooking the obvious, but I don’t do a lot of batch scripting.

  • 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-15T09:40:20+00:00Added an answer on June 15, 2026 at 9:40 am

    You have a few problems:

    • You define variable skip within a parenthesized block (the DO clause) and then attempt to use %skip% within the same loop. That can’t work because the variable is expanded when the entire code block is parsed – before the value is set! Normally the solution is to use delayed expansion, but you can’t used delayed expansion within FOR /F options. The solution is to CALL outside of the block.

    • Your logic uses the same random number for each iteration. I interpret your stated requirement to mean you want to use a different random number for each iteration.

    • Your GOTO breaks your FOR loop. The loop will not proceed to the next iteration if you issue a GOTO.

    • Your computation for var2 is not good. It can fail if %RANDOM%*9 exceeds the maximum value for a 4 byte signed int. I also don’t think it is giving the distribution of values that you want. You should use the mod operator like you do for the random line number.

    • The value for the SKIP option must be >= 1. If you want 0 then you must omit the option entirely. Your isolated code does this properly, but your broken code is, ummmm broken 🙂

    Additional improvements:

    • You compute the number of lines in the file each iteration. You only need to do that once.

    • It is much more efficient to use FIND to count the number of lines and capture the value using FOR /F. FOR /F ignores blank lines, so I use FINDSTR to find all non empty lines, and pipe those results to FIND to get the count.

    • Using TOKENS=* will strip leading spaces, whereas DELIMS= will preserve the whole line.

    • The default EOL option is EOL=;, which means any line that begins with ; will be ignored. The weird syntax with carets before each space and equal and the absence of quotes is needed to set EOL to nothing.

    • I added quotes around the file name and added the USEBACKQ option in case your file name includes spaces.

    Here is a working solution

    @echo off
    setlocal
    set "file=c:\myfile.txt"
    for /f %%N in ('findstr "." "%file%"^|find /c /v ""') do set lines=%%N
    set /a "iterations=%random% %% 8 + 2"
    for /l %%N in (1 1 %iterations%) do call :printRandomLine
    exit /b
    
    :printRandomLine
    set /a "skip=%random% %% %lines%"
    if %skip% lss 1 (set "skip=") else set "skip=skip^=%skip%"
    for /f usebackq^ %skip%^ delims^=^ eol^= %%A in ("%file%") do (
      echo %%A
      exit /b
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple batch file that will replace a line
I am trying to create a batch file that removes tel: from a number
I am trying to create a batch file that will Copy files from say
I'm trying to create a batch file that will convert an Excel file to
I am trying to write a batch file for Windows 7 that will create
I'm trying to create a batch file that will check the existence of a
I'm trying to create a batch file that will run Doug Cockford's JSMin against
I am trying to create a simple .bat file that will generate a .html
I am trying to create a batch file that searches a folder for zipped
I'm trying to find a way with a batch file that will delete specific

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.