Jump to content

Please hlep me convert VB 6 to Autoit


Recommended Posts

You create a 1 form with 1 Command button (command1), 1 listbox (list1) and 4 textboxe (text1, text2, text3 và text4)

Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long

Const MAX_PATH = 260
Const MAXDWORD = &HFFFF
Const INVALID_HANDLE_VALUE = -1
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_TEMPORARY = &H100

Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type

Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Function StripNulls(OriginalStr As String) As String
    If (InStr(OriginalStr, Chr(0)) > 0) Then
        OriginalStr = Left(OriginalStr, InStr(OriginalStr, Chr(0)) - 1)
    End If
    StripNulls = OriginalStr
End Function

Function FindFilesAPI(path As String, SearchStr As String, FileCount As Integer, DirCount As Integer)
    'KPD-Team 1999
    'E-Mail: KPDTeam@Allapi.net
    'URL: http://www.allapi.net/

    Dim FileName As String' Walking filename variable...
    Dim DirName As String' SubDirectory Name
    Dim dirNames() As String' Buffer For directory Name entries
    Dim nDir As Integer' Number of directories In this path
    Dim i As Integer' For-loop counter...
    Dim hSearch As Long' Search Handle
    Dim WFD As WIN32_FIND_DATA
    Dim Cont As Integer
    If Right(path, 1) <> "\" Then path = path & "\"
    ' Search For subdirectories.
    nDir = 0
    ReDim dirNames(nDir)
    Cont = True
    hSearch = FindFirstFile(path & "*", WFD)
    If hSearch <> INVALID_HANDLE_VALUE Then
        Do While Cont
        DirName = StripNulls(WFD.cFileName)
        ' Ignore the current And encompassing directories.
        If (DirName <> ".") And (DirName <> "..") Then
            ' Check For directory With bitwise comparison.
            If GetFileAttributes(path & DirName) And FILE_ATTRIBUTE_DIRECTORY Then
                dirNames(nDir) = DirName
                DirCount = DirCount + 1
                nDir = nDir + 1
                ReDim Preserve dirNames(nDir)
            End If
        End If
        Cont = FindNextFile(hSearch, WFD)'Get Next subdirectory.
        Loop
        Cont = FindClose(hSearch)
    End If
    ' Walk through this directory And sum file sizes.
    hSearch = FindFirstFile(path & SearchStr, WFD)
    Cont = True
    If hSearch <> INVALID_HANDLE_VALUE Then
        While Cont
            FileName = StripNulls(WFD.cFileName)
            If (FileName <> ".") And (FileName <> "..") Then
                FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * MAXDWORD) + WFD.nFileSizeLow
                FileCount = FileCount + 1
                List1.AddItem path & FileName
            End If
            Cont = FindNextFile(hSearch, WFD)' Get Next file
        Wend
        Cont = FindClose(hSearch)
    End If
    ' If there are sub-directories...
    If nDir > 0 Then
        ' Recursively walk into them...
        For i = 0 To nDir - 1
            FindFilesAPI = FindFilesAPI + FindFilesAPI(path & dirNames(i) & "\", SearchStr, FileCount, DirCount)
        Next i
    End If
End Function
Sub Command1_Click()
    Dim SearchPath As String, FindStr As String
    Dim FileSize As Long
    Dim NumFiles As Integer, NumDirs As Integer
    Screen.MousePointer = vbHourglass
    List1.Clear
    SearchPath = Text1.Text
    FindStr = Text2.Text
    FileSize = FindFilesAPI(SearchPath, FindStr, NumFiles, NumDirs)
    Text3.Text = NumFiles & " Files found In " & NumDirs + 1 & " Directories"
    Text4.Text = "Size of files found under " & SearchPath & " = " & Format(FileSize, "#,###,###,##0") & " Bytes"
    Screen.MousePointer = vbDefault
End Sub
'Just Add a listbox (List1)

Private Sub ListFiles(strPath As String, Optional Extention As String)
'Leave Extention blank For all files
    Dim File As String

    If Right$(strPath, 1) <> "\" Then strPath = strPath & "\"

    If Trim$(Extention) = " Then
        Extention = "*.*"
    ElseIf Left$(Extention, 2) <> "*." Then
        Extention = "*." & Extention
    End If

    File = Dir$(strPath & Extention)
    Do While Len(File)
        List1.AddItem File
        File = Dir$
    Loop
End Sub

Private Sub Form_Load()
    Text1.Text = "C:\"
    Text2.Text = "*.txt"
    ListFiles "C:\", "txt"
End Sub

Thank

Link to comment
Share on other sites

Hi,

Maybe something like this:

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

Func _FileListToArrayEx($sPath, $sMask='*')
    Local $i, $j, $blist, $rlist[1]=[0], $dlist = _DirListToArray ($sPath)
    _ArrayAdd ($dlist, $sPath)
    For $i=1 To $dlist [0] +1
        $blist = _FileListToArray ($dlist [$i], $sMask, 1)
        If Not @error Then
            For $j=1 To $blist [0]
                _ArrayAdd ($rlist, $dlist[$i] & "\" & $blist [$j])
            Next
        EndIf
    Next
    $rlist [0] = UBound ($rlist) - 1
    Return $rlist
EndFunc

Func _DirListToArray($sPath)
    Local $i, $j, $rlist[1]=[0], $blist, $alist=_FileListToArray ($sPath, '*', 2)
    If IsArray ($alist) Then
        For $i=1 To $alist [0]
            _ArrayAdd ($rlist, $sPath & "\" & $alist [$i])
            $blist = _DirListToArray ($sPath & "\" & $alist [$i])
            If $blist[0]>0 Then
                For $j=1 To $blist [0]
                    _ArrayAdd ($rlist, $blist [$j])
                Next
            EndIf
        Next
    EndIf
    $rlist [0] = UBound ($rlist) - 1
    Return $rlist
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Here is the same in one function:

#include <File.au3>
;#include <Array.au3> Not needed anymore

Func _FileListToArrayEx($sPath, $sMask='*')
    Local $i, $j, $RetList[1], $DirsArr = _FileListToArray($sPath, '*', 2), $SubDirFiles, $FilesArr
    
    $FilesArr = _FileListToArray($sPath, $sMask, 1)
    For $i = 1 To $FilesArr[0]
        ReDim $RetList[UBound($RetList)+1]
        $RetList[UBound($RetList)-1] = $sPath & "\" & $FilesArr[$i]
    Next
    
    If $DirsArr[0] > 0 Then
        For $i = 1 To $DirsArr[0]
            $SubDirFiles = _FileListToArrayEx($sPath & "\" & $DirsArr[$i], $sMask)
            If $SubDirFiles[0] > 0 Then
                For $j = 1 To $SubDirFiles[0]
                    ReDim $RetList[UBound($RetList)+1]
                    $RetList[UBound($RetList)-1] = $SubDirFiles[$j]
                Next
            Else
                $FilesArr = _FileListToArray($sPath & "\" & $DirsArr[$i], $sMask, 1)
                For $j = 1 To $FilesArr[0]
                    ReDim $RetList[UBound($RetList)+1]
                    $RetList[UBound($RetList)-1] = $sPath & "\" & $DirsArr[$i] & "\" & $FilesArr[$j]
                Next
            EndIf
        Next
    EndIf
    $RetList[0] = UBound($RetList) - 1
    Return $RetList
EndFunc

:)

Edited by MsCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Have some error, I have Fix, Thank You! :)

#include <File.au3>
#include <Array.au3>
$FileList = _FileListToArrayEx (@UserProfileDir, "*.*")
_ArrayDisplay($FileList,"$FileList")
Func _FileListToArrayEx($sPath, $sMask='*')
    Local $i, $j, $RetList[1], $DirsArr = _FileListToArray($sPath, '*', 2), $SubDirFiles, $FilesArr
   
    $FilesArr = _FileListToArray($sPath, $sMask, 1)
    For $i = 1 To UBound($FilesArr) - 1
        ReDim $RetList[UBound($RetList)+1]
        $RetList[UBound($RetList)-1] = $sPath & "\" & $FilesArr[$i]
    Next
   
    If UBound($DirsArr) > 0 Then
        For $i = 1 To $DirsArr[0]
            $SubDirFiles = _FileListToArrayEx($sPath & "\" & $DirsArr[$i], $sMask)
            If $SubDirFiles[0] > 0 Then
                For $j = 1 To $SubDirFiles[0]
                    ReDim $RetList[UBound($RetList)+1]
                    $RetList[UBound($RetList)-1] = $SubDirFiles[$j]
                Next
            Else
                $FilesArr = _FileListToArray($sPath & "\" & $DirsArr[$i], $sMask, 1)
                For $j = 1 To UBound($FilesArr) - 1
                    ReDim $RetList[UBound($RetList)+1]
                    $RetList[UBound($RetList)-1] = $sPath & "\" & $DirsArr[$i] & "\" & $FilesArr[$j]
                Next
            EndIf
        Next
    EndIf
    $RetList[0] = UBound($RetList) - 1
    Return $RetList
EndFunc
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...