Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: Using frontCall to check if a file exists  (Read 17949 times)
Sally W.
Posts: 32


« 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...
Stefan S.
Posts: 84


« Reply #1 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
Sally W.
Posts: 32


« Reply #2 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.
Lionel F.
Four Js
Posts: 82


« Reply #3 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

Sally W.
Posts: 32


« Reply #4 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
Sally W.
Posts: 32


« Reply #5 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
Lionel F.
Four Js
Posts: 82


« Reply #6 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

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
Sally W.
Posts: 32


« Reply #7 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.
Reuben B.
Four Js
Posts: 965


« Reply #8 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



Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Sally W.
Posts: 32


« Reply #9 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.
Sally W.
Posts: 32


« Reply #10 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.
Nuno G.
Posts: 38


« Reply #11 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
Sally W.
Posts: 32


« Reply #12 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
Jeff M.
Posts: 38


« Reply #13 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
Reuben B.
Four Js
Posts: 965


« Reply #14 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"}}}}










Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines