Jump to content

Best way to check if there is a certain type of file in a folder? Maybe if fileexists with a wild card?


Recommended Posts

Posted

Right now I am using the following code, but I feel like filefindfirstfile is a sloppy way to do this, in case the first file is not a zip file or an xml file, and neither is the second or third.

I could use if _filelisttoarray with a wild card I think but again that seems a little sloppy, but maybe there is a way to use if fileexists with a wild card?

if FileFindFirstFile(guictrlread($downloadbaklocation_box) & "\*.zip") = 1 Then
            RunWait(@WindowsDir & '\temp\7zip\7za.exe x ' & guictrlread($downloadbaklocation_box) & '\*.zip *.xml -oc:\temp\testxmls')
        Else
        $didnotfindzipfiles = 1
        MsgBox(0, "Error", "There are no .zip files in that folder")
        EndIf

if FileFindFirstFile(guictrlread($downloadbaklocation_box) & "\*.xml") = 1 Then
            _countdocsinnotif
        Else
        $didnotfindxmlfiles = 1
        MsgBox(0, "Error", "There are no .xml files in that folder")
        EndIf

Thanks for reading.

Posted (edited)

FileFindFirstFile() does not just check the first file in the directory. It returns a search handle when it finds the first MATCHING file. If it doesn't find one, it returns -1. So this is a test for *zip files:

$hSearch = FileFindFirstFile("C:\Temp\*.zip")
If $hSearch = -1 Then
     ConsoleWrite("No zip files." & @LF)
Else
     $sName = FileFindNextFile($hSearch)
     ConsoleWrite("First zip file: " & $sName & @LF)
EndIf
FileClose($hSearch)

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Posted

Did you even try doing it yourself first? It didn't take me many hours minutes seconds to write this two-liner to test if FileExist supports wild cards:

$iTest = FileExists("F:\*.mp3")
ConsoleWrite(@error & "/" & VarGetType($iTest) & "/" & $iTest & @CRLF)

(and it works!)

Posted

Yes, I tried this, I'll have to look at my code and figure out why it failed

Did you even try doing it yourself first? It didn't take me many hours minutes seconds to write this two-liner to test if FileExist supports wild cards:

$iTest = FileExists("F:\*.mp3")
ConsoleWrite(@error & "/" & VarGetType($iTest) & "/" & $iTest & @CRLF)

(and it works!)

Posted

Thanks Psalty

FileFindFirstFile() does not just check the first file in the directory. It returns a search handle when it finds the first MATCHING file. If it doesn't find one, it returns -1. So this is a test for *zip files:

$hSearch = FileFindFirstFile("C:\Temp\*.zip")
If $hSearch = -1 Then
     ConsoleWrite("No zip files." & @LF)
Else
     $sName = FileFindNextFile($hSearch)
     ConsoleWrite("First zip file: " & $sName & @LF)
EndIf
FileClose($hSearch)

:)

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...