File Delete in Lua - General Help and Assistance on using CHDK stable releases - CHDK Forum  

File Delete in Lua

  • 10 Replies
  • 3669 Views
File Delete in Lua
« on: 20 / January / 2019, 11:35:59 »
Advertisements
Hope some kind person can put me out of my frustration.

'All' I'm trying to do is add in the functionality in a script to delete a log file I've created.

At the moment I'm using the following, that doesn't seem to work:

Code: [Select]
    os.remove("CHDK\LOGS\LOG_2306.TXT")
What am I doing wrong?

Re: File Delete in Lua
« Reply #1 on: 20 / January / 2019, 13:00:19 »
What am I doing wrong?

Try :

Code: [Select]
    os.remove("A/CHDK/LOGS/LOG_2306.TXT")
Ported :   A1200    SD940   G10    Powershot N    G16

Re: File Delete in Lua
« Reply #2 on: 20 / January / 2019, 13:14:16 »
@waterwingz

That did it!!!
Do I assume that CHDK cards are always in the A directory?
Also, I was thrown by the separators: "\" vs "/"
I should have used the CHDK file browser to give me the full file name: idiot!
Thanks for the support.
Cheers
Garry

Re: File Delete in Lua
« Reply #3 on: 20 / January / 2019, 14:35:59 »


Do I assume that CHDK cards are always in the A directory?
I believe it's more a drive identifier - in this case for the SD card. 

AFAIK, there is no PowerShot that provides two SD card slots but if it did you would probably use a B to identify the second slot. This probably dates back to the old days of 16 bit computing where the floppy disc drives were labeled A and B (hence hard drives taking the next letter - C - when they came along) 
Ported :   A1200    SD940   G10    Powershot N    G16


Re: File Delete in Lua
« Reply #4 on: 20 / January / 2019, 17:23:21 »
@waterwingz

Many thanks for the insight.

BTW I've come to the conclusion that CHDK Lua doesn't support GOTO: I'm I right?

Cheers

Garry

Re: File Delete in Lua
« Reply #5 on: 20 / January / 2019, 18:04:58 »
BTW I've come to the conclusion that CHDK Lua doesn't support GOTO: I'm I right?
Well, there is this : Lua : goto statement

But as @reyalp is the CHDK Lua expert (and AFAIK the person who added it to CHDK) I will defer comments to him.
Ported :   A1200    SD940   G10    Powershot N    G16

Re: File Delete in Lua
« Reply #6 on: 20 / January / 2019, 18:18:03 »
@waterwingz

Yes I tried that, but kept getting errors.

As you say, let's hope @reyalp puts me right.

Cheers

Garry

*

Offline reyalp

  • ******
  • 14079
Re: File Delete in Lua
« Reply #7 on: 20 / January / 2019, 18:19:29 »
Many thanks for the insight.

BTW I've come to the conclusion that CHDK Lua doesn't support GOTO: I'm I right?
Correct. CHDK uses Lua 5.1, which does not have a GOTO. See http://www.lua.org/manual/5.1/

Generally, I'd suggest breaking things into functions and using returns for an early out.
If you need to share state among functions, using tables in the object:foo() style can be helpful.

edit:
Quote
But as @reyalp is the CHDK Lua expert (and AFAIK the person who added it to CHDK) I will defer comments to him.
To give due credit, the original CHDK Lua implementation was contributed by Velo https://chdk.setepontos.com/index.php/topic,1194.0.html

The reason CHDK is still on 5.1 is that a fair bit of hacking to the Lua source was required to make it work within the limitations of CHDK, and in my experience Lua 5.1 is quite adequate for complex scripts.
« Last Edit: 20 / January / 2019, 18:27:43 by reyalp »
Don't forget what the H stands for.


Re: File Delete in Lua
« Reply #8 on: 20 / January / 2019, 19:45:22 »
@reyalp

What I needed to do was a test up front in my script and use this test to decide if to run the script.

The way I have achieved this is to simply use an if-then-end construct around the main script. Thus seems to work.

My alternative thinking was to do the test and execute a goto.

Cheers

Garry

*

Offline reyalp

  • ******
  • 14079
Re: File Delete in Lua
« Reply #9 on: 20 / January / 2019, 19:55:03 »
What I needed to do was a test up front in my script and use this test to decide if to run the script.

The way I have achieved this is to simply use an if-then-end construct around the main script. Thus seems to work.
That makes sense.

FWIW, you can also use error() to immediately exit the the script from anywhere, so you can do
if condition then
 error("message")
end
rather than needing to wrap you whole script in a block. If you don't want an error message, return outside of a function will also end the current script.

FWIW 2: I noticed https://gist.github.com/pigeonhill/10a43f5ba543bc758f1ce21d28981a89 redefines error, which is legal in Lua but likely not what you want.
Don't forget what the H stands for.

 

Related Topics