A forced NewDataFolder to handle complex paths

The built-in NewDataFolder operation can create a missing data folder and switch to it but chokes on paths containing subfolders and possibly up-levels (::). This function accepts a relative or absolute path as string and returns a DFREF to the last folder in destPath (for switching to, if desired). It travels up folders (::=up 1, :::=up 2, ...) and creates subfolders as necessary; attempts to up-level past root are ignored.



// forceNewDataFolder( destPath )
//  string  destPath    relative or absolute path of directory to create
//
// Returns a DFREF which can be used to switching or referring to
//
Function/DF forceNewDataFolder( destPath )
    string destPath
    variable i 
    string folder = ""
    string up = ""
    DFREF sav0 = GetDataFolderDFR()
    DFREF out  
    for (i=0; i<ItemsInList(destPath, ":"); i+=1)                       // for each folder in path
//      folder = PossiblyQuoteName(StringFromList(i, destPath, ":"))            // extract from list, possibly quoting
        folder = ReplaceString("'", StringFromList(i, destPath, ":"), "")       // extract from list, quotes will screw up NewDatafolder
        If ( !strlen(folder) )                                          // if blank entry
            up += SelectString(i, "", ":")                                  // include semicolon, except for first row
            continue                                                    // skip remainder of loop
        elseif (strlen(up))                                         // if not blank but was
            up += ":"                                                   // grouped :'s lose the first ":" so replace it
            up = up[0, min(strlen(up), ItemsInList(GetDataFolder(1), ":"))-1]           // remove :'s which refer upwards of root
            SetDataFolder $up                                           // switch up levels
        endif
        If (cmpstr(folder,"root"))                                      // if folder isn't "root"
            NewDataFolder/O/S $folder                                   // create/switch to
        else                                                        // but if it is
            SetDataFolder root:                                         // start in root, don't try to make it
        endif
        up = ""                                                     // clear up before next loop
    endfor
    out = GetDataFolderDFR()                                        // capture current location to DFREF first
    SetDataFolder sav0                                          // then return user to prev folder
    return out                                                  // and hand back the DFREF
end

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More