Friday, August 16, 2024

vb

' VBScript to convert a text file to a CSV file

' Create a FileSystemObject to handle the file operations
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Open the input text file for reading (Make sure the text file path is correct)
Set objTextFile = objFSO.OpenTextFile("C:\path\to\your\input.txt", 1)

' Create or overwrite the output CSV file
Set objCSVFile = objFSO.CreateTextFile("C:\path\to\your\output.csv", True)

' Read the file line by line
Do Until objTextFile.AtEndOfStream
    strLine = objTextFile.ReadLine

    ' Assuming columns are separated by a tab, space, or comma, adjust this if needed
    ' Here we use tabs (vbTab) as an example
    arrValues = Split(strLine, vbTab)
    
    ' If your text file uses spaces or commas, use this:
    ' arrValues = Split(strLine, " ") for space-separated columns
    ' arrValues = Split(strLine, ",") for comma-separated columns

    ' Convert the array to a CSV format by joining with commas
    strCSVLine = Join(arrValues, ",")

    ' Write the CSV line to the output file
    objCSVFile.WriteLine strCSVLine
Loop

' Close the files
objTextFile.Close
objCSVFile.Close

' Notify the user
MsgBox "Conversion to CSV completed successfully!"

No comments:

Post a Comment