Create Cleaned Up Version of a Plain Text File

// WriteStrippedDataToNewFile(refNum, pathName, origFileName, newFileName)
// Creates new file containing cleaned up contents of original file.
// The original file is assumed to be a plain text file.
// The following are removed from the data:
//  All control characters except for carriage-return (0x0D)
//  All characters with codes greater than 0x7F (characters not in standard ASCII set)
// If newFileName exists, it is overwritten.
Function WriteStrippedDataToNewFile(pathName, origFileName, newFileName)
    String pathName             // Symbolic path where new original file exists
    String origFileName             // Name of file containing original data
    String newFileName              // Name of new file into which clean data is written

    Variable refNum                         // RefNum of raw data file
    Open/R/P=$pathName refNum as origFileName
    if (strlen(S_fileName) == 0)
        return -1                           // Error or user canceled from open file dialog.
    endif

    FStatus refNum                          // Sets V_logEOF to number of bytes in file
    Variable numBytesInFile = V_logEOF
    Make/O/B/U/N=(numBytesInFile) tempRawData
    FBinRead refNum, tempRawData            // Load raw data
   
    Variable numBytesAvailableInCleanData = numBytesInFile
    Make/O/B/U/N=(numBytesAvailableInCleanData) tempCleanData
   
    Variable numCleanBytes = 0
    Variable ch
    Variable i
    for(i=0; i<numBytesInFile; i+=1)
        ch = tempRawData[i]
        if (ch<0x80 && (ch>=0x20 || ch==0x0D))
            tempCleanData[numCleanBytes] = ch
            numCleanBytes += 1
        endif  
    endfor
   
    DeletePoints numCleanBytes, numBytesAvailableInCleanData - numCleanBytes, tempCleanData
   
    // Write clean data out to new file
    Variable cleanRefNum
    Open/P=$pathName cleanRefNum as newFileName
    FBinWrite cleanRefNum, tempCleanData
    Close cleanRefNum
   
    KillWaves/Z tempRawData, tempCleanData

    return 0
End

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More