Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Sally W. on July 10, 2015, 12:27:31 pm



Title: Using frontCall to check if a file exists
Post by: Sally W. on July 10, 2015, 12:27:31 pm
I know how to check if a file exists with the os.Path methods, but cannot get it to work via frontCall - the file in question is in a URL, potentially on a completely different machine.

If I use CMD this works:

if exist URL (echo 1) else (echo 0)

However using in frontCall it always succeeds - I've tried removing everything but 'if exist URL' and still get the same result.  I'm sure I'm doing something simple wrong, hopefully someone here can help me out...


Title: Re: Using frontCall to check if a file exists
Post by: Stefan S. on July 10, 2015, 01:16:51 pm
Hello,

You cann access the file from the server. Right?
So try to run a command within Genero:

...
let lo_stm = "test -f ", url clipped
run lo_stm  returning lo_errorcode

Try "display lo_errorcode" to see the result

Normaly:
lo_errorcode = 0 --> File exists
lo_errorcode = 1 --> File does not


HTH  Stefan Serwe


Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 10, 2015, 01:47:07 pm
No, the server might not be able to 'see' the file as it could be on a URL.  If the server could 'see' it I could simply use:

if os.Path.exists( filename) then
    etc.

This would word regardless of the OS the server is running - 'test' is a Unix command only.

I need to use frontCall as the place where the file might be is mapped in Windows from the PC running the GDC but not from the server running BDL.


Title: Re: Using frontCall to check if a file exists
Post by: Lionel F. on July 10, 2015, 02:42:29 pm
Hi Sally,

Currently there is no native frontcall for achieving what you expect. There is an existing feature request : GDC#1322 ("Would like a frontcall to check the existence of a file on the client side")  but this is not implemented.

Not sure but... from what I understand you're trying to do this using a frontcall "execute" and a command line that uses "cmd.exe", isn't it?

Thanks.

Regards,
Lionel



Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 10, 2015, 03:54:45 pm
OK, that's unfortunate.  Was trying to use shellexec with 'cmd.ex /c ....' in the input arguments.  Guess I always get true as the command doesn't generate a syntax error, for example.

Thanks


Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 10, 2015, 04:30:11 pm
Wish I could find the issue report you refer to.  The nearest I got is this one:

https://4js.com/support/issue/GDC-01331
Would like a way to know the size of a file located on the client side
Closed, won't fix


Title: Re: Using frontCall to check if a file exists
Post by: Lionel F. on July 10, 2015, 05:20:49 pm
The one your refer to is to know the size of the file on the client side. There is another reference for checking the existence of the file on the client side. It was an internal request, that's why you were not able to access it via the website but I moved it as "public" so you should be able to see it soon at the following URL:
https://4js.com/support/issue/GDC-01322 (https://4js.com/support/issue/GDC-01322)

It's not planned to implement it mainly for security purpose because we tend to limit the access to the environment.

Now if you're able to make work what you expect using "cmd" , it should be possible to achieve this using the "execute" frontcall.

Something like :
Code
  1. LET cmd = 'cmd.exe /C if exist.....'
and then
Code
  1. CALL ui.Interface.frontCall("standard", "execute", [cmd, 0], [ret])

