Moving several data folders

Hello everyone,

I am trying to move (or regroup) several data folder into pre-defined data folder. My goal is regrouping them to use another function to concetenate them into single waves. Here is an example which shows the data folder structure in my case:

-> root
+ DataFolder1_0000
+ DataFolder1_0001
+ DataFolder1_0002
+ DataFolder1_0003
+ DataFolder2_0000
+ DataFolder2_0001


I would like to create two new data folders (DataFoldersGroup1 & DataFoldersGroup2) and move the data folders ending with "_0000" into DataFoldersGroup1 and all other data folders into DataFoldersGroup2. Does anybody have an idea?

Thanks,
Emre
Here is a starting point:
Function Demo(sourceDFPath, destDFPath, suffix)
    String sourceDFPath // e.g., "root:" - contains data folders to be moved
    String destDFPath       // e.g., "root:Group0"
    String suffix               // e.g., "_0000" to move data folders with that suffix
   
    destDFPath = RemoveEnding(destDFPath, ":")  // Remove trailing colon if any
   
    NewDataFolder /O $destDFPath
    DFREF destDF = $destDFPath
   
    String topDF = sourceDFPath
    Variable numDataFolders = CountObjects(topDF, 4)
   
    Variable index
    for(index=numDataFolders-1; index>=0; index-=1)     // Do in reverse order
        String dfName = GetIndexedObjName(topDF, 4, index)
        if (strlen(dfName) == 0)
            break
        endif
       
        String regExp = ".+" + suffix + "$"     // Match suffix at end of string
        if (GrepString(dfName,regExp))
            String dfPath = topDF + PossiblyQuoteName(dfName)
            String destPath = destDFPath + ":" + PossiblyQuoteName(dfName)
            if (DataFolderExists(destPath))
                Printf "Can't move \"%s\" because it already exists in dest\r", dfPath
            else
                MoveDataFolder $dfPath, destDF
            endif
        endif
    endfor
   
    return 0
End

Hello,

Thank you so much. It is now clear to me. I will test it and post the results.

Cheers,
Emre
Hello,

I was wondering why do we need to do all in reverse order? When I move data folders they are in reverse order and this causes some problems for the next step. I tried to change it. However it doesn't work entirely. What am I doing wrong? First function works correctly but the second function moves only the first data folder and skips the second one (when I have two data folders ending with _0000 and two ending with 0001 and 0002).

Best,
Emre

Function TidyUpFTData(sourceDFPath, destDFPath, suffix)
    String sourceDFPath // e.g., "root:" - contains data folders to be moved
    String destDFPath       // e.g., "root:Group0"
    String suffix               // e.g., "_0000" to move data folders with that suffix
 
    destDFPath = RemoveEnding(destDFPath, ":")  // Remove trailing colon if any
 
    NewDataFolder /O $destDFPath
    DFREF destDF = $destDFPath
 
    String topDF = sourceDFPath
    Variable numDataFolders = CountObjects(topDF, 4)
 
    Variable index
    for(index= 0; index <= numDataFolders-1; index += 1)        // Other way around???
        String dfName = GetIndexedObjName(topDF, 4, index)
        if (strlen(dfName) == 0)
            break
        endif
 
        String regExp = ".+" + suffix + "$"     // Match suffix at end of string
        if (GrepString(dfName,regExp))
            String dfPath = topDF + PossiblyQuoteName(dfName)
            String destPath = destDFPath + ":" + PossiblyQuoteName(dfName)
            if (DataFolderExists(destPath))
                Printf "Can't move \"%s\" because it already exists in dest\r", dfPath
            else
                MoveDataFolder $dfPath, destDF
            endif
        endif
    endfor
    return 0
End

