The following function reads in a TWS and attempts to parse it for the file names. It returns an unnamed list. It is easy to modify it, as written it expects file names to only contain letters and numbers and : to mark Windows drive letters.
Code: Select all
scan.twsFile <- function(twsFile="/people/ccs3/morlan/research/bcp/bcp.tws",type="latex")
{# PURPOSE: To read in a TextPad TWS file and produce a list of files
# USE: lst <- scan.twsFile(twsFile="<fullpath to twsfile>")
if ( exists("con") ) close(con)
con <- file(twsFile,"rb")
str <- NULL
fileSize <- file.info(twsFile)$size
for ( ii in 1:fileSize )
{
tried <- try(newC <- readChar(con,1),silent=TRUE)
if ( class(tried)!="try-error" ) str <- c(str,newC)
}
c0 <- str %in% c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
":", "\\",".")
str <- str[c0]
str <- gsub("\\\\","/",str)
str <- paste(str,collapse="")
###
# hope the markers don't change
###
keyStr0 <- "EC"
keyStr1 <- "OD"
c0 <- regexpr(keyStr0,str)
editFiles <- NULL
while (c0[1]>0)
{
str <- substring(str,c0[1]+nchar(keyStr0))
c0 <- regexpr(keyStr1,str)
str <- substring(str,c0[1]+nchar(keyStr1))
keyStr0 <- "TIDEVIEW"
keyStr1 <- "VIEWECOD"
c1 <- regexpr(keyStr0,str)
editFiles <- c(editFiles,substring(str,1,c1-1))
str <- substring(str,c1)
c0 <- regexpr(keyStr0,str)
}
editFiles <- editFiles[editFiles!=""]
c0 <- substring(editFiles,1,1)=="."
editFiles[c0] <- substring(editFiles[c0],2)
return(invisible(editFiles))
# END FUNCTION scan.twsFile() as found in source(paste(RCommon,"LatexTools/latex.twsFiles.R",sep=""))
}# END FUNCTION scan.twsFile()