lua scripting help

  • 4 Replies
  • 945 Views
lua scripting help
« on: 09 / January / 2015, 09:46:02 »
Advertisements
Here come all my limits in lua scripting  ::)

How to delete, via lua script line, all *.cfg and *.0 files in A/CHDK/DATA/ directory?

Thank you


*

Offline reyalp

  • ******
  • 11496
Re: lua scripting help
« Reply #1 on: 09 / January / 2015, 17:05:30 »
Here come all my limits in lua scripting  ::)

How to delete, via lua script line, all *.cfg and *.0 files in A/CHDK/DATA/ directory?

Thank you
Of the top my head (not tested), something like

Code: [Select]
for f in os.idir('A/CHDK/DATA') do
 if f:match('%.CFG$') or f:match('%.0$') then
  os.remove(f)
 end
end
Further reading:
os.dir http://chdk.wikia.com/wiki/Lua#Added_features_2
string.match http://www.lua.org/manual/5.1/manual.html#5.4
Don't forget what the H stands for.

Re: lua scripting help
« Reply #2 on: 09 / January / 2015, 17:51:29 »
Of the top my head (not tested), something like

Code: [Select]
for f in os.idir('A/CHDK/DATA') do
 if f:match('%.CFG$') or f:match('%.0$') then
  os.remove(f)
 end
end
Further reading:
os.dir http://chdk.wikia.com/wiki/Lua#Added_features_2
string.match http://www.lua.org/manual/5.1/manual.html#5.4

Thanks reyalp but it doesn't work  :(
Maybe i have to replace the "*" wildcard with a "..string.format(something here).." ?

...lost in (lua) space...

*

Offline reyalp

  • ******
  • 11496
Re: lua scripting help
« Reply #3 on: 09 / January / 2015, 21:15:57 »
Thanks reyalp but it doesn't work  :(
Which part doesn't work? If you can't tell, my usual approach would be to add some print statements, and also check the return status of os.remove.

I think see my mistake though, idir just returns the file name, not the full path, so os.remove was just trying to remove ('FOO.CFG') or whatever. Also, Lua patterns used in match are case sensitive, so .cfg wouldn't have been found in the previous code.

Revised version (still untested):
Code: [Select]
for f in os.idir('A/CHDK/DATA') do
 if f:match('%.[cC][fF][gG]$') or f:match('%.0$') then
  if not os.remove('A/CHDK/DATA/'..f) then
    error('remove failed')
  end
 end
end
Don't forget what the H stands for.


Re: lua scripting help
« Reply #4 on: 10 / January / 2015, 08:55:32 »
Which part doesn't work? If you can't tell, my usual approach would be to add some print statements, and also check the return status of os.remove.

I think see my mistake though, idir just returns the file name, not the full path, so os.remove was just trying to remove ('FOO.CFG') or whatever. Also, Lua patterns used in match are case sensitive, so .cfg wouldn't have been found in the previous code.

Revised version (still untested):
Code: [Select]
for f in os.idir('A/CHDK/DATA') do
 if f:match('%.[cC][fF][gG]$') or f:match('%.0$') then
  if not os.remove('A/CHDK/DATA/'..f) then
    error('remove failed')
  end
 end
end

Sorry for late reply ... from the other side of the world.
The revised version works like a charm!

Thanks for don't letting me drown into the lua ocean.  :)

 

Related Topics