需要附加一个CSV文件来添加一列(Pkey),其每行数据的增量数字都从1开始。我更喜欢使用VB脚本来执行此操作,但是如果有一种更简单的方法,我可以在Windows Task Scheduler上运行,那么我将无所不能。
我只需要一个脚本,即可在CSV中创建一个名为“ Pkey”的列,并向每行数据(从1开始)添加一个递增的数字+1。这必须是唯一的,一直到最后一行数据的。
期望在列中添加标题Pkey,然后对数据1,2,3,4,5等行进行编号,直到所有数据行都已使用唯一的整数编号
我当前用于将查询拉入CSV并保存它的代码,每次我尝试执行增量操作时都会破坏脚本:
Option Explicit
Dim TextExportPath
Dim TextExportFile
Dim db
Dim cn
Dim strCon
Dim objFileSys
db = "X:\VPP DATABASE\Pham.mdb"
TextExportPath = "X:\Scripts\FTP\QuickBase Sync\CustomerData\"
TextExportFile = NewFileName(TextExportPath)
Set objFileSys = CreateObject("Scripting.FileSystemObject")
If objFileSys.FileExists("X:\Scripts\FTP\QuickBase Sync\CustomerData\MMD_Abacus.csv") Then
objFileSys.DeleteFile "X:\Scripts\FTP\QuickBase Sync\CustomerData\MMD_Abacus.csv"
End If
Set cn = CreateObject("ADODB.Connection")
cn.Open _
"Provider = Microsoft.Jet.OLEDB.4.0; " & _
"Data Source =" & db
cn.Execute "SELECT * INTO [text;HDR=Yes;Database=" & TextExportPath & _
"]." & TextExportFile & " FROM 83AFFNIGHT"
Function NewFileName(TextExportPath)
Dim fs
Dim NewFileTemp
Dim a, i
Set fs = CreateObject("Scripting.FileSystemObject")
NewFileTemp = "MMD_Abacus" & ".csv"
a = fs.FileExists(TextExportPath & NewFileTemp)
NewFileName = NewFileTemp
End Function
所以我会添加这样的东西,或者甚至创建一个单独的vb脚本在此之后运行吗?
Dim lines As New List(Of String)
Dim lineNumber = 1
Using parser As New TextFieldParser("TextFile1.txt") With {.Delimiters = {","}}
Do Until parser.EndOfData
Dim fields = parser.ReadFields()
'Add quotes around the column that may be multiline.
fields(1) = $"""{fields(1)}"""
lines.Add(String.Join(",", fields) & "," & lineNumber)
lineNumber += 1
Loop
End Using
File.WriteAllLines("TextFile1.txt", lines)
答案 0 :(得分:0)
使用StdIn和StdOut意味着我们不必编写文件处理代码。
从1开始
Count = 1
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Do Until Inp.AtEndOfStream
Line=Inp.readline
outp.writeline Count & vbtab & Line
Count = Count + 1
Loop
要使用
cscript //nologo MyScript.vbs < InputFile > Outputfile
答案 1 :(得分:0)
...如果有更简单的方法,我可以在Windows任务计划程序上运行,则可以打开任何东西。
让我们做PowerShell!
确保为此使用the 32-bit powershell:
function Get-MSAccessTable {
Param(
[parameter(Mandatory=$true)] $filePath,
[parameter(Mandatory=$true)] $tableName
)
$conn = New-Object System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=$filePath;Persist Security Info=False")
$cmd=$conn.CreateCommand()
$cmd.CommandText="Select * from $tableName"
$conn.open()
$rdr = $cmd.ExecuteReader()
$dt = New-Object System.Data.Datatable
$dt.Load($rdr)
$conn.close()
$dt
}
$TextExportPath = "X:\Scripts\FTP\QuickBase Sync\CustomerData"
$TextExportFile = "$TextExportPath\MMD_Abacus.csv"
$db = "X:\VPP DATABASE\Pham.mdb"
$data = Get-MSAccessTable -filePath $db -tableName "83AFFNIGHT"
$Global:seq = 1;
$data = $data | Select-Object <LIST COLUMNS HERE>, @{label = “PKey”; Expression = {$Global:seq;$Global:seq++ }}
Remove-Item -Path "X:\Scripts\FTP\QuickBase Sync\CustomerData\MMD_Abacus.csv"
$data | Export-Csv -Path $TextExportFile -NoTypeInformation