B4J Question Get MS-DOS Path

wonder

Expert
Licensed User
Longtime User
Is there a way to turn "C:\Program Files" into "C:\Progra~1", without resorting to jShell?
 

eps

Expert
Licensed User
Longtime User
How do you mean? What are you attempting to solve?

I was going to suggest that you can just modify the path if it's over 8 characters in length, so that it is Progra~1 but there could be two directories similarly named and therefore that wouldn't account for that. One would be Progra~1 and one Progra~2...

You could then use File.Exists to check that it exists, but of course both may exist but not actually be the one you want.

How are you using jShell to do this? If you can get the output from dir/x then you should be able to use that.. You might have to resort to pumping it in to a file and then sifting through that.

I'm sure someone will come along with a more elegant or sophisticated solution!!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Download jna-4.5.0.jar and jna-platform-4.5.0.jar: https://github.com/java-native-access/jna#download
Copy them to the additional libraries folder.

2.
B4X:
#AdditionalJar: jna-4.5.0
#AdditionalJar: jna-platform-4.5.0
  
'usage:
Log(GetShortPathName("C:\Users\H\Downloads\xCustomListView.zip"))

Sub GetShortPathName(Path As String) As String
   Dim jo As JavaObject
   jo = jo.InitializeStatic("com.sun.jna.platform.win32.Kernel32").GetField("INSTANCE")
   Dim buffer(256) As Char
   Dim res As Int = jo.RunMethod("GetShortPathName", Array(Path, buffer, buffer.Length))
   If res = 0 Then
     Return Path 'failed
   End If
   Dim sb As StringBuilder
   sb.Initialize
   For i = 0 To res - 1
     sb.Append(buffer(i))
   Next
   Return sb.ToString
End Sub
 
Upvote 0
Top