B4J Question Network resource path instead of drive letter

Didier9

Well-Known Member
Licensed User
Longtime User
I am trying to get the actual network path to a file that the user selects through the file chooser and the file chooser returns the drive as a letter instead of as a network resource (for instance "Y:\" instead of "\\Machine\Folder")
Of course, network drives are mapped to different letters on different computers (and some don't necessarily have the drive mapped at all) so if I create a batch file to do some operation on these drives, the probability that it will work more than once is pretty low.
Is there a convenient way to get one from the other?
TIA
 

DonManfred

Expert
Licensed User
Longtime User
Why you are not using the networkpath directly? As far as i think it should work with B4J.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
The user is expected to navigate to a certain file using the file chooser.
The returned path depends on the configuration of the machine used. If the file is on a network drive that is mapped to a drive letter, the File Chooser returns the drive letter, but on another machine, that same network drive may not be mapped to the same drive letter, or may not be mapped to any letter and the file chooser will return the network location (which would be fine).
I want to create a batch file that could be used on any machine, so I can't have the drive letter.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
I want to create a batch file that could be used on any machine, so I can't have the drive letter.

A Windows (default) directory can be shared through a Share. This Share contains the drive letter of the relevant Share. For example, if the directory "E:\Data\TempDir" have the Share name "TempDir" you can get the drive path with internal jShell library:

FileChoocer:
'    --- This program reuires:
'    --- A file share with the name TempDir
'    --- Libraries: jShell (internal)

#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region

'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=Project.zip

Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private Page As Form
    Private fc As FileChooser
    Private DirPath As String
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")   
    fc.Initialize
    FindShare
    fc.InitialDirectory = DirPath
    fc.SetExtensionFilter("Layouts",Array("*.*"))
    fc.Title = "Open Share"
End Sub

Private Sub Button1_Click
    FindShare
    Dim f As String = fc.ShowOpen(Page)
'    --- process result   
    If f <> Null Then Log(f)
End Sub

Sub FindShare As ResumableSub
    Dim shl As Shell
    shl.Initialize("shl", "wmic", Array("share", "TempDir", "get", "Path"))
    shl.Run(-1)
    Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success Then
        Dim x As Int = 0
        For Each line As String In Regex.Split("[\r\n]+", StdOut)
            If x = 1 Then    '--- We need second result value
                Dim fc As FileChooser
                fc.Initialize
                fc.InitialDirectory = line.Trim
                DirPath = line.Trim
                Return line.Trim
            End If
            x = x+1
        Next
    End If
    Return ""
End Sub
 
Upvote 0
Top