Deleting files using a Wild Card and clear all Globals

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Deleting files using a Wild Card and clear all Globals

Post by Nakia » Tue Aug 14, 2012 11:22 am

Hi,

Trying to use the following to delete any files that exist in the Cache Directory that end with .dat
Can someone tell me what stupid mistake I have made..'

Also, is there a way to empty all Global Variables in the Stack?
delete URL("file:" & specialFolderPath("Cache") & "/" & "*" & ".dat")

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Deleting files using a Wild Card and clear all Globals

Post by Klaus » Tue Aug 14, 2012 11:44 am

Hi Nakia,

1. file operations do not work with wildcards!
You need to get a list of the files, do your filtering and then apply your actions to the list of files.

Like this (out of my head):

Code: Select all

...
put the folder into olddir

## Need this later for the list of ABSOLUTE pathnames
put specialfolderpath("cache") & "/" into tCacheFolder
set the directory tCacheFolder

## Get the list of files
put the files into tFiles

## Reset directory = good programming style  :) 
set the directory to olddir

## Filter list of files:
filter tFiles with "*.dat"

if tFiles = empty then
  exit whatever_handler_we_are_in
end if

repeat for each line i in tFiles
   delete file (tCacheFolder & i)
end repeat
...
2. check "delete variable" in the dictionary, yes, there are many helpful infos in the dictionary 8)
And "globalnames"!


Best

Klaus

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: Deleting files using a Wild Card and clear all Globals

Post by Nakia » Tue Aug 14, 2012 12:56 pm

Thanks Klaus,

what does the "i" reference?
I have not used that before..

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: Deleting files using a Wild Card and clear all Globals

Post by shaosean » Tue Aug 14, 2012 1:08 pm

i is a variable that will store the current line in the repeat loop

Code: Select all

repeat for each line i in tFiles
   delete file (tCacheFolder & i)
end repeat
so lets say in the variable tFiles you had the following list:
  • budget.xls
  • family_photo.jpg
  • demo.mp3
the loop will happen three times and each time the variable i will contain the next line of data

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Deleting files using a Wild Card and clear all Globals

Post by Klaus » Tue Aug 14, 2012 1:09 pm

Hi Nakia,

well, there are two possibilities top use repeat":
1.

Code: Select all

repeat with i = 1 to the num of lines of fld XYZ
  ## do someting with "line i of fld XYZ"
end repeat
This will do for most cases and is fast enough for lots of repeats.
But when unsing "repeat with x =..." the engine will always start to count from 1 (or your start value).

Here: i = the current number of the loop

#################################################################
2. For VERY fast "repeat" loops you can use "repeat for each..."

Code: Select all

repeat for each line i in MyVar
   ## do something
end repeat
The min difference is that i is NOT a number here but the CONTENT of that line!
Advantage:
The engine is just passing the values to your handler and does NOT count from 1 every time!
THIS IS INSANELY FAST!!!!!!!
Means processing several thousand of lines in fractions of a seconds, depending on your handlers!

Disadvantage:
a. You cannot alter/modify i in this case:

Code: Select all

repeat for each line i in MyVar
  ## this does not work!
   delete char 1 of i
end repeat
Do something lime this:

Code: Select all

repeat for each line i in MyVar
  ## this does not work!
  put i into i2
   delete char 1 of i2
  put i2 & CR after myNewList
end repeat
## Get rid of trailing CR
delete char -1 of MyNewList
## do something with this variuable
b. I f you need a counter though, you need to create your own:

Code: Select all

put 0 into tCounter
repeat for each line i in MyVar
   add 1 to tCounter
   ## do something
end repeat
...
REPEAT FOR EACH does not support all kind of loops:
repeat for each cd i in this stack #not works

Check the dictionary for more info.


Best

Klaus

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Deleting files using a Wild Card and clear all Globals

Post by sturgis » Tue Aug 14, 2012 5:12 pm

/begin slight thread hijack
Hey Klaus, for cycling through cards using the for each method could you do something like

put the cardnames into tNames
put 0 into tCounter
repeat for each line tLine in tNames
add 1 to tCounter
## do something with cd tCounter of this stack
end repeat

(using the counter rather than the name to avoid issues with duplicate names)

I've haven't had a project with enough cards for this to become an issue but I can see the possibility that it would so the time savings might be worthwhile?

/end hijack

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Deleting files using a Wild Card and clear all Globals

Post by Klaus » Tue Aug 14, 2012 6:22 pm

Hi Jack :D

I'm afraid this is not much faster than: repeat with i = 1 to the num of cds

One shouldn't have too many cards anyway :D


Best

Klaus

EDIT:
Corrected an ugly syntax error 8)

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Deleting files using a Wild Card and clear all Globals

Post by sturgis » Tue Aug 14, 2012 8:10 pm

Oh, duh. Because you're not then accessing "line i of..." so yeah speedwise it wouldn't make much diff. Thx, I knew I was thinking wrong somewhere in there.

Klaus
Posts: 14212
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Deleting files using a Wild Card and clear all Globals

Post by Klaus » Wed Aug 15, 2012 1:50 pm

The difference between "repeat with i = X" and "repeat fore each" will only be noticable
when it comes to < 1000's of cds/lines etc.

But then the difference is HUGE! :D

Post Reply