我有一个简单的 VBScript,它“填充”了一个 .csv 文件以具有特定数量的列(当 .csv 文件没有统一的列数时很有用)。我的 .wsf 文件运行良好,但是当我将其合并到 .hta 文件中作为工具箱的一部分时,它失败了。
我不是在寻求有关代码的帮助,而只是解释为什么它在一个文件而不是另一个文件中失败。对不起,糟糕的代码,但有人有想法吗?
作品.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> </td>
</tr>
<tr>
<td colspan="2"><b>Number of Columns: </b></td>
<td><input type="text" name="csvPadNo"></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><input type="button" value="Browse" onclick="padCSV" name="Radb"><br></tr>
</tr>
</table>
</div>
</span>
</BODY>
代码只是略微更改以允许输入值,但对我来说它应该工作相同。
我错过了什么吗?
多谢你们!