Jump to content

Send file names in folder in random order


address
 Share

Recommended Posts

Yeah, I was about to post a correction, I found that your test wasn't testing correctly for my function either, which is why it came back so fast. Using the file search function it was even slower.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yeah, I was about to post a correction, I found that your test wasn't testing correctly for my function either, which is why it came back so fast. Using the file search function it was even slower.

It was just a rough test. I hope to find the link to the original post where this was discussed. I would like to read that thread again. I'll PM you the link when I find it.

Edit

Here's the thread:

Learn how to Send file names in folder in random order

Some solutions have already been provided for you. You still need to understand how the code works to be able to use it. This is where the tutorials come in handy. I think there's enough for you to chew over in this thread. I need you to tell me what the code is for if you expect me to spend any more time on it. Edited by czardas
Link to comment
Share on other sites

If you mean what real life implementation could be - like to fill bases of numerous financial programs 1C etc. But this is rather learning tool. For example, as I understand if you use function they will repeat forever so I will try do without or will try to find out how to stop it after one sending etc.

Edited by address
Link to comment
Share on other sites

#include <Array.au3>
For $i = 0 To 99
    $iFlag = Random(0,1,0)
    ConsoleWrite("line 1" & @LF) ; Line 1
    ConsoleWrite("line 2" & @LF) ; Line 2
    If $iFlag > 0.7 Then
        _RunTest() ; Call the function here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    EndIf
    ConsoleWrite("line 5" & @LF) ; Line 5
Next
Func _RunTest()
    Local $sSource = "d:/Misc/Pics/Old comp"
    Local $search = FileFindFirstFile($sSource & "/*.*")
    ; Check if the search was successful
    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Exit
    EndIf
    Local $sDelimStr = "", $file
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $sDelimStr &= $file & "*" ; Choose a delimiter which can't possibly appear in the file name.
    WEnd
    FileClose($search)
    ; Close the search handle
    Local $aArray = StringSplit(StringTrimRight($sDelimStr, 1), "*", 2)
    ;_ArrayDisplay($aArray, "Before Shuffling")
    _ArrayRandomShuffle($aArray)
    ;_ArrayDisplay($aArray, "After Shuffling")
    _WaitSend($sSource, $aArray) ; <== Added
EndFunc
Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc
Func _WaitSend($sSource, $aArray, $iSleep = 1000)
    For $i = 0 To UBound($aArray) -1
        Sleep($iSleep) ; Can also go after the next line.
        Send($sSource & "/" & $aArray[$i] & @CRLF)
    Next
EndFunc

So as of now I have no idea how to stop infinite filename sending.

Link to comment
Share on other sites

#include <Array.au3>
#include <File.au3>

For $i = 0 To 99
    $iFlag = Random(0,1,0)
    ConsoleWrite("line 1" & @LF) ; Line 1
    ConsoleWrite("line 2" & @LF) ; Line 2
    If $iFlag > 0.7 Then


  $Dir = 'C:\Windows\System32\'
$Files = _FileListToArray($Dir, '*', 1)
    Sleep(1000)
    Send($Dir & $Files[Random(1, $Files[0], 1)])


    EndIf
    ConsoleWrite("line 5" & @LF) ; Line 5

Next

This one acts bad. Often sends the same filename.

#include <File.au3>
$Dir = 'C:\Windows\System32\'
$Files = _FileListToArray($Dir, '*', 1)
While 1
    Sleep(1000)
    Send($Dir & $Files[Random(1, $Files[0], 1)])
WEnd

And when it's a pure random loop it repeats filenames after all, not perfect.

Edited by address
Link to comment
Share on other sites

#include <File.au3>
#include <Array.au3>
For $i = 0 To 55
    ConsoleWrite("1" & @LF) ; Line 1

$sDir = "d:MiscPics11"
$aFiles = _FileListToArray($sDir, '*', 1)
_ArrayDelete($aFiles,0)
Do
    ConsoleWrite("2") ; Line 2
    $iFlag = Random(0,1,0)
    If $iFlag > 0.7 Then
        ;_ArrayDisplay($aFiles)
        $iRandom = Random(0, UBound($aFiles)-1,1)
        ;ConsoleWrite($iRandom & @TAB)
        ConsoleWrite(@CRLF & ">>>>>>>>>>>>>>>>>>>" & $sDir & $aFiles[$iRandom]& @CRLF) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        _ArrayDelete($aFiles,$iRandom)
        Sleep(10)
    EndIf
    ConsoleWrite("3") ; Line 3
