Jump to content

Random selection of filename without replacement; loop; exit


Recommended Posts

Autoit forum,

I think this is a fairly simple question, but I am very new to programming, and I would greatly appreciate some script examples for this problem. I would like to sequentially open instances of Notepad.exe, wait for it to be filled and closed by a user, then open a new instance of Notepad.exe (please see sample script below). However, I would like the .txt files (e.g., file1.txt, file2.txt) to be selected without replacement in a random order.

I'm thinking that I can loop this basic 3-liner, and define the to-be-opened filename as a variable linked to an array (from which to be selected randomly without replacement). However, I am very unskilled. Is there such a code that would loop the 3-liner until it has selected all of the filenames in random order without replacement from the array... then close?

Thanks a bunch in advance!

Frank

Run("notepad.exe file1.txt")

[the user types some information, and closes the .txt file]

WinWaitClose("file1.txt - Notepad")

Run("notepad.exe file2.txt")

[the user types some information, and closes the .txt file]

WinWaitClose("file2.txt - Notepad")

Run("notepad.exe file3.txt")

[the user types some information, and closes the .txt file]

WinWaitClose("file3.txt - Notepad")

Edited by frankbosco
Link to comment
Share on other sites

Dim $I, $IMax = 5; 5 Times, Increase or Decrease or Get from User if necessary
Dim $Filename
For $I = 1 to $IMax
      $Filename = "file" & $I & ".Txt"
      Run("notepad.exe " & $Filename)
     ; [the user types some information, and closes the .txt file]
      WinWaitClose($Filename & " - Notepad")
Next;$I

Link to comment
Share on other sites

Dim $I, $IMax = 5; 5 Times, Increase or Decrease or Get from User if necessary
Dim $Filename
For $I = 1 to $IMax
      $Filename = "file" & $I & ".Txt"
      Run("notepad.exe " & $Filename)
; [the user types some information, and closes the .txt file]
      WinWaitClose($Filename & " - Notepad")
Next;$I

Thanks both of you for your excellent suggestions! I have implemented both of your suggestions, but have one remaining issue -- I need the script to randomly select the text files (for example, on the first run of the program, it may open the text files in the order: 2, 3, 1; on the second program run it may select 3, 1, 2, etc.)

Any insight would be greatly appreciated!

Thanks in advance!

Currently used code:

Dim $I, $IMax = 3

; (3 Times, Increase or Decrease or Get from User if necessary)

Dim $Filename

For $I = 1 to $IMax

$Filename = "file" & $I & ".txt"

Run("notepad.exe " & $Filename)

; [the user types some information, and closes the .txt file]

WinWait($Filename & " - Notepad")

WinWaitClose($Filename & " - Notepad")

Next;$I

Edited by frankbosco
Link to comment
Share on other sites

It's not terribly random, but here it is. The larger the value of $iMax, the more random the numbers may be:

#include <Array.au3>
Dim $i, $iMax = 3 ;(3 Times, Increase or Decrease or Get from User if necessary)

Dim $FileNameArray[$iMax]
For $x = 0 To UBound($FileNameArray) - 1
    $FileNameArray[$x] = "File" & ($x + 1) & ".txt"
Next


For $i = 1 To $iMax
    $CurrentNumber = Random(0, UBound($FileNameArray) - 1, 1)
    $CurrentName = $FileNameArray[$CurrentNumber]
    FileWrite($FileNameArray[$CurrentNumber], "")
    Run("notepad.exe " & $CurrentName)  ; [the user types some information, and closes the .txt file]
    WinWait($CurrentName & " - Notepad")
    WinWaitClose($CurrentName & " - Notepad")
    _ArrayDelete($FileNameArray, $CurrentNumber)
Next    ;$i
Link to comment
Share on other sites

Or you could just jumble your array up front and go from there...

#include <File.au3>
#include <Array.au3>
Dim $SourceArray, $tmp  

_FileReadToArray(@ScriptDir & "\files.txt", $SourceArray)
;_ArrayDisplay($SourceArray); filelist in

For $i= $SourceArray[0] to 1 Step -1
    $new = Random(1, $i + 1, 1)
    $tmp = $SourceArray[$i]
    $SourceArray[$i] = $SourceArray[$new]
    $SourceArray[$new] = $tmp
Next
;_ArrayDisplay($SourceArray); filelist out

For $i = 1 to $SourceArray[0]
; do your notepad stuff here
Next

Edit: code tweak. grr!!!

Edited by Spiff59
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...