B4J Question B4J Updeter

alirezahassan

Active Member
Licensed User
hi all,
i want to have Updater in my project. i wrote these codes but i have error.
First my app download zip file which has bin, conf, legal, lib And exe files. then my app extract this zip file to 'update' Folder. then Run These codes.




B4J code:
    File.WriteString(Codes.Get_FileDir,"Updater.bat",$"@ echo off       <-There is no space between @ and echo.
title Updater of Application
echo Please wait. Application is updating...
timeout /t 3
Rmdir /S /Q bin
Rmdir /S /Q conf
Rmdir /S /Q legal
Rmdir /S /Q lib
del /F /Q Panel.exe
xcopy /e update
Rmdir /S /Q update
start "" Panel.exe
del Updater.bat"$)
    Sleep(1000)
    fx.ShowExternalDocument(File.GetUri(Codes.Get_FileDir,"Updater.bat"))
    Sleep(10)
    ExitApplication

These codes first create a bat file. And open the file. Then close the application. It then deletes the previous application. And then moves the new application And opens.
but i have error

Did not delete any files. And did not convey anything.

The bat file is correct.

Sometimes it works fine when I open the bat file myself.
and show me

what should i do?
 

Attachments

  • 2021-08-31_00-36-25.png
    6.8 KB · Views: 213
Last edited:

roerGarcia

Active Member
Licensed User
Longtime User
does the script run in the correct folder?
It is a research question.
try to secure the script directory before running
Or the synchronization between deleting and copying the new package fails
 
Upvote 0

alirezahassan

Active Member
Licensed User
You should use jShell and run cmd.exe /c to start the batch file.
Thanks for the reply
Are you sure this code works before the program exits?
I checked the library but did not notice.
Can you help me?
@Erel
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Thanks for the reply
Are you sure this code works before the program exits?
I checked the library but did not notice.
Can you help me?
@Erel

Hi @alirezahassan. Did you make it work with the code? Does it require elevated rights like being administrator?
 
Upvote 0

tchart

Well-Known Member
Licensed User
Longtime User
One thing to remember is that if you "Run As Administrator" that the working directory will be C:\Windows\System32 (even for batch files)



So if that is the case you need to fully qualify your paths.

Eg this batch file uninstalls a windows service. Even though ServiceName.exe is in the same diretcory as the batch file you need to grab the path of the batch file (in homedir variable) and use that to fuilly qualify the path.

B4X:
@echo off
setlocal

net stop "Service Name"

SET homedir=%~dp0
IF %homedir:~-1%==\ SET homedir=%homedir:~0,-1%

%homedir%\ServiceName.exe uninstall
 
Upvote 0

alirezahassan

Active Member
Licensed User
hi @hatzisn and @tchart
i changed my code like these.

B4X:
@echo off
title Updater of Application
setlocal
net stop "Service Name"
SET homedir=%~dp0
IF %homedir:~-1%==\ SET homedir=%homedir:~0,-1%
%homedir%\ServiceName.exe uninstall
echo Please wait. Application is updating...
timeout /t 3
Rmdir /S /Q bin
Rmdir /S /Q conf
Rmdir /S /Q legal
Rmdir /S /Q lib
del /F /Q Panel.exe
xcopy /e update
Rmdir /S /Q update
start "" Panel.exe
del Updater.bat

But it gives an error when run with my application. (It does not give an error when I open the batch file myself.)

 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Change the line "net..." to "runas /noprofile /user:Administrator net..."

Change the Rmdir lines when creating in B4J to (f.e.) :

"Rmdir /S /Q " & File.Combine(File.DirApp, "..\bin")

and "Del " & File.Combine(File.DirApp, "..\Panel.exe")

Change the xcopy line in the .bat when you are creating the bat file to:

"xcopy /e " & File.Combine(File.DirData , "update") & " " & File.Combine(File.DirApp, "..")

Save the ".bat" file in File.DirData and save there also the zip which you will unzip.



I believe also you will have to do the following in code :

B4X:
    Dim js As Shell
    Dim sRet As String
    Dim jres As ShellSyncResult
    js.Initialize("js", "runas", Array As String ("/noprofile", "/user:Administrator", "cmd", "/c", File.Combine(File.DirData, "Update/Updater.bat")))
    jres = js.RunSynchronous(5000)  '<- maybe you will have to change this to a greater value for waiting
    sRet = jres.StdErr
 
Last edited:
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I have changed several things in my previous message so if @alirezahassan you have already seen it before I post this message you'd better see it again.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…