Sign in to follow this  
Followers 0
monter

AnyFileExists = FileExists and *.*

4 posts in this topic

#1 ·  Posted (edited)

I noticed that

FileExists("path\filename.ext") and FileExists("path\*.ext") works properly if I want to check if given files exist, but if any file exist:

FileExists("path\*.*") it fails, because it works like FileExists("path") - checks existence of directory.

So I made a small func AnyFileExists:

$path = 'C:\Newly_created_123\directory_456'

DirCreate($path)
If @error Then
    MsgBox(16, Default, 'Error creating directory', 12)
    Exit
EndIf
OnAutoItExitRegister('OnExit')
MsgBox(64, Default, $path & @CRLF & "shouldn't contain any files.", 4)

Test_FileExists()
Test_FFFF()

Func Test_FileExists()
    If FileExists($path) Then MsgBox(64, 'Test_FileExists', $path & @CRLF & 'EXISTS.', 6) ; TRUE
    If FileExists($path & '\*.mp3') Then
        MsgBox(64, 'Test_FileExists', $path & '\*.mp3' & @CRLF & 'exists.', 6)
    Else
        MsgBox(64, 'Test_FileExists', $path & '\*.mp3' & @CRLF & "DOESN'T EXIST.", 6) ; TRUE
    EndIf
    If FileExists($path & '\*.*') Then MsgBox(48, 'Test_FileExists', $path & '\*.*' & @CRLF & 'exists (but directory is empty!).', 9) ; FALSE - IMO it should be TRUE!
EndFunc ;==>Test_FileExists


Func Test_FFFF()
    $checkFiles = AnyFileExists($path, '*.*')
    If $checkFiles = 0 Then
        MsgBox(64, 'Test_FFFF', $path & '\*.*' & @CRLF & "DOESN'T EXIST.", 9) ; TRUE
    Else
        MsgBox(64, 'Test_FFFF', $path & '\*.*' & @CRLF & 'exists.', 6)
    EndIf
EndFunc ;==>Test_FFFF

Func AnyFileExists($sPath, $sFilter)
    FileChangeDir($sPath)
    Local $ff1f = FileFindFirstFile($sFilter)
    FileChangeDir(@ScriptDir)
    FileClose($ff1f)
    If $ff1f = -1 Then
        Return 0
    Else
        Return 1
    EndIf
EndFunc ;==>AnyFileExists

Func OnExit()
    FileChangeDir('C:\')
    If FileExists(StringLeft($path, StringInStr($path, '\', 0, -1) -1)) Then DirRemove(StringLeft($path, StringInStr($path, '\', 0, -1) -1), 1)
EndFunc ;==>OnExit

AnyFileExists.au3

Edited by monter

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Func _FileExistsEx($s_path)
    Local $hFF = FileFindFirstFile($s_path)
    If $hFF = -1 Then Return SetError(1, 0, 0)
    FileClose($hFF)
    Return 1
EndFunc

Edit:

I just wanted to show you, in case you don't notice, you're missing a key element in your call ( FileClose() ).

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

you're missing a key element in your call ( FileClose() ).

Of course, I forgot it ;)

monter.FM [font="Tahoma;"]Full programs:[/font][font="'Microsoft Sans Serif';"] LogOnOff - keeps alive user session, after set time it performs logoff (instead of locking [acronym="Personal Computer"]PC[/acronym], useful in some corporations working with [acronym="Active Directory"]AD[/acronym]).[/font] ČharCönvěr - character set converter. [font="'Microsoft Sans Serif';"]CDTray - automated opening/closing the [acronym="Compact Disc"]CD[/acronym] tray.[/font] [font="'Microsoft Sans Serif';"]Example scripts: [/font][font="'Microsoft Sans Serif';"]RecentFolders - managing recently used folder list with combobox.[/font] [font="'Microsoft Sans Serif';"]AutoUpdater - periodic auto-checking, auto-updating and auto-relaunching newest script version.[/font] Changed host from monter.homeip.net to monter.homenet.org - replace address in my scripts to get back them to work.

Share this post


Link to post
Share on other sites

And another way of course, is to use DirGetSize("$path", 1) on the parent folder, which will return an array with the files & folders count.

Perhaps you could do a version of your function using that ... to cater for when a filepath (not folderpath is used)?

It should take less code and have the added benefit of reporting both file numbers and (especially) folder numbers of the passed path.

Which means you get to see if files &/or folders exist in the given path.

;)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

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