List files in Igor special folders following aliases (links) and directories

This code will create listing of all files in the Igor special folders (User Procedures for example) while following all linked folders (follow aliases) as well as looking into all folders. The output is in the form of two text waves (path and file name) as well as wave with version numbers (if #pragma version = is used). These waves are located in "root:Packages:UseProcedureFiles". Change as needed...
The code can be easily modified to provide different output or look into different folders.
Example at the top are two functions - one to get User Procedures and one to get Igor Procedures content. Both call static function, which is heavily commented.

Function ListUserProcFiles()
    GetFileFolderInfo/Q/P=Igor "User Procedures"   
    ListProcFiles(S_Path)
end
Function ListIgorProcFiles()
    GetFileFolderInfo/Q/P=Igor "Igor Procedures"   
    ListProcFiles(S_Path)
end

static Function ListProcFiles(PathStr)
    string PathStr
    string OldDf=GetDataFolder(1)
    //create location for the results waves...
    NewDataFolder/O/S root:Packages
    NewDataFolder/O/S root:Packages:UseProcedureFiles
    //if this is top call to the routine we need to wipe out the waves so we remove old junk
    string CurFncName=GetRTStackInfo(1)
    string CallingFncName=GetRTStackInfo(2)
    variable runningTopLevel=0
    if(!stringmatch(CurFncName,CallingFncName))
        KillWaves/Z FileNames, PathToFiles, FIleVersions
        runningTopLevel=1
        //catch if the waves were not killed sice they were in use...
        Wave/Z/T FileNames
        if(WaveExists(FileNames))
            setDataFolder OldDf
            Abort "Output waves could not be killed, they are probably part of table. Please, close all output waves tables or graphs."
        endif
    endif
    //if this was first call, now the waves are gone.
    //and now we need to create the output waves
    Wave/Z/T FileNames
    Wave/Z/T PathToFiles
    Wave/Z FIleVersions
    If(!WaveExists(FileNames) || !WaveExists(PathToFiles))
        Make/O/T/N=0 FileNames, PathToFIles
        Make/O/N=0 FileVersions
        Wave/T FileNames
        Wave/T PathToFiles
        Wave FileVersions
        //I am not sure if we really need all of those declarations, but, well, it should not hurt...
    endif
    //this is temporary path to the place we are looking into now...  
    NewPath/Q/O tempPath, PathStr
    //list al items in this path
    string ItemsInTheFolder= IndexedFile(tempPath,-1,"????")+IndexedDir(tempPath, -1, 0 )
    //remove all . files.
    ItemsInTheFolder = GrepList(ItemsInTheFolder, "^\." ,1)
    //Now we removed all junk files on Macs (starting with .)
    //now lets check what each of these files are and add to the right lists or follow...
    variable i, imax=ItemsInList(ItemsInTheFolder)
    string tempFileName, tempScraptext
    For(i=0;i<imax;i+=1)
        tempFileName = stringfromlist(i,ItemsInTheFolder)
        GetFileFolderInfo/Q/P=tempPath tempFileName
        if(V_isAliasShortcut)
            //is alias, need to follow and look further. Use recursion...
            ListProcFiles(S_aliasPath)
            //and now when we got back, fix the path definition to previous or all will crash...
            NewPath/Q/O tempPath, PathStr
        elseif(V_isFolder) 
            //is folder, need to follow into it. Use recursion.
            ListProcFiles(PathStr+tempFileName+":")
            //and fix the path back or all will fail...
            NewPath/Q/O tempPath, PathStr
        elseif(V_isFile)
            //this is real file. Store information as needed.
            Redimension/N=(numpnts(FileNames)+1) FileNames, PathToFiles,FileVersions
            FileNames[numpnts(FileNames)] = tempFileName
            PathToFiles[numpnts(FileNames)] = PathStr
            //try to get version from #pragma version = ... This seems to be the most robust way I found...
            if(stringmatch(tempFileName, "*.ipf"))
                Grep/P=tempPath/E="(?i)^#pragma[ ]*version[ ]*=[ ]*" tempFileName as "Clipboard"
                tempScraptext = GetScrapText()
                if(strlen(tempScraptext)>10)        //found line with #pragma version"
                    tempScraptext = replaceString("#pragma",tempScraptext,"")   //remove #pragma
                    tempScraptext = replaceString("version",tempScraptext,"")       //remove version
                    tempScraptext = replaceString("=",tempScraptext,"")         //remove =
                    tempScraptext = replaceString("\t",tempScraptext,"  ")          //remove optional tabulators, some actually use them.
                    //forget about the comments behind the text.
                                       //str2num is actually quite clever in this and converts start of the string which makes sense.
                    FileVersions[numpnts(FileNames)]=str2num(tempScraptext)
                else             //no version found, set to NaN
                    FileVersions[numpnts(FileNames)]=NaN
                endif
            else                    //no version for non-ipf files
                FileVersions[numpnts(FileNames)]=NaN
            endif
        endif
    endfor
    if(runningTopLevel)
        //some output here...
        print "Found   "+num2str(numpnts(FileNames))+"  files in   "+PathStr+" folder, its subfolders and linked folders and subfolders"
        KillPath/Z tempPath
    endif
   
    setDataFolder OldDf
end

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More