Dim d As String = ProjectDir & "\"
Dim MyFile as string = "test.txt"
MyFile = ProjectDir & "\" & MyFile
Dim script As String = $"
Get-Item -Path ${MyFile} | fl * > ${d}details.txt
"$
Wait For (PowerShellScript(script)) Complete (Result As ShellSyncResult)
If Result.ExitCode = 0 Then
Dim lst As List = File.ReadList( ProjectDir, "details.txt" )
Log( lst )
End If
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private Directory As String="E:"
Private FileName As String="test.txt"
'Get-Item -Path e:\test.txt | f1 *
Private Script As String=$"Get-ItemProperty -Path ${Directory}\${FileName} | format-list"$
Private res As ShellSyncResult
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
PowerShellScript(Script)
End Sub
Sub Button1_Click
MainForm.Close
End Sub
Public Sub PowerShellScript(s As String) As ResumableSub
's = s.Replace(CRLF, ";").Replace("""", "'")
Dim shl As Shell
shl.InitializeDoNotHandleQuotes("shl", "powershell.exe", Array("-Command", s))
shl.Run(-1)
Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
res.ExitCode = ExitCode
res.StdErr = StdErr
res.StdOut = StdOut
res.Success = Success
If StdErr <> "" Then
Log(StdErr)
If ExitCode = 0 Then res.ExitCode = 1
End If
Dim InfoArray() As String=Regex.Split(CRLF, StdOut)
Log(InfoArray(8))
Return res
End Sub
Log( s2 )
Dim sout As String
For n=0 To s2.Length-1
sout = sout & " " & Asc( s2.SubString( n ))
Next
Log( sout )
If s2.IndexOf( "LastWriteTime" ) > -1 Then
writeTime = s2.SubString( s2.IndexOf( ":" ))
Exit
End If
The strings returned by the shell are encoded with 16 bit chars, and for some reason IndexOf() or Contain() do not work
Public Sub PowerShellScript(s As String) As ResumableSub
's = s.Replace(CRLF, ";").Replace("""", "'")
Dim shl As Shell
shl.InitializeDoNotHandleQuotes("shl", "powershell.exe", Array("-Command", s))
shl.Run(-1)
Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
res.ExitCode = ExitCode
res.StdErr = StdErr
res.StdOut = StdOut
res.Success = Success
If StdErr <> "" Then
Log(StdErr)
If ExitCode = 0 Then res.ExitCode = 1
End If
InfoArray=Regex.Split(CRLF, StdOut)
Log($"Array size: ${InfoArray.Length}"$)
Log(InfoArray(8))
Log(" ")
Log($"Position of year: ${InfoArray(8).IndexOf(2021)}"$) '<-------- here i am using indexof!!!
Return res
End Sub
Public Sub PowerShellScript(s As String) As ResumableSub
's = s.Replace(CRLF, ";").Replace("""", "'")
Dim shl As Shell
shl.InitializeDoNotHandleQuotes("shl", "powershell.exe", Array("-Command", s))
shl.Run(-1)
Wait For shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
res.ExitCode = ExitCode
res.StdErr = StdErr
res.StdOut = StdOut
res.Success = Success
If StdErr <> "" Then
Log(StdErr)
If ExitCode = 0 Then res.ExitCode = 1
End If
InfoArray=Regex.Split(CRLF, StdOut)
Log($"Array size: ${InfoArray.Length}"$)
Log(" ")
Log(StdOut)
Log(" ")
Log($"Position of year: ${InfoArray(9).IndexOf( 2021 )}"$)
writetime=InfoArray(9).SubString2(InfoArray(9).IndexOf(2021)-6,InfoArray(9).IndexOf(2021)+4)
Log($"Last Write Time: ${writetime}"$)
Return res
End Sub
Waiting for debugger to connect...
Program started.
lastAccessTime 2020-05-19T09:58:55.583569Z
lastModifiedTime 2007-11-07T08:00:40Z
size 17734
creationTime 2007-11-07T08:00:40Z
isSymbolicLink false
isRegularFile true
fileKey null
isOther false
isDirectory false
***
Creation Time : 2007-11-07T08:00:40Z
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
Dim JO As JavaObject = Me
dim FilePath as string ="C:\......."
JO.RunMethod("FileDetails",Array(filePath))
End Sub
#if java
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
public static void FileDetails(String filePath) {
File file = new File(filePath); //with \ escape pattern for backslashes
System.out.println("Name: " + file.getName());
System.out.println("Absolute path: " + file.getAbsolutePath());
System.out.println("Size: " + file.length());
System.out.println("Last modified: " + new Date(file.lastModified()));
}
#End If
You should learn javaobject you could save a lot of work. It is easier than it seemsB4X:Sub AppStart (Form1 As Form, Args() As String) MainForm = Form1 MainForm.RootPane.LoadLayout("Layout1") MainForm.Show Dim JO As JavaObject = Me dim FilePath as string ="C:\......." JO.RunMethod("FileDetails",Array(filePath)) End Sub #if java import java.io.File; import java.io.IOException; import java.text.ParseException; import java.util.Date; public static void FileDetails(String filePath) { File file = new File(filePath); //with \ escape pattern for backslashes System.out.println("Name: " + file.getName()); System.out.println("Absolute path: " + file.getAbsolutePath()); System.out.println("Size: " + file.length()); System.out.println("Last modified: " + new Date(file.lastModified())); } #End If
Anything wrong with the code, I want to learn from you please.You should learn javaobject you could save a lot of work. It is easier than it seems
This is the output:You should learn javaobject you could save a lot of work. It is easier than it seems
Just updated to make it even simpler. It was something I used in a hurry a few years ago and hadn't tidied it up .it works simply and perfectly.