Quicker Way to Overwrite Folders

I haven't been able to find a good overwrite function for folders, so I use this:

            string/g reffftfolderstr = "root:"+ref+":fft"
            if(datafolderexists(reffftfolderstr)==1)
            killdatafolder $reffftfolderstr
         newdatafolderpath(reffftfolderstr)
         else
         newdatafolderpath(reffftfolderstr)
            endif


Is there a faster way to do this? Especially, if I have to do this to five folders in a function, is there way to make a text wave to cycle through the folders I would like to kill? Also, note that there is a unique string title, such as reffftfolderstr, being created for each.

newdatafolderpath is a function pulled off the Igor Exchange that creates data folder paths.
If I understand correctly, it should work simply with a for loop around your code:

make /T RefWave={"Folder1","Folder2"} // or a global text wave reference
variable i
for (i=0;i<numpnts(RefWave);i+=1)
    string/g reffftfolderstr = "root:"+RefWave[i]+":fft"  // Why global?    Note: RefWave[i]
    if(datafolderexists(reffftfolderstr)==1)
        killdatafolder $reffftfolderstr
        newdatafolderpath(reffftfolderstr)
    else
        newdatafolderpath(reffftfolderstr)
    endif
endfor

Not tested but it should work like this.
HJ
This would have the global string be created as the same for each Folder1 and Folder2.

I've been on the fence about whether or not to use global.

I have many functions that call from each other, and often I will need to pull waves from a folder different than the one I'm in. I like being able to list the svar folder names and other definitions at the beginning of a folder so that I don't have to import variables and strings into each function. I simply use function1() and have every global string, etc. defined as svar and nvar.

But, I'm a little confused as to how global these strings and variables are...If they're really global, is there a way not to have to load them into new functions each time?
geologic wrote:
I've been on the fence about whether or not to use global.

In general, you should avoid global variables. They make it hard to know how a change in one place will affect code in another place.
Quote:
I have many functions that call from each other, and often I will need to pull waves from a folder different than the one I'm in. I like being able to list the svar folder names and other definitions at the beginning of a folder so that I don't have to import variables and strings into each function. I simply use function1() and have every global string, etc. defined as svar and nvar.

Use a structure instead. In a top-level function you can create a structure instance and load it up with all the info your system needs. Then pass that single object around to all the sub-functions.
Quote:
But, I'm a little confused as to how global these strings and variables are...If they're really global, is there a way not to have to load them into new functions each time?

DisplayHelpTopic "Runtime Lookup of Globals"
And there's a lot of good material before and after that topic.

John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
I would recommend to have a closer look at the scope of variables in the manual.
displayhelptopic "Runtime Lookup of Globals"
NVar and SVar provide a connection to a global variable or string. Variable / String creates a local variable or string which is lost after the function ends. The /G flag does the same but keeps the object in memory and lists it in the data folder structure.

You string is overwritten in every iteration of the loop since you don't change the data folder (or you are in the wrong one). If you need to use many variables etc. you might want to store them together in a dedicated data folder (eg. root:MySettings) or use data structures in case you have different parameter sets for each experiment set (eg. data42016, data42116, data42216 ect.)

HJ

PS: Saw JW's post after I posted my comment.
johnweeks wrote:
In general, you should avoid global variables. They make it hard to know how a change in one place will affect code in another place.


I'm not redefining these variables. I'm assigning folder names, results, constants one value and then leaving it. I *think* global variables will work like this even if it's not the elegant solution.


johnweeks wrote:
Use a structure instead. In a top-level function you can create a structure instance and load it up with all the info your system needs. Then pass that single object around to all the sub-functions.


I'll look into this. I'd be adding new global variables throughout the function, so I need to make sure this is the right choice. For instance, maybe I take the input parameters and do a calculation and the result of this calculation would be used elsewhere in the function, so I would like to declare it as a global variable or part of a structure.

I'm going through the documentation now, but wanted to outline what I'm looking for in case anyone can comment in the mean time.
HJDrescher wrote:
I would recommend to have a closer look at the scope of variables in the manual.
displayhelptopic "Runtime Lookup of Globals"
NVar and SVar provide a connection to a global variable or string. Variable / String creates a local variable or string which is lost after the function ends. The /G flag does the same but keeps the object in memory and lists it in the data folder structure.

You string is overwritten in every iteration of the loop since you don't change the data folder (or you are in the wrong one). If you need to use many variables etc. you might want to store them together in a dedicated data folder (eg. root:MySettings) or use data structures in case you have different parameter sets for each experiment set (eg. data42016, data42116, data42216 ect.)

HJ

PS: Saw JW's post after I posted my comment.


I see...I need to learn about data structures...
There is no datafolder change in your code. Hence, the string /g command overwrites the global string (most likely located in root) in each iteration of the loop.
HJ
I see. My current scheme is to have the data folders all as global strings in root. You're right, I could have made a loop that would change data folder and have the global string for each of these folders have the same name within those folders.

At least for the level I'm coding at, I think I can use global strings without too much headache. Regarding the structures, am I right in understanding that this can be a block of strings that I can append new strings to?
Maybe you want ot have a look at
displayhelptopic "getdatafolder".
There is no need to store the name of a data folder in a global string in that specific data folder...

Concerning the structures: yes you could. Maybe a 'list of strings' (->stringfromlist), a text wave, or a wave of data folder references MIGHT also be applicable, depending on the actual problem.
HJ