Sign in to follow this  
Followers 0
leuce

How to FileOpen a whole directory

11 posts in this topic

G'day everyone

As I understand it, you can't use a filehandle with FileOpen. You have to know the filename when you write the script, right? But I want to FileOpen an entire directory of files (one after another, in a looped script), and I don't know in advance what the filenames are. I'm using the $foo = FileFindFirstFile("*.*") and FileFindNextFile($foo) method to determine the file names, but how can I open each of the files if I'm not allowed to use filehandles in FileOpen?

Thanks

Samuel

Share this post


Link to post
Share on other sites



Your only allowed to have so many files open... I can't remember the limit off hand.

This returns a 2 Dim array, [0][N] returns the Full FilePath and file and [1][N] returnst that files handle.

Local $FilesAndFileOpenHandles = _FileOpenReturnFiles(@DesktopDir & '\DummyFolder')
If IsArray($FilesAndFileOpenHandles) Then
    For $i = 1 To UBound($FilesAndFileOpenHandles, 2) - 1
        MsgBox(64, 'Info', _
            'File Name: ' & @CR & $FilesAndFileOpenHandles[0][$i] & @CR & @CR & _
            'File Open Handle: ' & @CR & $FilesAndFileOpenHandles[1][$i])
            ;Should go ahead and close them now lol
            FileClose($FilesAndFileOpenHandles[1][$i])
    Next
EndIf

Func _FileOpenReturnFiles($DirectoryLocation, $iOpenFlag = 0)
    Local $FindFirst = FileFindFirstFile($DirectoryLocation & '\*.*')
    Local $FileOpenHandle[2][1], $iCount = 1
    While 1
        $FindNext = FileFindNextFile($FindFirst)
        If @error Then ExitLoop
        $iCount += 1
        ReDim $FileOpenHandle[2][$iCount]
        $FileOpenHandle[0][$iCount - 1] = $DirectoryLocation & '\' & $FindNext
        $FileOpenHandle[1][$iCount - 1] = FileOpen($DirectoryLocation & '\' & $FindNext, $iOpenFlag)
    WEnd
    Return $FileOpenHandle
EndFunc

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Your only allowed to have so many files open... I can't remember the limit off hand.

Thanks for that bit of info, but I'll have one file open at a time. There'll be a FileOpen at the start of the loop, then the file contents will be processed, and then there'll be a FileClose at the end of the loop.

I must admit that I'm a bit lost when it comes to your given solution... I'm not the brightest light in the house. Is _FileOpenReturnFiles a beta feature? I can't find it in my stableversion help file.

==

Here's another question related to my first one. How can I tell a script to FileOpen a file which has been dragged and dropped onto it (if it's an EXE'd file)? I know I'm supposed to use $CmdLine[1] for that, but again my dilemma is that I can't use FileOpen ($CmdLine[1], 1).

Samuel

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

1. No I just wrote that UDF

2. Are you sure your getting the full path, please look how I added Directory + File

3.

FileOpen('"' & $CmdLine[1] & '"', 1)
or
FileOpen(FileGetShortName($CmdLine[1]), 1)
should work.

Edit:

Are you saying you Litterally want to see these files open?

Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

2. Are you sure your getting the full path, please look how I added Directory + File

I'll have another look, but it is rather complex for me.

3.

FileOpen('"' & $CmdLine[1] & '"', 1)
or
FileOpen(FileGetShortName($CmdLine[1]), 1)
should work.
They don't.

Are you saying you Litterally want to see these files open?

Heh-heh, no. But thanks for trying to cover all bases :-)

==

See, here's a piece of code that I had hoped would count the lines of the file, but instead it gives me an error:

#include <Array.au3>
#include <file.au3>

$dirlist1 = FileFindFirstFile("*.*")  

; These two lines take care of "." and ".."
$dirlist2 = FileFindNextFile($dirlist1) 
$dirlist2 = FileFindNextFile($dirlist1) 

While 1

