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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:35:41+00:00 2026-06-05T11:35:41+00:00

As the title says, I’m trying to pick up to four random files (wallpapers)

  • 0

As the title says, I’m trying to pick up to four random files (wallpapers) from a folder, for further processing. The folder does not contain subfolders, just *.jpg’s, *.bmp’s and *.png’s (it may contain a Thumbs.db file, but I already took care of that).

I read all the files with a for loop making something similar to an array, then I’d like to run another for loop for making the random numbers that will act as indexes for choosing the files.

setlocal enabledelayedexpansion
set "wps=1 2 3 4"
set /a ind = 0

for /f "tokens=* delims=" %%g in ('dir C:\Wallpapers /a:-h-s /b /s') do (
    set /a ind += 1
    set "!ind!=%%g"
)

for %%g in (%wps%) do (
    set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind% + 1"
    echo Wallpaper %%g is #!num! - Title: "!!num!!"
)

Of course the line that echoes just outputs Wallpaper 1 is #118 - Title: "118" instead of Wallpaper 1 is #118 - Title: "C:\Wallpapers\Miami Skyline.jpg".

So my specific question is: how can I double expand a variable inside a for loop?

[Note #1: the line that creates the random number needs to be so long because it gives a good random distribution of values]
[Note #2: I need wps to be stored that way, because sometimes I could need just three wallpapers, not necessarily in numerical order]

  • 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-05T11:35:43+00:00Added an answer on June 5, 2026 at 11:35 am

    Transfer the !num! value to a FOR variable 🙂

    for %%N in (!num!) do echo Wallpaper %%g is #%%N - Title: "!%%N!"
    

    A few additional pointers:

    • As written, your DIR command is including folders. You need to add the /A-D option

      dir C:\Wallpapers /a:-h-s-d /b /s

    • Your numeric variable names work in your existing code. But you will have fits if you ever try to access them using normal expansion because %1% will expand as the 1st batch argument instead of the environment variable. In general practice you should avoid beginning an environment variable with a number. You could use:

      set wp!ind!=%%g
      ...
      echo Wallpaper %%g is #!num! - Title: "!wp%%N!

    • You can make your array 0 based by incrementing your counter at the bottom of the loop instead of the top. Then you don’t need to add one to your random number. (This tip is trivial. More a matter of style.)

    • You don’t need both "tokens=*" and "delims=". I recommend the latter. "delims=" will keep the entire string. tokens=* will keep the entire string after it strips leading spaces and tabs. A file name can begin with spaces.

    • Your code will fail if any of the file names contain ! characters. The exclamation will be corrupted during expansion of %%g because of delayed expansion. It is fairly easy to fix by toggling delayed expansion on and of and using a FOR variable to transfer the value across the ENDLOCAL barrier.

    Here is the code with all of the recommendations in place

    @echo off
    setlocal disableDelayedExpansion
    set "wps=1 2 3 4"
    set /a ind = 0
    
    for /f "tokens=* delims=" %%g in ('dir C:\Wallpapers /a:-h-s-d /b /s') do (
      setlocal enableDelayedExpansion
      for %%N in (!ind!) do (
        endlocal
        set "wp%%N=%%g"
      )
      set /a ind += 1
    )
    
    setlocal enableDelayedExpansion
    for %%g in (%wps%) do (
      set /a "num = (((!random! & 1) * 1073741824) + (!random! * 32768) + !random!) %% %ind%"
      for %%N in (!num!) do echo Wallpaper %%g is #%%N - Title: "!wp%%N!"
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As title says, I'd like to block visitors from viewing content of a page
As title says, what does grep ^- mean in UNIX? I know what grep
Title says all - what library(ies) or code or whatever does a digital audio
As title says, I have a div. Somewhere (could be embedded in further child
As the title says, I'm trying to do something like this: public abstract class
So as the title says i want to login with my email, not username,
Title says it pretty much all... I'm trying to automate tests on a web
As title says i whant to change my variables in java from my jruby
As title says. Story is, I've changed meta mark-up of my _Layout.cshtml page from:
Title says what i'm trying to do. I can successfully generate an assembly if

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.