Sign in to follow this  
Followers 0
luis713

find hidden files and check a checkbox

5 posts in this topic

hello, i don't know what is wrong with my code, i'm trying to find hidden files in a directory and if exist at least one hidden file check a checkbox, here's my code

#Include <GUIConstantsEx.au3>
#include <GuiComboBox.au3>


Local $msg
GUICreate("My GUI") ; will create a dialog box that when displayed is centered
$Combo = GUICtrlCreateCombo("c:\test", 20, 20, 200, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "c:\test2|c:\test3", "c:\test3")
$ShowHiddenFiles = GUICtrlCreateCheckbox("Show hidden files", 10, 50, 100, 20)

GUISetState(@SW_SHOW) ; will display an empty dialog box
; Run the GUI until the dialog is closed

While 1
Global $Path = GUICtrlRead($Combo)
DetectHiddenFiles()
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()



Func DetectHiddenFiles()
If GUICtrlRead($ShowHiddenFiles) = 4 Then ;if the checkbox is unchecked
$path = GUICtrlRead($Combo) & "\*."
$handle = FileFindFirstFile($Path)
While 1
$file = FileFindNextFile($handle)
If @error Then ExitLoop
$attrib = FileGetAttrib($Path & "\" & $file)
If StringInStr($attrib, "H") = 1 Then
If GUICtrlRead($ShowHiddenFiles) = 4 Then ;if is disabled
GUICtrlSetState($ShowHiddenFiles, $GUI_CHECKED)
ExitLoop
EndIf
EndIf
WEnd
FileClose($handle)
EndIf
EndFunc ;==>FindHiddenDirs

Share this post


Link to post
Share on other sites



#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#include <FileOperations.au3>

GUICreate("My GUI") ; will create a dialog box that when displayed is centered
$iCombo = GUICtrlCreateCombo("", 20, 20, 200, 20, $CBS_DROPDOWNLIST)
GUICtrlSetData(-1, "c:test2|c:test|D:test1", "D:test1")
$ShowHiddenFiles = GUICtrlCreateCheckbox("Show hidden files", 10, 50, 100, 20)

GUISetState(@SW_SHOW) ; will display an empty dialog box
; Run the GUI until the dialog is closed

While 1
    Switch GUIGetMsg()
        Case $iCombo
            $sPath = GUICtrlRead($iCombo)
            $Trg = _DetectHiddenFiles($sPath)
            If @error Then
                MsgBox(0, 'error', @error)
            Else
                If $Trg Then
                    GUICtrlSetState($ShowHiddenFiles, $GUI_CHECKED)
                Else
                    GUICtrlSetState($ShowHiddenFiles, $GUI_UNCHECKED)
                EndIf
            EndIf
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _DetectHiddenFiles($sPath)
    $aFileList = _FO_FileSearch($sPath)
    If Not @error Then
        For $i = 1 To $aFileList[0]
            If StringInStr(FileGetAttrib($aFileList[$i]), "H") Then Return SetError(0, 0, 1)
        Next
        Return SetError(0, 0, 0)
    Else
        SetError(@error, 0, 0)
    EndIf
EndFunc   ;==>_DetectHiddenFiles

Share this post


Link to post
Share on other sites

Following issues stand out that you may want to look at (rather than just copying AZJIOs code) as they are very common mistakes/problems:

  • You are detecting the hidden files on every loop iteration. That will make your GUI a lot slower and more unresponsive if $Path happens to include a lot of files. The solution is to either use events to know when the controls value has changed, or to manually check for when it changes. (Fixed by AZJIOs code above, by checking $iCombo in the message loop).
  • You often check the checkbox state by using the equals operator. This is very bad practice as states do not necessarily have to have only one flag set. BitAND is a much better way to do this, so change "GUICtrlRead($ShowHiddenFiles) = 4" to "BitAND(GUICtrlRead($ShowHiddenFiles), 4)"

Share this post


Link to post
Share on other sites

Following issues stand out that you may want to look at (rather than just copying AZJIOs code) as they are very common mistakes/problems:

  • You are detecting the hidden files on every loop iteration. That will make your GUI a lot slower and more unresponsive if $Path happens to include a lot of files. The solution is to either use events to know when the controls value has changed, or to manually check for when it changes. (Fixed by AZJIOs code above, by checking $iCombo in the message loop).
  • You often check the checkbox state by using the equals operator. This is very bad practice as states do not necessarily have to have only one flag set. BitAND is a much better way to do this, so change "GUICtrlRead($ShowHiddenFiles) = 4" to "BitAND(GUICtrlRead($ShowHiddenFiles), 4)"
The script works fine, just i've a doubt about bitand, i don't know why is better? i'm asking because i want to learn about autoit

Share this post


Link to post
Share on other sites

The script works fine, just i've a doubt about bitand, i don't know why is better? i'm asking because i want to learn about autoit

States can be combined. A checkbox can be checked or un-checked, but at the same time it can be enabled/disabled, shown/hidden, focused/not and a few more (see here).

No idea how much you understand about binary and bitwise operators, so I won't attempt to explain them here.

The solution to this problem is to assign each state to a bit, and then toggle the bit. You can get/set each bit in a number individually which makes it very useful. Same is done for styles, and any other set of flags that can be combined.

What if you wanted an unchecked, but disabled, checkbox? The state would be 132 (128 OR 4).

When you then see if its checked using "state = 4" it returns false, giving the wrong result. "BitAND(state, 4)" return 4 which will evaluate to true, giving the correct result.

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