Jump to content

How to select the first file in a folder


dunk6
 Share

Recommended Posts

Hi all,

How to check the first file in the folder and doing and doing zip to it?

thanks

Hello dunk6!

In order to find the first file, use the command FileFindFirstFile.

Here is the AutoIT Help Example :

; Shows the filenames of all files in the current directory.
$search = FileFindFirstFile("*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    MsgBox(4096, "File:", $file)
WEnd

; Close the search handle
FileClose($search)

Also, you might check this.

Edited by KaZo
Link to comment
Share on other sites

In order to find the first file, use the command FileFindFirstFile.

Im sorry, still cannot figure out how to implement this. For example if i have a function call testFunc(), how should i make the first file invoke the function first, then the next file, and next2.

testFunc() is trying to open context menu and select some shortcut.

$StartDir = "C:\Archive\"
$search = FileFindFirstFile($StartDir & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop    
    testFunc()
    ;$file2 = FileOpen("test.txt", 1)   
    ;FileWriteLine($file2, $file & @CRLF)           
    ;FileClose($file2)
        
WEnd

; Close the search handle
FileClose($search)
Func testFunc ()
    MouseClick("menu")
    $count = 1
    Do
        Send("{DOWN}")
        $count = $count + 1  

    Until $count > 7
    Send("{ENTER}") 
EndFunc
Link to comment
Share on other sites

I've struggle with these "find first file" routines myself. I keep at it and now I use it a lot, though I still struggle.

The search returns $file

You call a function using $file and probably $StartDir

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

CopyMyFile()

WEnd

Func CopyMyFile()

FileCopy($StartDir & "\" & $file,"c:\" & $file)

The loop will continue passing $file to the function until there are no files left in $StartDir. With more searching you will find that it can be made to search sub-directories as well.

Hope that gives you the clues you need...

Im sorry, still cannot figure out how to implement this. For example if i have a function call testFunc(), how should i make the first file invoke the function first, then the next file, and next2.

testFunc() is trying to open context menu and select some shortcut.

$StartDir = "C:\Archive\"
$search = FileFindFirstFile($StartDir & "*.*")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop    
    testFunc()
    ;$file2 = FileOpen("test.txt", 1)   
    ;FileWriteLine($file2, $file & @CRLF)           
    ;FileClose($file2)
        
WEnd

; Close the search handle
FileClose($search)
Func testFunc ()
    MouseClick("menu")
    $count = 1
    Do
        Send("{DOWN}")
        $count = $count + 1  

    Until $count > 7
    Send("{ENTER}") 
EndFunc

Link to comment
Share on other sites

Welcome to the forum OregonJohn.

If you and dunk6 could wrap your AutoIt code in autoit tags instead of code tags, the forum software will work its magic on the code.

AutoIt tabs start with [autoit ] <remove the space before the bracket.

AutoIt tabs end with [/autoit ] <remove the space before the bracket.

MsgBox(0, '', '')
Part of the magic is the function MsgBox is now a link to the online docs for that function.

Also, if you are using the full free version of the SciTE editor (SciTE4AutoIt3):

http://www.autoitscript.com/autoit3/scite/downloads.shtml

Then you can press Ctrl-T to "Tidy up" your code before posting it to the forum.

Edited by herewasplato

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

how do you define a "1st file" anyway? is it the alphabetical name that is the criteria, or size or what? how is the folder arranged?

if its just file names then you can just use the filegetlist command and then take the 1st row of the array as the 1st file.

Link to comment
Share on other sites

I've struggle with these "find first file" routines myself. I keep at it and now I use it a lot, though I still struggle.

...

Hope that gives you the clues you need...

Thank for idea, Jz wannna know, is there a function like 'for each..' in the AutoIt?

Welcome to the forum OregonJohn.

If you and dunk6 could wrap your AutoIt code in autoit tags instead of code tags, the forum software will work its magic on the code.

Ahh.. very sorry, i though it was the same. I'll keep that in mind . thanks

how do you define a "1st file" anyway? is it the alphabetical name that is the criteria, or size or what? how is the folder arranged?

if its just file names then you can just use the filegetlist command and then take the 1st row of the array as the 1st file.

I'm not using any sorting method for the folder. it just a folder and inside there contain a file. '1st file', i'm referring to he beginning of the first file inside that folder. sorry, i dont know how to explain in correct terms. I'll try go tru with filegetlist command first!.

thanks all

Edited by dunk6
Link to comment
Share on other sites

if its just file names then you can just use the filegetlist command and then take the 1st row of the array as the 1st file.

Hi..could not find anyhtg reagarding filegetlist command in AutoIt/vba. is it the write name?

I can select the file and apply some process on it, but got problem, eg, after sometime, the moveFiles function is not execute. Can i forced the FileMove() no matter conditions are? can tell me wat's wrong n way to fix it?

Func Encrypt()
    $objShell = ObjCreate("Shell.Application")
    $objFolder = $objShell.Namespace($source)
    $colItems = $objFolder.Items
    For $i = 0 to $colItems.Count - 1
        $colItems.Item($i).InvokeVerbEx("Encrypt")
        Sleep(500)
        ;send password
        ControlSend("xxxx",  "", "Edit2", $pass)
        Sleep(500)  
        ;confirm password
        ControlSend("xxxx",  "", "Edit3", $pass)
        Sleep(500)
        ;execute program
        Send("{ENTER}")
        Sleep(500)
        Send("{ENTER}")
    Next
    Sleep(500)
    FileMove( $source &"*.pac" , $dest,1)
endFunc
Edited by dunk6
Link to comment
Share on other sites

it's actually _FileListToArray, under User Defined Function Reference menu tree...

by default, this function return both files and folders... use flag = 1 if you want files only list

Edited by Mison

Hi ;)

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...