Android Question Issue with SMB.ListFiles

MrKim

Well-Known Member
Licensed User
Longtime User
When using ListFiles
SMB1_ListCompleted (Url As String, Success As Boolean, Entries() As SMBFile)
returns Success = False if there are no entries for the filespec passed.

This does not seem right to me. I thought that Success returned true if SMB was able to successfully execute the command. Whether there are any results should be a separate issue.

All I want to do is see if a file exists.

I am checking a directory that may have thousands of files and I don't want to loop through all of them to look for one, I am fine with no entries, but I also want to know that the command executed successfully.

Am I missing something?

Thanks
 

MrKim

Well-Known Member
Licensed User
Longtime User
What is the value of Log(LastException) when there are no matches?
The value of Success and LastException. This is what I get when the file doesn't exist.
B4X:
false
(SmbException) jcifs.smb.SmbException: The system cannot find the file specified.
This is what I get when the file DOES exist. I ran this immediately after running the above because what the routine does is copy the file if it doesn't exist.
B4X:
true
(SmbException) jcifs.smb.SmbException: The system cannot find the file specified.
If I restart the program and do it a gain when the file exists I get:
B4X:
true
(Exception) Not initialized

FYI I hacked the problem by parsing out the file name and subdirectory and running Listfiles with the Sub-directory as the filepattern (for my application there will ALWAYS be a sub-directory) that way I can 'test' the connection without returning thousands of files.
If that returns true then I have a reasonable expectation that running listfiles on the file itself is actually 'working' as it should and false means the file does not exist (I'm a long time windows/VBA programmer - I'm used to writing hacks to work around bugs:D:eek:).
Here is how I solved the problem (FInfo.WD is our destination directory):
B4X:
        SMB1.ListFiles(FInfo.WD.SubString2(0, FInfo.WD.LastIndexOf2("/", FInfo.WD.Length - 2) + 1), FInfo.WD.SubString2(FInfo.WD.LastIndexOf2("/", FInfo.WD.Length - 2) + 1, FInfo.WD.Length - 1))
        Wait For SMB1_ListCompleted (Url As String, Success As Boolean, Entries() As SMBFile)
        If Not(Success) Then
            FInfo.LD = "Unable to find our network directory " & FInfo.WD.SubString(4) & ". This may be due to an invalid directory, bad username/password, or inadequate rights."
            Return
        Else
            SMB1.ListFiles(FInfo.WD, FInfo.WF)
            Wait For SMB1_ListCompleted (Url As String, Success As Boolean, Entries() As SMBFile)
            Log(Success)
            Log(LastException)
            Log (Entries.Length)
            If Success Then
                If Entries.Length > 0 Then
                    FInfo.LD = "This file already exists! You are not allowed to overwrite existing files."
                Return
                End If         
            End If
        End If
Thanks for your time Erel.
 
Last edited:
Upvote 0
Top