until UBound($aFiles) = 0
    ConsoleWrite("4") ; Line 4
Next

This one does list all files without repetition. But it lists lines 1 and 4 only one time, not 55 times. Second problem is that after it lists all files in folder it goes again, I would like it to stop after he lists all files but For Next loop should continue. Help please.

Link to comment
Share on other sites

Every time you run through the For loop, you're getting the list of the files back into the array, so of course it's going to list them all again. If you move this

$sDir = "c:temp"
$aFiles = _FileListToArray($sDir, '*', 1)
_ArrayDelete($aFiles, 0)

outside of the For loop, that won't happen, but the script will crash after the first run through because there's nothing in the array any longer.

I can't for the life of me figure out what the purpose of this script is. If you're going to run through a loop 55 times but only want it to do anything the first time through it what's the point?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Firstly, the loop was not infinite. You asked for a sleep of 10 seconds and the loop runs 100 times. I also don't know how many files there are in your folder, it certainly isn't infinite. If the code were to run to the end, you would have between 0 and 100 times the number of file paths from the folder written to the console. Also it would have taken over 15 minutes.

Now this loop runs 56 times.A randomly selected file path is inserted between lines 2 and 4. The previous action is skipped at irregular intervals. The loop continues to run after all files in the directory have been written to the console. No file path occurs twice.

#include <File.au3>
#include <Array.au3>

$sDir = "d:MiscPics11"
$aFiles = _FileListToArray($sDir, '*', 1)
;Dim $aFiles[9] = [8,"file_1.txt","doc_2.doc","new_3.doc","text_4.txt","spread sheet_5.xml","word doc_6.doc","image_7.jpg","anim_8.gif"]

$iCount = 0
$iMax = $aFiles[0] ; The number of files in the directory
_ArrayDelete($aFiles,0)

_ArrayRandomShuffle($aFiles)

For $i = 0 To 55
    ConsoleWrite("1" & @CRLF) ; Line 1
    ConsoleWrite("2" & @CRLF) ; Line 2
    If $iCount < $iMax And Random(0, 1, 1) Then ; Skip the next two lines randomly
        ConsoleWrite($sDir & $aFiles[$iCount]& @CRLF) ; Line 3 is a random path
        $iCount += 1
    EndIf
    ConsoleWrite("4" & @CRLF) ; Line 4
Next

Func _ArrayRandomShuffle(ByRef $aArray)
    Local $iRandom, $iBound = UBound($aArray) -1
    For $i = 0 To $iBound
        $iRandom = Random(0, $iBound, 1)
        If $i <> $iRandom Then _ArraySwap($aArray[$i], $aArray[$iRandom])
    Next
EndFunc

As this is just an exercise, I saw fit to change the 70 / 30 weighted argument to a 50 / 50 chance of skipping the line where a file path would normally go. If you wanted to, you could place the text "line skipped" in the appropriate places, as an exercise.

Edited by czardas
Link to comment
Share on other sites

Now this loop runs 56 times.A randomly selected file path is inserted between lines 2 and 4. The previous action is skipped at irregular intervals. The loop continues to run after all files in the directory have been written to the console. No file path occurs twice.

Wow. Why are you so awesome? Thanks so much.

Can you tell me please if source of text line could be not filename but from text file (any text, not just filename with path) and when it sent randomly, can it be deleted from said text file IN BACKGROUND so I actually won't see how AutoIt opens txt etc?

So when script is exit and then I run it again next day it would send random lines but it wouldn't send already sent lines because they are deleted.

Edited by address
Link to comment
Share on other sites

Now your asking quite a lot. Yes you can write to and delete things from files without viewing them. You need to look at FileWriteLine in the help file. Also look at the related functions such as FileRead and FileReadLine. Run the help file examples and learn the differences between them and how they work. Spend a few hours on this and make something simple, so we can help you if you get stuck. :)

Edited by czardas
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...