Sign in to follow this  
Followers 0
njscorpion

how do i distinguish between file and folder?

4 posts in this topic

hi guys,

i am trying to make a autoit script which will search inside a directory and distinguish between files and folders, so that i can run different operations on my files and folders separately.

so i started with the following

$RootDirectory = @WorkingDir&"\Rar_FolderToBeProcessed"
FileChangeDir($RootDirectory)

$searchFiles = FileFindFirstFile("*.???")
If $searchFiles = -1 Then
    MsgBox(16, "Error :", "No files/directories matched the search pattern")
    Exit
EndIf
#EndRegion

While 1
    $File = FileFindNextFile($searchFiles)
    if $File=="" then ExitLoop
    MsgBox(0,"Found Files:",$File)
WEnd
FileClose($searchFiles)

which mainly copied from Autoit Help File, only thing changed is "FileFindFirstFile("*.???")"

but the above is returning files as well as folders where as if i use ".???" it sets $searchFiles to "-1" and troughs the error.

So, what is the best way i can process file and folders separately?

Just to illustrate my situation lets say i have the following:

\RootFolder

|--Folder1\SomeFile1.txt

|--Folder1\SomeFile2.txt

|--Folder2\SomeFile1.txt

|--Folder2\SomeFile2.txt

|--FileWithoutFolder1.txt

|--FileWithoutFolder2.txt

inside the root folder i will consider Folder1 and 2 as single entities and run some operation where as all the FileWithoutFolder i am considering as another entity.

Sorry! if was unable to explain properly :D plz let me know! i will try again...

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Check out FileGetAttrib to see the different file attributes.

While 1
    $fname = FileFindNextFile($search)
    If @error == 1 Then
        ExitLoop
    EndIf
    $attrib = FileGetAttrib($fname)
    If StringInStr($attrib, "D") <> 0 Then
        ;File is a folder
    Else
        ;File is a file
    EndIf
WEnd
Edited by omikron48

Share this post


Link to post
Share on other sites

omicron48 is on the right track

$RootDirectory = @WorkingDir&"\Rar_FolderToBeProcessed"
FileChangeDir($RootDirectory)

$searchFiles = FileFindFirstFile($RootDirectory & "\*");; Windows will actualy ignore "*.*"
If $searchFiles = -1 Then
    MsgBox(16, "Error :", "No files/directories matched the search pattern")
    Exit
EndIf
#EndRegion
$iCount = 0
$iFcount = 0
While 1
    $File = FileFindNextFile($searchFiles)
    if @Error then ExitLoop
    If StringInStr(FileGetAttrib($RootDirectory & "\" & $File), "D") Then
        MsgBox(0, "Result", $File & " is a folder.", 2)
        $iFcount += 1
    Else
        MsgBox(0, "Result", $File & " is a file.", 2)
        $iCount += 1
    EndIf
WEnd
FileClose($searchFiles)
MsgBox(0, "Count","Found " & $iCount & " files" & @CRLF & "and " & $iFcount & " folders)

George

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:3.0.1.13) - 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 omicron48 and GEOSoft for your help :D

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