Discussions related to Visual Prolog
pauljouet
Posts: 4
Joined: 2 May 2021 8:06

Batch file generated with file::writeString does not properly execute

Unread post by pauljouet »

Hello,

I am having an issue with generated files from Visual Prolog...

My ultimate goal is to send http requests to fill an ElasticSearch database. The usual way would be to use cURL with a command such as

Code: Select all

curl -XGET "http://localhost:9200/someindex/_search" -H "Content-Type: application/json" -d"{ \"query\":{ \"match_all\":{}}}"
I have read about a VPcURL library, which would allow me to send requests from my prolog code, however I could not find any documentation. I then figured it would be easy to write a batch file containing the command, and then run it using the shellApi.

Code: Select all

Cmd = "curl -XPOST \"http://localhost:9200/someindex/_update/b-QvG3sBhb30C69Tqn7V\" -H 'Content-Type: application/json' -d 'my-update-script'",   file::writeString("path\\to\\test.bat", Cmd), shell_api::shellExecute(nullHandle, "open", "path\\to\\test.bat", "", "", 1)
The problem with this approach is that the .bat file generated with writeString does not launch. When I create the same file manually it works, however when it is created with writeString it will not open, nothing happens. I also tried with basic batch commmands such as "echo test" "pause", same problem. Even by manually editing the file and renaming it or launching as adminstrator. Once generated with writeString the file seems to be compromised.

What am I missing ?

Thanks for any help, even a link to a documentation of VPcURL would be really helpful, as the writeString method is already a workaround.

Paul
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: Batch file generated with file::writeString does not properly execute

Unread post by Thomas Linder Puls »

file::writeString will create a file with utf16 encoding, and that is not good in that context.

For a bat/cmd file I will suggest using file::writeStringUtf8 (because cmd does not like utf8 bom either).
Regards Thomas Linder Puls
PDC
User avatar
Thomas Linder Puls
VIP Member
Posts: 1245
Joined: 28 Feb 2000 0:01

Re: Batch file generated with file::writeString does not properly execute

Unread post by Thomas Linder Puls »

You could also consider calling curl directly using useExe:

Code: Select all

constants     cmd : string =         @[curl -XPOST "http://localhost:9200/someindex/_update/b-QvG3sBhb30C69Tqn7V" -H 'Content-Type: application/json' -d 'my-update-script'].   clauses     run() :-         E = useExe::new(cmd),         E:run().
The discussion forum does not token color the string literal correctly (it does not know the format @[...]), but the code is correct and the IDE will color it correctly.
Regards Thomas Linder Puls
PDC
pauljouet
Posts: 4
Joined: 2 May 2021 8:06

Re: Batch file generated with file::writeString does not properly execute

Unread post by pauljouet »

Thank you very much Thomas, it now works.
Post Reply