+ Post New Thread
Results 1 to 13 of 13
Scripts Thread, executing a random command in a batch file in Coding and Web Development; Hi all, Not entirely sure this is possible but essentially what I'd like to do is have my login script ...
  1. #1

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    18

    executing a random command in a batch file

    Hi all,

    Not entirely sure this is possible but essentially what I'd like to do is have my login script pick one of 5 lines and execute it.

    My idea was using a command to pick a random number (1-5) then calling in one of five external batch files (named batch1.bat, batch2.bat etc) and running it.

    Possible? It's just a bit of fun to randomise pupil desktops with BGInfo

  2. #2

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740

    Re: executing a random command in a batch file

    You will have to try and fiddle around with the %Random% global variable - I am sure something could be done - I did a random 6 letter thing in AutoIT. You are welcome to edit and mess around with it to suit your requirements:

    Code:
    ; AutoIt Version:  	3.00
    ; Language:        	English
    ; Platform:        	WinXP
    ; Author:          	Matt 
    ; Script Function: 	Random Letters Generator For Possible Password Use
    
    $__msgbox = MsgBox(4, 'Random Letter Generator By Matt', 'This Will Generate 6 Random Letters For You, Continue?')
    if ($__msgbox = 7) Then
       Exit
    EndIf
    SplashTextOn("Random Letter Generator By Matt", "Thinking Of 6 Letters, Please Wait...", 300, 75)
    Sleep(3000)
    Dim $i = 0, $word = ""
    For $i = 1 To 6
       If Random() < 0.5 Then
          $Letter = Chr(Random(Asc("A"), Asc("Z")))
       Else
          $Letter = Chr(Random(Asc("a"), Asc("z")))
       EndIf
       $word = $word & $Letter
    Next
    Splashoff()
    Msgbox(64, "Your Random Letters Are:", $word)

  3. #3

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740

    Re: executing a random command in a batch file

    Just checking, a certain Rob van der Woude [ I must E-mail and thank him one day for his excellent website I have been using for years ] has done something which may help. Check out:

    http://www.robvanderwoude.com/batexamples_r.html#R

  4. #4

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    18

    Re: executing a random command in a batch file

    Cheers Matt, looks good! *bookmarks site*

  5. #5

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    18

    Re: executing a random command in a batch file

    Hi chaps here's what I hacked together from the link Matt gave me, just tested it and it appears to work.
    --------------------------------------
    @ECHO OFF
    ECHO.
    IF NOT [%1]==[] GOTO Syntax
    VER | FIND "Windows 2000" >NUL
    IF NOT ERRORLEVEL 1 GOTO Start
    VER | FIND "Windows XP" >NUL
    IF NOT ERRORLEVEL 1 GOTO Start

    :Start
    SET NAT=%Random%
    IF [%NAT%]==[] GOTO Syntax
    IF %NAT% GTR 5 SET /A NAT = 1%NAT:~-1% -10
    rem SET NAT

    IF '%NAT%'=='0' GOTO Nat0
    IF '%NAT%'=='1' GOTO Nat1
    IF '%NAT%'=='2' GOTO Nat0
    IF '%NAT%'=='3' GOTO Nat1
    IF '%NAT%'=='4' GOTO Nat0
    IF '%NAT%'=='5' GOTO Nat1
    IF '%NAT%'=='6' GOTO Nat0
    IF '%NAT%'=='7' GOTO Nat1
    IF '%NAT%'=='8' GOTO Nat0
    IF '%NAT%'=='9' GOTO Nat1

    Else goto Nat0

    goto end

    :Nat0
    "\\staff\NETLOGON\wallpapers\bginfo.exe" "\\staff\NETLOGON\wallpapers\Startup.bgi" /timer:0
    GOTO End

    :Nat1
    "\\staff\NETLOGON\wallpapers\bginfo.exe" "\\staff\NETLOGON\wallpapers\Startup2.bgi" /timer:0
    GOTO End

    :End

    ---------------------------------

    Obviously you could have it randomising up to 10 different BGinfo setups but at the moment I only have two and have gotten side tracked as always! If I'm right the "else goto nat 0" shouldn't be needed as it can only produce results from 0-9, but I've been wrong before!

  6. #6

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740

    Re: executing a random command in a batch file

    God, I miss using GOTO commands.......... :-)

  7. #7

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    18

    Re: executing a random command in a batch file

    yeah I should stop using the login batch files but you can have so much fun with them mate

  8. #8
    Iain's Avatar
    Join Date
    Oct 2006
    Location
    Warwickshire
    Posts
    185
    Thank Post
    28
    Thanked 92 Times in 52 Posts
    Rep Power
    31

    Re: executing a random command in a batch file

    A possible vbscript solution could be:

    random.vbs:
    Code:
    Dim objShell, intOption
    
    Set objShell = CreateObject("WScript.Shell")
    Randomize()
    intOption = Int(5*Rnd())
    
    Select Case intOption
    	Case 0
    		' Option 1
    		objShell.Run "batch1.bat"
    	Case 1
    		' Option 2
    		objShell.Run "batch2.bat"
    	Case 2
    		' Option 3
    		objShell.Run "batch3.bat"
    	Case 3
    		' Option 4
    		objShell.Run "batch4.bat"
    	Case 4
    		' Option 5
    		objShell.Run "batch5.bat"
    End Select
    
    Set objShell = Nothing
    Iain

  9. #9

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,236
    Thank Post
    1,057
    Thanked 1,067 Times in 624 Posts
    Rep Power
    740

    Re: executing a random command in a batch file

    I wonder how many of us have written Lottery Number Generators ?
    Last count I had done 3 - sad :downtown:

  10. #10
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23

    Re: executing a random command in a batch file

    In PHP:

    Code:
    <?php 
    $num = array();
    
    for($i=1;$i<=49;$i++)
    {
        array_push($num,$i);
    }
    
    shuffle($num);
    
    echo "<h1>Sad Little Lottery Generator for mattx</h1>
    ";
    
    for($i=0;$i<6;$i++)
    {
        echo $num[$i] . " ";
    }
    
    echo "
    
    Powered By Geekiness(tm)";
    
    
    ?>
    Victory is mine!

  11. #11

    Join Date
    Mar 2007
    Location
    Central Scotland
    Posts
    829
    Thank Post
    12
    Thanked 8 Times in 8 Posts
    Rep Power
    18

    Re: executing a random command in a batch file

    I knew someone was going to do that!

  12. #12
    Friez's Avatar
    Join Date
    Dec 2006
    Posts
    839
    Thank Post
    22
    Thanked 22 Times in 21 Posts
    Rep Power
    23

    Re: executing a random command in a batch file

    Quote Originally Posted by Halfmad
    I knew someone was going to do that!
    It had to be done TBH!

  13. #13

    Join Date
    Nov 2007
    Posts
    8
    Thank Post
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Re: executing a random command in a batch file

    Here is a pretty flexible solution for running random files. It looks through a specified folder and picks a file at random out of the list of files in that folder. That way it works if you have 1 file in the folder or 100.

    dim arrImages(5000)
    strSourceFolder = "c:\FolderWithBatchFiles"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWshShell = WScript.CreateObject("WScript.Shell")

    'Get a list of all of the files in the folder
    set objFolder = objFSO.GetFolder(strSourceFolder)
    Set colBatchFolder = objFolder.Files
    For Each objBatchFile in colBatchFolder
    arrBatchFiles(i) = objBatchFile.Name
    i=i+1
    Next
    ' Create a random number from 0 to i
    Randomize
    intRandomNumber = Int(rnd* i)
    'Run that puppy
    WshShell.Run arrBatchFiles(intRandomNumber)

    A good check would be to look at the file extension before tossing it into the array to make sure that it was a .bat file.
    I originally made it to look through my pictures and toss a random one into the registry as my wallpaper, but ran into problems with XP not refreshing it. Because I converted it, the variable names are probably a little wonky.

    Another way would be a modification of what was posted above. Generate a random number from 1 to 5 and then use a case statement. Rather than going to an external file the case statement would call 1 of 5 subroutines that would run the part of the logon script that provides individualization. If needed, any common elements can be meted out to users either before or after that part runs.

    Have a great day.

SHARE:
+ Post New Thread

Similar Threads

  1. Rename a PC from a batch file - Any ideas?
    By MrLudwig in forum Windows
    Replies: 13
    Last Post: 8th December 2007, 02:40 AM
  2. Prommisor - Installing using a batch file/macro?
    By googlemad in forum Windows
    Replies: 21
    Last Post: 3rd December 2007, 08:17 PM
  3. batch file to shutdown PCs
    By macca in forum Windows
    Replies: 5
    Last Post: 3rd May 2007, 09:20 AM
  4. Batch: Text file -> Environment variable?
    By PiqueABoo in forum Scripts
    Replies: 5
    Last Post: 13th December 2006, 11:09 PM
  5. batch file help
    By beeswax in forum Scripts
    Replies: 9
    Last Post: 3rd March 2006, 07:48 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •