Can't rename or duplicate the file

Hi everyone,

I have tried to make a script to load ibw files, and that is no bigger problem, but when I try to rename or duplicate the files in the same script something happens and it don't work. I have copied my script below and I really hope that someone can tell me what I have done wrong.

Kindest regards
Jörgen
Function Load_ibw_Wave(start_Num, end_Num)
    String start_Num, end_Num
    String root:S_waveNames
   
    String fileName, old_fileName, S_stop_Num
    String file_List, file_Path, file_Num, new_FileName
   
    Variable i=0, numItems, List_Num, Match_Num, file_Length
    Variable single_Num=1, stop_Num=str2num(end_Num)+1, count_up=str2num(start_Num)
   
    start_Num=start_Num+"*"
    end_Num=end_Num+"*"
    S_stop_Num="0"+num2str(stop_Num)+"*"
   
    NewPath/O/Q my_Path "Macintosh HD:Users:gladh:Documents:Projects:DESY 2018"
   
    file_List = IndexedFile(my_Path, -1, ".ibw")
    file_List = SortList(file_List, ";", 16)
    numItems = ItemsInList(file_List)
   
    do
        fileName = StringFromList(i, file_List)
        Match_Num=StringMatch(fileName, start_Num)
       
        if(Match_Num==1)
            Match_Num=0

            fileName = StringFromList(i, file_List)
            file_Length=strlen(fileName)
            LoadWave/O/A/P=my_Path/E=0/Q fileName
            file_Num=fileName[1,3]
           
            old_fileName=S_waveNames
            new_FileName=file_Num+old_fileName
           
            Duplicate/O $old_fileName, $new_FileName; KillWaves root:$old_fileName
           
            count_up+=1
            start_Num="0"+num2str(count_up)+"*"
        endif
       
        i+=1
       
        if(stringmatch(start_Num,S_stop_Num)==1)
            Break
        endif
    while(i<numItems)
End
I assume by renaming "files", you actually refer to waves.

The solution might depend a little on your data structure.
Did you enable the debugger (Right click on the code and select "Enable Debugger" & "Debug on Error")?
Where does the code stop? Is there a more specific error message given?

On a first shot, I'd check the content of S_wavenames (plural! there might be several) and whether "new_FileName" is a valid name in your Igor version.
displayhelptopic "Wave Names"

HJ
Hi,

One thing to check is the namespace of the S_wavenames variable. I think I ran into a similar problem assuming that the string in the root folder was the being undated with the load function. It appeared that the loadwave function creates an implicit local variable S_wavenames. By explicitly having the previously created string may not have the updated contents.

I would suggest a simple print statement if you are not comfortable with the debugger.

Andy
One problem with your code is that S_waveNames is a semicolon-separated string. That means that there is a semicolon after each wave name. To get the wave name without a semicolon, you need to use StringFromList, as shown below.

This makes no sense:
String root:S_waveNames

For one thing, S_waveNames does not exist until after LoadWave executes.

Your use of root: (KillWaves root:$old_fileName) is also wrong. LoadWave works in the current data folder which may or may not be root.

I suspect there may be other problems.

Instead of this:
old_fileName=S_waveNames
new_FileName=file_Num+old_fileName
Duplicate/O $old_fileName, $new_FileName; KillWaves root:$old_fileName


your might try this:

String old_WaveName = StringFromList(0, S_waveNames)
String new_WaveName = file_Num + old_WaveName
Duplicate/O $old_WaveName, $new_WaveName
Wave oldWave = $old_WaveName
KillWaves oldWave


The whole approach of loading the wave, duplicating it with a new name, and killing the loaded wave is unnecessary. You can tell LoadWave to assign the desired final name using the /B flag. Then you don't need any renaming, duplication or killing of waves.

It would make more sense if the start_Num, end_Num parameters were numeric (Variable), not strings.
Thank you all for the input on my problem. By taking a little of all of your recommendations I got it to work. So thanks again.

Jörgen