Frage

I'm trying to set up a batch file that launches 10 Steam games, through Steam Achievement Manager, at a time. Here is the current script I am using (which includes many more than 10):

@echo off
echo Launching Steam games...
start sam.game.exe 233720
start sam.game.exe 113200
start sam.game.exe 219640
start sam.game.exe 2500
start sam.game.exe 204300
start sam.game.exe 49600
start sam.game.exe 107100
start sam.game.exe 730
start sam.game.exe 550
start sam.game.exe 35700
start sam.game.exe 92300
start sam.game.exe 217690
start sam.game.exe 620
start sam.game.exe 8930
start sam.game.exe 57690
start sam.game.exe 24240
start sam.game.exe 3830
start sam.game.exe 35140
start sam.game.exe 200260
start sam.game.exe 4920
start sam.game.exe 245070
start sam.game.exe 204360
start sam.game.exe 46750
start sam.game.exe 300
start sam.game.exe 18500
start sam.game.exe 63800
start sam.game.exe 214340
start sam.game.exe 25800
start sam.game.exe 8500
start sam.game.exe 212680
start sam.game.exe 4000
start sam.game.exe 41800
start sam.game.exe 220
start sam.game.exe 25890
start sam.game.exe 219150
start sam.game.exe 1250
start sam.game.exe 42170
start sam.game.exe 202090
start sam.game.exe 220860
start sam.game.exe 4700
start sam.game.exe 32200
start sam.game.exe 22100
start sam.game.exe 39800
start sam.game.exe 91900
start sam.game.exe 40300
start sam.game.exe 4760
start sam.game.exe 45100
start sam.game.exe 11200
start sam.game.exe 107200
start sam.game.exe 99900
start sam.game.exe 440
start sam.game.exe 105600
start sam.game.exe 207610
start sam.game.exe 206440
start sam.game.exe 24010
start sam.game.exe 46540
start sam.game.exe 13230
start sam.game.exe 42960
pause

The numbers that you see there are Steam game IDs. Basically, I want to be able to create an array filled with all of the Steam game IDs that I own, and the script to pick 10 of them (non-duplicating) at random and only start these 10. I'm very new with batch files so I am having a lot of trouble with this.

War es hilfreich?

Lösung

@echo off
setlocal EnableDelayedExpansion

rem You may define next value from a parameter, if you wish
set number=10

rem Define the array of game ID's
set n=0
for %%a in (233720 113200 219640 2500 204300 49600 107100 730   550    35700
            92300  217690 620    8930 57690  24240 3830   35140 200260 4920
            245070 204360 46750  300  18500  63800 214340 25800 8500   212680
            4000   41800  220
           ) do (
   set /A n+=1
   set gameID[!n!]=%%a
)

rem Select %number% non-duplicated random elements from previous array and run such Steam games
for /L %%n in (1,1,%number%) do (
   rem Get the index of a random element
   call :getRandomElem i=
   rem Run such game
   for /F %%i in ("!i!") do start sam.game.exe !gameID[%%i]!
   rem Put a zero in such element
   set gameID[!i!]=0
)

goto :EOF


rem Get the index of a random element greater than 0 from gameID array
:getRandomElem i=
set /A i = n * %random% / 32768 + 1
if !gameID[%i%]! equ 0 goto getRandomElem
exit /B

You may review a detailed explanation on array management in Batch files at this post.

Andere Tipps

@ECHO OFF
SETLOCAL enabledelayedexpansion
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="

FOR /f "tokens=1,2delims=[]" %%a IN ('type q23645114.txt^|find /n /v "" ') DO SET $%%a=%%b&SET max=%%a

SET /a games=5

:choose
SET /a index=(%RANDOM% %% %max%) + 1
IF NOT DEFINED $%index% GOTO choose
ECHO START sam.game.exe !$%index%!
SET "$%index%="
SET /a games-=1
IF %games% gtr 0 GOTO choose

GOTO :EOF

You don't say where you're getting your game numbers, so I used a file named q23645114.txt containing this data for my testing.

63800
214340
25800
8500
212680
4000
41800
220
25890
219150

The first thing this routine does is clear out any variable that starts $

It then reads the file q23645114.txt and uses find to convert it to [1]63800 [2]214340 ...

(the /n installs the [number] and the /v "" says 'find all lines not containing nothing')

Then, using [ and ] as delimiters, it assigns 1 to the first metavariable %%a and 63800 to the second, %%b. and then sets $1 to 63800 and so forth for the entire file. It also tracks the maximum number (=number of lines) in max

Then sets the number of games (to 5 for testing - your choice for any other value...)

then chhoses a random number 0..(max-1) by invoking the modulus operator %% - and adds one

Looks to see whether $thatindex is set. If it isn't, chooses again.

If it is set, then start that game-number, clears the game-number from the array, counts one game started and chooses again untill the required number have commenced.

(The code only echoes the start for testing purposes. - Change echo start to start after verifying to actually start the games.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top