Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using VBScript to delete a column from CSV file - RESOLVED

Status
Not open for further replies.

KevinNassauNY

Programmer
Nov 13, 2015
1
US
The solution from a previous thread about this did not work for me. It only deleted the column header, none of the data. The reason being the line that measures the UBOUND of ClippedArray was not working. To get around that, I simply looped through the text object line by line. In the following example, I remove column 20 from the input file. This works for me:

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, strLine, dataArray, clippedArray()

InputFile="C:\Users\kevind\Desktop\InSight_Inbound_View_Download.csv"
OutputFile="C:\Users\kevind\Desktop\InSight_Inbound_View_Download_New.csv"

Set fso = CreateObject("Scripting.FileSystemObject")

Set InFile = fso.OpenTextFile(InputFile, ForReading)
Set OutFile = fso.OpenTextFile(OutputFile, ForWriting, True)

Do While InFile.AtEndOfStream <> True

    strLine = InFile.ReadLine
    ReDim Preserve clippedArray(x)
    clippedArray(x) =  Split(strLine,",")
    CutColumn = 20 ' The column I want to delete
    intCount = 0
    newLine = ""

    For Each Element In clippedArray(x)    
        If intCount <> (CutColumn -1) Then
           EndChar = ","
           newLine = newLine & Element & EndChar
        End If
        intCount = intCount + 1	
    Next

    OutFile.WriteLine newLine

Loop

InFile.Close
OutFile.Close

WScript.Echo "Done"


and if you want to delete several columns at once, you could replace this line:

If intCount <> (CutColumn -1) Then

with something like this:

If intCount <> (3 OR 19 OR 24 OR 26) Then ....

(but here you subtract one (-1) from each column you want to remove since they are zero based and you are supplying literals)

Enjoy!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top