Jump to content

FileOpen with Wildcard


kylet90
 Share

Go to solution Solved by Melba23,

Recommended Posts

; filename to read
$source_file = "C:\testing\*.txt"
; open file to read and store the handle
$handle_read = FileOpen($source_file, 0)
; check the handle is valid
If $handle_read = -1 Then
    ; show warning and exit with code 1
    MsgBox(0, @ScriptName, 'failed to open handle to read the file')
    
   Exit 1
EndIf

Hi,

I am trying to use the FileOpen command to open a text file but it doesn't work. I tried searching the documentation but I can't see a way to use a wildcard.

The actual file name is source.txt, but I plan for this process to repeat so it needs to be capable of reading any text file within that directory (there will only be one, at the end of the script I will have the file move to a complete folder).

Thanks

Edited by kylet90
Link to comment
Share on other sites

  • Moderators

kylet90,

You cannot use wildcards with FileOpen - as it returns a handle it can only deal with one file at a time. :)

So I suggest you use _FileListToArray (which does accept wildcards) to get the filename so that you can use it. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

; filename to read
$path = "C:\testing\"
$source_file = _FileListToArray($path, "*", 1)

; open file to read and store the handle
$handle_read = FileOpen($source_file, 0)
; check the handle is valid
If $handle_read = -1 Then
    ; show warning and exit with code 1
    ;MsgBox(0, @ScriptName, 'failed to open handle to read the file')
    MsgBox(0, "Message", $source_file)

I'm obviously doing it wrong, can you shed any light on my mistake?

Link to comment
Share on other sites

  • Moderators
  • Solution

kylet90,

_FileListToArray returns (as it name suggests) an array - so you have to use the correct syntax to access it. ;)

This reads a file in the script folder named "Lines.txt (I already had it in place because I was working on your other problem earlier): :)

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

; filename to read
$path = @ScriptDir & "\"
$source_file = _FileListToArray($path, "*.txt", 1)
; Check we have a return
If Not @error Then
    ; open file to read and store the handle - you said only 1 file so it must be index [1]
    $handle_read = FileOpen($path & $source_file[1], 0)
    ; check the handle is valid
    If $handle_read = -1 Then
        ; show warning and exit with code 1
        MsgBox(0, @ScriptName, 'failed to open handle to read the file')
        Exit
    EndIf
    MsgBox($MB_SYSTEMMODAL, "Read", FileRead($handle_read))
    FileClose($handle_read)
EndIf
Please ask if you still have questions. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

kylet90,

Glad I could help. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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