Best regards,
Lionel


Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 11, 2015, 10:08:48 am
This is indeed what I tried, on testing it always seemed to return true.  :(

I will revisit it.


Title: Re: Using frontCall to check if a file exists
Post by: Reuben B. on July 13, 2015, 12:19:20 am
Hi Sally,

You want to use the execute frontcall and not shellexec.  Think of shellexec as the equivalent of double-clicking (or right-click select option) on the file.
When using the execute frontcall, you want to set the second parameter to 1 so that it waits for the result. (similar to RUN RETURNING)
Finally you want to test the exit status of what you launched, not what is displayed to standard output.  So use exit.

So try this...

Code
  1. FUNCTION client_file_exists(filename)
  2. DEFINE filename STRING
  3. DEFINE cmd STRING
  4. DEFINE ok BOOLEAN
  5.  
  6.    LET cmd = SFMT('cmd /C "if exist %1 (exit 0) else (exit 1)"',filename)
  7.    CALL ui.Interface.frontCall("standard","execute",[cmd,1],[ok])
  8.    RETURN ok
  9. END FUNCTION

... you may need to improve it to handle spaces in filenames but a quick test passing gdc.exe and gdc2.exe as arguments returned TRUE/FALSE respectively.

Reuben




Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 13, 2015, 11:22:56 am
Thought 'hurrah' and it does indeed work with gdc.exe and gdc2.exe.  However I am using frontCall as the file might be on a drive the PC (where GDC is) can access but not the server (where BDL is) and I've not managed to make it work with a file specified as a URL e.g. \\<ip-number>\filename, but I think that might be some sort of network problem.  this is very weird as I'm sure I had it working from cmd last Friday.

However have found it works using a URL (\\<ip-number>\path) with a drive that I think is shared via SAMBA, and it will work with a mapped Windows drive.


Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 13, 2015, 11:37:40 am
Another penny has dropped.  It always seemed to find the file last week because of the way it worked using frontCall - actually it never did!

Home & dry, many thanks Reuben.


Title: Re: Using frontCall to check if a file exists
Post by: Nuno G. on July 15, 2015, 12:39:15 pm
One possible workaround would be use fgl_getfile to try to copy the file to server and check the resulting boolean value. It's not perfect but I believe it works.
Regards
Nuno


Title: Re: Using frontCall to check if a file exists
Post by: Sally W. on July 15, 2015, 12:41:54 pm
Hi Nuno

I guess that might work, but it could really overload the network depending on the size of the files, the speed of the network and the frequency of copying.

Reuben B has posted the correct answer above.

Thanks anyway


Title: Re: Using frontCall to check if a file exists
Post by: Jeff M. on July 21, 2015, 12:16:53 pm
Thank you Reuben.

I had a need for this and it works great on a network drive on windows for me.

Jeff


Title: Re: Using frontCall to check if a file exists
Post by: Reuben B. on April 03, 2020, 04:05:45 am
Hi Sally,

You want to use the execute frontcall and not shellexec.  Think of shellexec as the equivalent of double-clicking (or right-click select option) on the file.
When using the execute frontcall, you want to set the second parameter to 1 so that it waits for the result. (similar to RUN RETURNING)
Finally you want to test the exit status of what you launched, not what is displayed to standard output.  So use exit.

So try this...

Code
  1. FUNCTION client_file_exists(filename)
  2. DEFINE filename STRING
  3. DEFINE cmd STRING
  4. DEFINE ok BOOLEAN
  5.  
  6.    LET cmd = SFMT('cmd /C "if exist %1 (exit 0) else (exit 1)"',filename)
  7.    CALL ui.Interface.frontCall("standard","execute",[cmd,1],[ok])
  8.    RETURN ok
  9. END FUNCTION

... you may need to improve it to handle spaces in filenames but a quick test passing gdc.exe and gdc2.exe as arguments returned TRUE/FALSE respectively.

Reuben




This came up in a support call recently where someone had been using the function I suggested above and it was not working with a filename that had spaces.  (Thank you Microsoft for the space in "Program Files", mind you we have used "Four Js" in a directory name at some point too so I can't criticise too much).

After a little bit of experimentation, this worked better and catered for my limited number of test cases.

Code
  1. FUNCTION client_file_exists(filename)
  2. DEFINE filename STRING
  3. DEFINE cmd STRING
  4. DEFINE ok BOOLEAN
  5.  
  6.    LET cmd = SFMT('cmd /C if exist "%1" (exit 0) else (exit 1)',filename)
  7.    CALL ui.Interface.frontCall("standard","execute",[cmd,1],[ok])
  8.    RETURN ok
  9. END FUNCTION

Same disclaimer as before, test to make sure it meets your needs.

Reuben



PS Two things that helped were testing the command in command prompt window to find out exactly what command I needed


Code:
C:\Program Files\FourJs\gdc\3.20.11\bin>cmd /c if exist "gdc.exe" (exit 0) else (exit 1)

C:\Program Files\FourJs\gdc\3.20.11\bin>echo %errorlevel%
0

C:\Program Files\FourJs\gdc\3.20.11\bin>cmd /c if exist "gdc2.exe" (exit 0) else (exit 1)

C:\Program Files\FourJs\gdc\3.20.11\bin>echo %errorlevel%
1

C:\Program Files\FourJs\gdc\3.20.11\bin>cmd /c if exist "C:/Program Files/FourJs/gdc/3.20.11/bin/gdc.exe" (exit 0) else (exit 1)

C:\Program Files\FourJs\gdc\3.20.11\bin>echo %errorlevel%
0

C:\Program Files\FourJs\gdc\3.20.11\bin>cmd /c if exist "C:/Program Files/FourJs/gdc/3.20.11/bin/gdc2.exe" (exit 0) else (exit 1)

C:\Program Files\FourJs\gdc\3.20.11\bin>echo %errorlevel%
1

and also using the GDC Debug Console to view the command as received by the GDC and noting if it was transformed at all e.g.

Code
  1. om 7 {{an 0 FunctionCall 110 {{isSystem "0"} {moduleName "standard"} {name "execute"} {paramCount "2"} {returnCount "1"}} {{FunctionCallParameter 111 {{dataType "STRING"} {isNull "0"} {value "cmd /c if exist \"C:/Program Files/FourJs/gdc/3.20.11/bin/gdc2.exe\" (exit 0) else (exit 1)"}} {}} {FunctionCallParameter 112 {{dataType "INTEGER"} {isNull "0"} {value "1"}} {}}}} {un 95 {{selection "99"}}}}