×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using VBScript to delete a column from CSV file - RESOLVED

Using VBScript to delete a column from CSV file - RESOLVED

Using VBScript to delete a column from CSV file - RESOLVED

(OP)
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 --> VBScript

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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close