B4J Question ¿ [ABMaterial]? Real name file

amminf

Active Member
Licensed User
Longtime User
Hi,

In Windows, in cDir, there is a file with name "Something.PDF" (note .PDF)


cFile = "Something.pdf"
lFound = file.Exists(cDir, cFile) ' True

In ABMaterial, in ABMPdfViewer do not open cFile because real name in disk is "Something.PDF".

How to know real name in disk ?

My workaround is poor, because is can be slow.


B4X:
                ' Return Real name file in disk
                Dim cFileTmp As String
                Dim listFiles As List = File.ListFiles(cDirFull)
                Dim nI As Int
                Dim cTmp1 As String
                Dim cTmp2 As String
              
              
                cFileTmp = cFile
                cFileTmp = cFileTmp.ToUpperCase

                For nI= 0 To listFiles.Size- 1
                    cTmp1 = listFiles.Get(nI)
                    cTmp2 = cTmp1
                    cTmp2 = cTmp2.ToUpperCase
                  
                    If cTmp2 == cFileTmp Then
                        ' Encontrado: coge el nombre del file en disco y sale.
                        cFile = cTmp1
                        'Log("encontrado !! " & cFile & "///" & cTmp1 & "///" & cTmp2 & "///"& cFileTmp)
                        Exit
                    End If
                  
                Next
               'Log("aqui viendo " & lExist & "///" & File.GetName(cFileFullFull) & " " & File.Size(cDirFull, cFile) & "///"& cFile  )
 

mindful

Active Member
Licensed User
1. You need to add JavaObject library from the Libraries Manager Tab in the IDE
2. Add the following Sub in your class or code module:
B4X:
Sub GetCanonicalFilePath(fullpath As String) As String
    Dim joFileSource As JavaObject
    joFileSource.InitializeNewInstance("java.io.File", Array As Object(fullpath))
    Return joFileSource.RunMethod("getCanonicalFile", Null)
End Sub
3. If you need the full path you call:
B4X:
GetCanonicalFilePath(cDir & cFile)
4. If you need only the real file name:
B4X:
File.GetName(GetCanonicalFile(cDir & cFile))


Hope this helps !
 
Upvote 0
Top