Function TidyUpRealData(sourceDFPath, destDFPath, prefix)
    String sourceDFPath // e.g., "root:" - contains data folders to be moved
    String destDFPath       // e.g., "root:Group0"
    String prefix               // e.g., "WIBS" to move data folders with that prefix
 
    destDFPath = RemoveEnding(destDFPath, ":")  // Remove trailing colon if any
 
    NewDataFolder /O $destDFPath
    DFREF destDF = $destDFPath
   
    String topDF = sourceDFPath
    Variable numDataFolders = CountObjects(topDF, 4)
 
    Variable index
    for(index= 0; index <= numDataFolders-2; index += 1)        // Other way around???
        String dfName = GetIndexedObjName(topDF, 4, index)
        if (strlen(dfName) == 0)
            break
        endif
 
        String regExp = prefix [0,3]                        // Match prefix at beginning of string
        if (GrepString(dfName,regExp))
            String dfPath = topDF + PossiblyQuoteName(dfName)
            String destPath = destDFPath + ":" + PossiblyQuoteName(dfName)
            if (DataFolderExists(destPath))
                Printf "Can't move \"%s\" because it already exists in dest\r", dfPath
            else
                MoveDataFolder $dfPath, destDF
            endif
        endif
        endfor
    return 0
End
Quote:
I was wondering why do we need to do all in reverse order?


Because otherwise index and numDataFolders will be wrong.

You can probably fix this by decrementing index and numDataFolders after calling MoveDataFolder.
Could you please explain what you mean? I tried it other way around and works for upper part of the code. However, it fails somehow for the lower part. It doesn't make any sence.
Quote:
Could you please explain what you mean?


Assuming you are moving data folders in forward order, when you move a data folder out of the parent data folder, numDataFolders is 1 fewer than when you started so you must adjust its value by subtracting 1 from it.

Similarly index is affected by the fact that you moved the data folder out of the parent and you must adjust its value by subtracting 1 from it.
Thanks for the comment. I learned something new about the "index". The problem with my code was that index changes if I move a data folder and this needs to be corrected by adjusting index to zero to be able to start looking frim the top of the list. Now it works pretty good.

 
Function TidyUpFTData(sourceDFPath, destDFPath, suffix)
    String sourceDFPath // e.g., "root:" - contains data folders to be moved
    String destDFPath       // e.g., "root:Group0"
    String suffix               // e.g., "_0000" to move data folders with that suffix
 
    destDFPath = RemoveEnding(destDFPath, ":")  // Remove trailing colon if any
 
    NewDataFolder /O $destDFPath
    DFREF destDF = $destDFPath
 
    String topDF = sourceDFPath
    Variable numDataFolders = CountObjects(topDF, 4)
 
    Variable index
    for(index = 0; index <= numDataFolders-2;)      // Do in reverse order
        String dfName = GetIndexedObjName(topDF, 4, index)
        if (strlen(dfName) == 0)
            break
        endif
 
        String regExp = ".+" + suffix + "$"     // Match suffix at end of string
        if (GrepString(dfName,regExp))
            index = 0
            String dfPath = topDF + PossiblyQuoteName(dfName)
            String destPath = destDFPath + ":" + PossiblyQuoteName(dfName)
            if (DataFolderExists(destPath))
                Printf "Can't move \"%s\" because it already exists in dest\r", dfPath
            else
                MoveDataFolder $dfPath, destDF
            endif
        else
            index += 1
        endif
    endfor
    return 0
End

Function TidyUpRealData(sourceDFPath, destDFPath, prefix)
    String sourceDFPath // e.g., "root:" - contains data folders to be moved
    String destDFPath       // e.g., "root:Group0"
    String prefix               // e.g., "WIBS" to move data folders with that prefix
 
    destDFPath = RemoveEnding(destDFPath, ":")  // Remove trailing colon if any
 
    NewDataFolder /O $destDFPath
    DFREF destDF = $destDFPath
   
    String topDF = sourceDFPath
    Variable numDataFolders = CountObjects(topDF, 4)
 
    Variable index
    for(index=0; index <= numDataFolders-3;)        // Do in reverse order
        String dfName = GetIndexedObjName(topDF, 4, index)
        if (strlen(dfName) == 0)
            break
        endif
 
        String regExp = prefix [0,3]                        // Match prefix at beginning of string
        if (GrepString(dfName,regExp))
            index = 0
            String dfPath = topDF + PossiblyQuoteName(dfName)
            String destPath = destDFPath + ":" + PossiblyQuoteName(dfName)
            if (DataFolderExists(destPath))
                Printf "Can't move \"%s\" because it already exists in dest\r", dfPath
            else
                MoveDataFolder $dfPath, destDF
            endif
        else
            break
        endif
        endfor
    return 0
End