Michael
Mon May 08 19:47:19 CDT 2006
mj.redfox.mj@gmail.com wrote:
> I think I've cracked this, for anyone interested. This will cope with
> nested commas and quotes.
>
>
I first posted a csvparse function (vbscript code) 5 years ago and many
people use it - feel free.
csvparse group:*.scripting - Google Groups
http://groups.google.com/groups?q=csvparse group:*.scripting
> Dim fso, fsoFolder, fsoFile, sFolder, sFileName, sCreateFile,
> sDeleteFile
> Dim readdata, writedata, dataarray, arraycount, writestring
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set fsoFolder = fso.GetFolder(sFolder)
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set sOpenFile = fso.OpenTextFile("folder\file.csv", 1)
>
> set sCreateFile = fso.CreateTextFile("temp\temp.csv")
>
>
> do while not sOpenFile.atendofstream
>
> writestring = ""
> readdata = sOpenFile.ReadLine
> dataarray = split(readdata, ",")
>
> for arraycount = 0 to ubound(dataarray)
>
> if (left(dataarray(arraycount), 1) = """" or
> right(dataarray(arraycount), 1) = """") and len(dataarray(arraycount))
>> 2 then
> if left(dataarray(arraycount), 1) = """" then
> writestring = writestring & """"
> else
> writestring = writestring & left(dataarray(arraycount),1)
> end if
> writestring = writestring &
> replace(mid(dataarray(arraycount),2,len(dataarray(arraycount))-2),
> chr(34), "'")
> if right(dataarray(arraycount), 1) = """" then
> writestring = writestring & """"
> else
> writestring = writestring & right(dataarray(arraycount),1)
> end if
> elseif dataarray(arraycount) = """""" then
> writestring = writestring & dataarray(arraycount)
> else
> writestring = writestring & replace(dataarray(arraycount), chr(34),
> "'")
> end if
>
> if arraycount < ubound(dataarray) then writestring = writestring & ","
>
> next
> sCreateFile.writeline(writestring)
>
> loop
--
Michael Harris
Microsoft MVP Scripting