Jump to content
Sign in to follow this  

How can I serach all files using wildcard

Recommended Posts


I have been looking for a way to list all the files on C drive containing a name where I can use a wild card. I would like to do it with the autoit functions and don't call cmd.exe for this purpose.

To make it clear I am looking for something similar to the Dir command:

Dir /a/b/s C:\afd.sys lists all the files on C drive with the exact name "afd.sys"

Dir /a/b/s C:\afd.sys* lists all the files on c drive with the exact name and also the files with additional extensions like "afd.sys.bak"

Dir /a/b/s C:\*afd.sys lists all the files on c drive with the exact name and also the files with which end with "afd".

Dir /a/b/s C:\*afd.sys* lists all the files on c drive with the exact name and also the files like: maafd.sys.bak

I can manage to use wild card for a specified directory with:

Func Search($current, $file)
Local $search = FileFindFirstFile($current & "\" & $file)
while 1
    Dim $file = FileFindNextFile($search)
    If @error Or StringLen($file) < 1 Then ExitLoop
    $path = $current & "\" & $file
    FileWrite("filelist.txt" , $path & @CRLF)

Local $file = "afd.sys*"
Search ("C:" , $file)

And I can use wild card like $file = "afd.sys*" or $file = "*afd.sys" or $file = "*afd.sys". But it only lists the files in the root of C drive and doesn't go to sub-directories.

I found and adjusted the following, which lists all the the files on C drive when I search for "afd.sys" but I can't use wild card with it:

Func Search($current,$ext)

Local $search = FileFindFirstFile($current & "\*.*")
While 1
    Dim $file = FileFindNextFile($search)
    If @error Or StringLen($file) < 1 Then ExitLoop
    If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") Then
    If StringRight($current & "\" & $file,StringLen($ext)) = $ext then
        FileWrite ("filelist.txt" , $current & "\" & $file & @CRLF)
    If StringInStr(FileGetAttrib($current & "\" & $file), "D") Then
    Search($current & "\" & $file, $ext)

$test = "afd.sys"
search ("c:" , $test)

I couldn't find it and I greatly appreciate any hint, link to a topic, or a script that help me out.

Thanks in advanced.

Share this post

Link to post
Share on other sites


What you are trying to do is a recursive file search. There are plenty of examples on the forum, like this one. ;)


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:


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


Share this post

Link to post
Share on other sites

Here is an example of file search. Count number of file found. Update search progress every 3s

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$result = 0
$nbFiles = 0

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Counter", 189, 74, 388, 325, $WS_POPUP)
$Label1 = GUICtrlCreateLabel($nbFiles, 40, 10, 106, 17, $SS_CENTER)
$Label2 = GUICtrlCreateLabel("Files count", 70, 40, 55, 17)
#EndRegion ### END Koda GUI section ###

$time = TimerInit()
$var = DriveGetDrive("FIXED")
If Not @error Then
    For $i = 1 To $var[0]
        SearchFile($var[$i] & "\", 0)

Func SearchFile($dir, $recur)
    Local $search, $file

    $search = FileFindFirstFile("*.*")
    If $search = -1 Then Return

    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $nbFiles += 1
        If TimerDiff($time) > 3000 Then
;~      Mod($nbFiles,1000)=0 Then GUICtrlSetData($Label1, $nbFiles)
            GUICtrlSetData($Label1, $nbFiles)
            $time = TimerInit()
        If $file = "nameoffile.extension" Then $result += 1
        If StringInStr(FileGetAttrib($dir & $file), "D") > 0 And $recur < 10 Then
            SearchFile($dir & $file & "\", $recur + 1)

EndFunc   ;==>SearchFile

MsgBox(0, "", $result)
Edited by trung0407

Share this post

Link to post
Share on other sites

Take a look at the Run() command in the help file. It shows examples of using @Comspec which allows you to run the command interpreter using whatever you want. You could either forward it to a file or use the StdIO stream to get the results. I would actually set the switches for the Dir command with EnvSet("DIRCMD", "/a/b/s") so it doesn't need to be included in the Run() function then unset it with EnvSet("DIRCMD", "") after.


EnvSet("DIRCMD", "/a/b/s")
RunWait(@ComSpec & " /c dir C:\*afd.sys > C:\Results.txt")
EnvSet("DIRCMD", "")
Edited by GEOSoft


Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver: - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post

Link to post
Share on other sites

Thanks for the replies.

Thanks a lot Melba23 for the help. It was a great help. It worked for me and there is a small detail to sort out and I will shift to that topic.;)

trung0407, Thanks, it looks pretty similar to the one I posted and I'm not sure it will work with wild cards. I will give it a try.

GEOSoft, The dir command is simple and works and I was aware of that and gave some examples. But as I said I didn't want to call cmd.exe for that purpose (didn't want to depend on cmd.exe) and wanted to use Autoit functions. Thanks anyway.

Edited by Factfinder

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...