Jump to content
Sign in to follow this  
frew

How to run random scripts from a scripts folder?

Recommended Posts

frew

Hello,

I have a folder with 10 scripts in it.

I want to create a script that will randomly choose one of the ten scripts and run it, short pause, then randomly choose another one of the ten scripts and run it.

I don't mind if the same script is run more than once in a row, ie there can be repeats.

I tried using Random(1,10, 1) in combination with If Else...ElseIf

...but I was wondering if there might be a better way, since if I get hundreds of scripts to choose from, that's a lot of Ifs and ElseIfs etc.

Thank you very much for any ideas.

frew

Share this post


Link to post
Share on other sites
stampy

Why not use _FileListToArray to build a list of the files in an array. Use random to select a subscript in the array and that would be the file.

Share this post


Link to post
Share on other sites
frew

Thanks for the idea stampy.

Thanks for the code Marlo. I'm trying to get it to work.

It does not seem to run the files though.

I saved this as an au3 and ran it but it does nothing:

#include <File.au3>

Dim $iScriptNum, $aFileList = _FileListToArray("C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1\", "*.au3")

While True

$iScriptNum = Random(1, $aFileList[0], 1)

;$aFileList[$iScriptNum] will be the path of one of the random scripts.

WEnd

What else do I need to do? I was hoping to run this script and have the scripts in the folder that I point to above start running at random...one, then another, then another, having them start to run randomly one by one, and when one ends the next randomly chosen au3 runs...on and on like that.

Thanks for any other ideas for me to try.

frew

Share this post


Link to post
Share on other sites
Nahuel

Of course it doesn't work. He didn't give you the whole code, he gave you a way to start. Here would be a closer approach:

#include <File.au3>

Dim $iScriptNum, $aFileList = _FileListToArray("C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1", "*.au3")
While True
    $iScriptNum = Random(1, $aFileList[0], 1)
;$aFileList[$iScriptNum] will be the path of one of the random scripts.
    ShellExecute($aFileList[$iScriptNum],"","","Run")
WEnd

Get used to not using trailing backslashes.

Share this post


Link to post
Share on other sites
stampy

You'll need to add a call or run to the code. Right now it only picks a random number but then does nothing with it.

#include <File.au3>

Dim $iScriptNum, $aFileList = _FileListToArray("Scripts path", "*.au3")

While True
    $iScriptNum = Random(1, $aFileList[0], 1);<<<<<pick the random number
    ;$aFileList[$iScriptNum] will be the path of one of the random scripts.
    ;runwait - call - run here <<<<<<<<<<<<<<<<<<<<<< command to do something here.
        ;the choice will depend on what you want it to do and what format your scripts are in.
    ;ie:   runwait($aFileList[$iScriptNum]) .......... something like that
WEnd

Share this post


Link to post
Share on other sites
frew

Thank you very much Nahuel for the extra code.

And thank you stampy for your clarification.

Okay, this works! Very nice.

I just had to tinker around with it a bit.

Turns out what was throwing me off a bit too was that, (perhaps just on my older system Windows 98SE),

nothing worked if the .au3 files had spaces in their names.

Also, he whole macro thing is a bit tricky (the path,or filename? as a parameter?...in helpfile)...but it turns out if I put the path after the $aFileList[$iScriptNum], then it works.

#include <File.au3>

Dim $iScriptNum, $aFileList = _FileListToArray("C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1", "*.au3")
While True
    $iScriptNum = Random(1, $aFileList[0], 1)
;$aFileList[$iScriptNum] will be the path of one of the random scripts.
    ShellExecute(@AutoItExe, $aFileList[$iScriptNum], "C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1")
    Sleep(5000)
WEnd

Thanks again so much for your help with this.

frew

Share this post


Link to post
Share on other sites
Valuater

This should fix any "space" problem...

#include <File.au3>

Dim $iScriptNum, $aFileList = _FileListToArray("C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1", "*.au3")
While True
    $iScriptNum = Random(1, $aFileList[0], 1)
    $Runner = FileGetShortName($aFileList[$iScriptNum]) ; will be the path of one of the random scripts.
    ShellExecute(@AutoItExe, $Runner, "C:\Program Files\AutoIt3\Scripts_AutoIT3_my_1\random chain 1")
    Sleep(5000)
WEnd

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
frew

Hmmm....does not work here.

Thank you though Valuater.

I run that script and it works fine when the three files in the folder have underscores, no spaces.

When I rename them replacing the underscores with spaces, I get

AutoIt Error

Line 0 (File "C:\Program Files\AutoIt3\Scripts_AutoIt3_my_1\random chain 1\C"):

Error: Error opening the file.

Could me using Windows 98SE have anything to do with it? (I know...)

Thank you very much for any other ideas.

For now I may just be sure to create script names with no spaces in them.

frew

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.