B4J Question Re-mount external Drive

le_toubib

Active Member
Licensed User
Longtime User
Hi all ,
How to -reliably- eject , and then remount an external hard drive , programmatically, while the drive is still connected to USB
Thanks
 

le_toubib

Active Member
Licensed User
Longtime User
I found 2 solutions from chatgpt, one through a Windows script , and the other is through b4x :
Eject:
import anywheresoftware.b4a.objects.IntentWrapper;
import anywheresoftware.b4a.objects.Service;
import java.io.IOException;

public Sub EjectExternalDrive
    Dim driveDetector As DriveDetector
    driveDetector.Initialize("driveDetector")
    If driveDetector.IsUSBDriveConnected Then
        Dim driveLetter As String
        driveLetter = driveDetector.GetUSBDriveLetter
        Try
            Dim p As String
            p = "cmd /c echo off && echo list disk | diskpart && echo select disk " & driveLetter & " && echo offline disk && echo. | diskpart"
            Dim su As ShellUtil
            su.Initialize("su", Me)
            su.ShellCommand(p, Null, Null)
            su.Run(5000)
        Catch
            Log("Error: " & LastException.Message)
        End Try
    Else
        Log("No external drive connected.")
    End If
End Sub

And here's the code for remount :
Remount:
import anywheresoftware.b4a.objects.IntentWrapper;
import anywheresoftware.b4a.objects.Service;

public Sub RemountExternalDrive
    ' Replace DriveLetter with the actual drive letter of the ejected external drive
    Dim driveLetter As String
    driveLetter = "D:" ' Replace with the correct drive letter
    Try
        Dim p As String
        p = "cmd /c echo off && echo select disk " & driveLetter & " && echo online disk && echo. | diskpart"
        Dim su As ShellUtil
        su.Initialize("su", Me)
        su.ShellCommand(p, Null, Null)
        su.Run(5000)
    Catch
        Log("Error: " & LastException.Message)
    End Try
End Sub

I couldn't find this driveDetector library though.

The other method is through a batch file , that worked fine , but that was outside b4x:
Batch file:
@echo off
setlocal enabledelayedexpansion

rem Check if three command line parameters are provided
if "%~1"=="" (
    echo Usage: %0 [eject|remount] [VolumeNumber] [DriveLetter]
    goto :eof
)

rem Check if the first parameter is "eject" or "remount"
if /i "%~1"=="eject" (
    rem Eject operation
    echo Ejecting volume %2...
    echo select volume %2 > ejectscript.txt
    echo remove all noerr >> ejectscript.txt
    diskpart /s ejectscript.txt
    del ejectscript.txt
    echo Volume %2 has been ejected.
) else if /i "%~1"=="remount" (
    rem Remount operation
    if "%~3"=="" (
        echo Missing drive letter. Please provide a drive letter for remounting.
    ) else (
        echo Remounting volume %2 with drive letter %3...
        echo select volume %2 > remountscript.txt
        echo assign letter=%3 noerr >> remountscript.txt
        diskpart /s remountscript.txt
        del remountscript.txt
        echo Volume %2 has been remounted with drive letter %3.
    )
) else (
    echo Invalid operation. Please use "eject" or "remount".
)

endlocal
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…