$dirlist2 = FileFindNextFile($dirlist1) 
MsgBox (0, "", $dirlist2, 10)

$src0 = FileOpen ('"' & $dirlist2 & '"', 0)
If $src0 = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$src0count = _FileCountLines ($src0)
MsgBox (0, "", $src0count, 10)

WEnd

Exit

The result is "Unable to open file".

Thanks for your comments.

Edited by leuce

Share this post


Link to post
Share on other sites

This returns a 2 Dim array, [0][N] returns the Full FilePath and file and [1][N] returnst that files handle...

If your piece of code was intended to be able to be run as a complete code, then it doesn't work. I get "Error: Invalid File handle used" on line 16, which is $FindNext = FileFindNextFile($FindFirst). Since that level of code is still too advanced for me, I haven't been able to debug it.

Thanks for trying, though.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Pass the filename to _FileCountLines().

$src0count = _FileCountLines ($dirlist2)
oÝ÷ ٩ݶ¼°ØhºÚ®¢×¬¢{axX¥xê^«­¢+Ø(ÀÌØíÍÉÀô¥±=Á¸ ÀÌØí¥É±¥ÍÐÈ°À¤)%ÀÌØíÍÉÀô´ÄQ¡¸(5Í    ½à À°ÅÕ½ÐíÉɽÈÅÕ½Ðì°ÅÕ½ÐíU¹±Ñ¼½Á¸¥±¸ÅÕ½Ðì¤(á¥Ð)¹%(
Edited by MHz

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I reworked your script some. It had a number of issues.

#include <Array.au3>
#include <file.au3>

$dirlist1 = FileFindFirstFile("*.*")
If $dirlist1 = -1 Then
    MsgBox(0, "Error", "Unable to open search handle.")
Else
    While 1
        $dirlist2 = FileFindNextFile($dirlist1)
        If @error Then ExitLoop
        If $dirlist2 = '.' Or $dirlist2 = '..' Then ContinueLoop
        MsgBox (0, "", $dirlist2, 10)
        ;
        $src0 = FileOpen ('"' & $dirlist2 & '"', 0)
        If $src0 = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        Else
            $src0count = _FileCountLines ($dirlist2)
            MsgBox (0, "", $src0count, 10)
            FileClose($src0)
        EndIf
    WEnd
EndIf

Exit
Edited by MHz

Share this post


Link to post
Share on other sites

I reworked your script some. It had a number of issues.

Thanks. When I run your version of the script, it tells me the name of the first file, and then it says "Unable to open file", and then it exits.

Share this post


Link to post
Share on other sites

Thanks. When I run your version of the script, it tells me the name of the first file, and then it says "Unable to open file", and then it exits.

Because it's missing the path to that file.

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

If your compiling your script, then it maybe finding the script in use, and exits out as it cannot open it. The script is using the current working directory.

This will skip the script

#include <Array.au3>
#include <file.au3>

$dirlist1 = FileFindFirstFile("*.*")
If $dirlist1 = -1 Then
    MsgBox(0, "Error", "Unable to open search handle.")
Else
    While 1
        $dirlist2 = FileFindNextFile($dirlist1)
        If @error Then ExitLoop
        If $dirlist2 = '.' Or $dirlist2 = '..' Then ContinueLoop
        If $dirlist2 = @ScriptName Then ContinueLoop
        MsgBox (0, "", $dirlist2, 10)
        ;
        $src0 = FileOpen ('"' & $dirlist2 & '"', 0)
        If $src0 = -1 Then
            MsgBox(0, "Error", "Unable to open file.")
            Exit
        Else
            $src0count = _FileCountLines ($dirlist2)
            MsgBox (0, "", $src0count, 10)
            FileClose($src0)
        EndIf
    WEnd
EndIf

Exit

Perhaps turning the Exit in the loop into a ContineLoop may work better as your not exitting for the sake of 1 in use file causing grief.

Edited by MHz

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  
Followers 0