Question

I have a simple bit of VBScript which "pads" out a .csv file to have a specific number of columns (useful when the .csv file doesn't have a uniform number of columns). My .wsf file works perfectly but when I incorporated this into a .hta file as part of a toolbox it fails.

I'm not looking for help with the code as such, more just an explanation on why it fails in one file and not the other. Sorry for the poor code but does anyone have ideas?

Works.wsf

 <JOB ID="CSVPad">
<SCRIPT LANGUAGE="VBScript">    
    Dim delim, colNum
    'Set delim based on delimiter type of CSV file: 1 = Comma 2 = Pipe 3 = Caret    
    delim = 2
    'Set colNum based on the number of columns to pad out to
    colNum = 10
    ''Get location of .csv file
    'set the type of dialog box you want to use: 1 = Open 2 = SaveAs 3 = File Picker 4 = Folder Picker
    Const msoFileDialogOpen = 1
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objWord = CreateObject("Word.Application")
    Set WshShell = CreateObject("WScript.Shell")
    'Launch at default path
    strInitialPath = WshShell.ExpandEnvironmentStrings("V:\Toolbox\Test")
    objWord.ChangeFileOpEndirectory(strInitialPath)

    With objWord.FileDialog(msoFileDialogOpen)
        .Title = "Select the file to process"
        .AllowMultiSelect = False
        .Filters.Clear
        .Filters.Add "CSV Files / TXT Files", "*.csv; *.txt"
        .Filters.Add "All Files", "*.*"              
        If .Show = -1 Then 
            For Each File in .SelectedItems 
            Set objFile = fso.GetFile(File) 
            Next 
        End If
    End With
    objWord.Quit

    Dim oFso, oReg, objInputFile, objOutputFile, sLine, lCount
    Const ForReading = 1
    Set oReg = New RegExp
    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set objInputFile = oFso.OpenTextFile(objFile.Path,1)
    Set objOutputFile = oFso.OpenTextFile(objFile.Path&".bak",8,True)
    Do Until objInputFile.AtEndOfStream
        sLine = objInputFile.ReadLine
        oReg.Global = True
        If delim = 1 Then
            oReg.Pattern = ",(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
        ElseIf delim = 2 Then
            oReg.Pattern = "\|(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
        ElseIf delim = 3 Then
            oReg.Pattern = "\^(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
        End If
        lCount = oReg.Execute(sLine).Count + 1
        Do While lCount < colNum
            If delim = 1 Then
                sLine = sLine + ","
            ElseIf delim = 2 Then
                sLine = sLine + "|"
            ElseIf delim = 3 Then
                sLine = sLine + "^"
            End If
            lCount = oReg.Execute(sLine).Count + 1
        Loop
        objOutputFile.WriteLine(sLine)
        Set lCount = Nothing
    Loop
    objInputFile.Close
    objOutputFile.Close
    Set oFso = Nothing
    Set oReg = Nothing
    Set delim = Nothing
    Set sLine = Nothing
    Set lCount = Nothing
    Msgbox "Operation Complete"
</SCRIPT>

NotWork.hta

 <JOB ID="CSVPad">

<HEAD>
    <TITLE>Support Toolbox</TITLE>
    <HTA:APPLICATION 
     ID="Test"
     APPLICATIONNAME="Test"
     BORDER="thin"
     CONTEXTMENU="no"
     MAXIMIZEBUTTON="no"
     MINIMIZEBUTTON="yes"
     RESIZE="no"
     SCROLL="no"
     SHOWINTASKBAR="yes"
     SINGLEINSTANCE="yes"
     WINDOWSTATE="normal"
     VERSION="1.0"
     NAVIGABLE="yes"
    />

    <SCRIPT LANGUAGE="VBScript">    

        Sub padCSV
            If isNumeric(csvPadNo.value) Then
                Dim delim, colNum
                If Delimeter(0).Checked Then
                  delim = "1"
                End If
                If Delimeter(1).Checked Then
                  delim = "2"
                End If
                If Delimeter(2).Checked Then
                  delim = "3"
                End If
                'Set colNum based on the number of columns to pad out to
                colNum = csvPadNo.value
                ''Get location of .csv file
                'set the type of dialog box you want to use: 1 = Open 2 = SaveAs 3 = File Picker 4 = Folder Picker
                Const msoFileDialogOpen = 1
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set objWord = CreateObject("Word.Application")
                Set WshShell = CreateObject("WScript.Shell")
                'Launch at default path
                strInitialPath = WshShell.ExpandEnvironmentStrings("V:\Toolbox\Test")
                objWord.ChangeFileOpEndirectory(strInitialPath)

                With objWord.FileDialog(msoFileDialogOpen)
                    .Title = "Select the file to process"
                    .AllowMultiSelect = False
                    .Filters.Clear
                    .Filters.Add "CSV Files / TXT Files", "*.csv; *.txt"
                    .Filters.Add "All Files", "*.*"              
                    If .Show = -1 Then 
                        For Each File in .SelectedItems 
                        Set objFile = fso.GetFile(File) 
                        Next 
                    End If
                End With
                objWord.Quit

                Dim oFso, oReg, objInputFile, objOutputFile, sLine, lCount
                Const ForReading = 1
                Set oReg = New RegExp
                Set oFso = CreateObject("Scripting.FileSystemObject")
                Set objInputFile = oFso.OpenTextFile(objFile.Path,1)
                Set objOutputFile = oFso.OpenTextFile(objFile.Path&".bak",8,True)
                Do Until objInputFile.AtEndOfStream
                    sLine = objInputFile.ReadLine
                    oReg.Global = True
                    If delim = 1 Then
                        oReg.Pattern = ",(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
                    ElseIf delim = 2 Then
                        oReg.Pattern = "\|(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
                    ElseIf delim = 3 Then
                        oReg.Pattern = "\^(?=(([^""\\]|\\.)*""([^""\\]|\\.)*"")*([^""\\]|\\.)*$)"
                    End If
                    lCount = oReg.Execute(sLine).Count + 1
                    Do While lCount < colNum
                        If delim = 1 Then
                            sLine = sLine + ","
                        ElseIf delim = 2 Then
                            sLine = sLine + "|"
                        ElseIf delim = 3 Then
                            sLine = sLine + "^"
                        End If
                        lCount = oReg.Execute(sLine).Count + 1
                    Loop
                    objOutputFile.WriteLine(sLine)
                    Set lCount = Nothing
                Loop
                objInputFile.Close
                objOutputFile.Close
                Set oFso = Nothing
                Set oReg = Nothing
                Set delim = Nothing
                Set sLine = Nothing
                Set lCount = Nothing
                Msgbox "Operation Complete"
            Else
                MsgBox "Please enter a number" 
            End If
        End Sub

    </SCRIPT>
</HEAD>

<BODY>
    <span id="CSVPad">
        <div id="PageCSVPad" class="PageCSVPad">
            <table class="CSVPad" Border="0" Cellspacing="0" Align="Left">
                <tr>
                    <td width=25%><b>Delimiter Type: </b></td>
                    <td><input type="radio" name="Delimeter" value="1" checked>Comma</td>
                    <td><input type="radio" name="Delimeter" value="2">Pipe</td>
                    <td><input type="radio" name="Delimeter" value="3">Caret</td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2"><b>Number of Columns: </b></td>
                    <td><input type="text" name="csvPadNo"></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td><input type="button" value="Browse" onclick="padCSV" name="Radb"><br></tr>
                </tr>
            </table>
        </div>
    </span>
</BODY>

The code is only changed slightly to allow input of values, but to me it should work just the same.

Am I missing something?

Thank's guys!

Was it helpful?

Solution

In line 78 of the HTA:

Do While lCount < colNum

you're not comparing what you think you're comparing. Insert the following line just before the above line and you'll see what's different:

MsgBox "lCount: " & TypeName(lCount) & vbNewLine & "colNum: " & TypeName(colNum)

As for resolving the issue, change this line:

colNum = csvPadNo.value

into this:

colNum = CInt(csvPadNo.value)

and the problem will disappear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top