我想在我想要加载到数据库中的文本文件中读取固定长度记录。
我从
开始$Content = Get-Content –Path C:\ps_test_in\*\ -Filter *.txt.
我应该将每个字段移动到循环遍历内容的数组或数组列表中 然后遍历数组写入数据库?
还是第一次写入数据库循环?
这是一个示例输入行。
ZASC 01 4644646464646566606464464460000000000030090000000010000000000183434200000000000000000000Bruce Bear 111 ST IA 4
有数千条记录可供阅读。
$FileName = Get-ChildItem –Path C:\ps_test_in -Filter *.txt
$Content = Get-Content –Path C:\ps_test_in\*\ -Filter *.txt
Write-Host "________________________________________________________"
for ($i=0; $i -lt $FileName.Count; $i++){
Write-Host $FileName[$i]
for ($j=0; $j -lt $Content.Count; $j++){
$Type = $Content[$j].substring(0,1)
$CustomerName = $Content[$j].substring(12,20)
if ($CustomerName.Trim().Length -ne 0){
if ($Type -eq "z") {
<# Split Fields Off of Input Files #>
$AccountID = $Content[$j].substring(22,11)
$stuff1 = $Content[$j].substring()
$JobName = $Content[$j].substring()
$JobCode = $Content[$j].substring()
$JobDD = $Content[$j].substring()
$CustomerName = $Content[$j].substring()
$Address01 = $Content[$j].substring()
$Address02 = $Content[$j].substring()
$Address03 = $Content[$j].substring()
$Address04 = $Content[$j].substring()
$CityStateZip = $Content[$j].substring()
$RecNum = $Content[$j].substring()
#Write Records to Database
$insert_stmt =
"INSERT INTO FD_MRDF(
AcctNumber,
stuff1,
JobName,
JobNumber,
OutputGrp,
Name,
Address01,Address02,Address03,Address04,
CityStateZip,
RecordNumber
)
VALUES (
'$AccountID',
'$stuff1',
'$JobName',
'$JobCode',
'$JobDD',
'$CustomerName',
'$Address01','$Address02','$Address03','$Address04',
'$CityStateZip',
'$RecNum'
)"
$cmd = $conn.CreateCommand()
$cmd.CommandText = $insert_stmt
$cmd.ExecuteNonQuery()
}
}
}
Write-Host "________________________________________________________"
}
Write-Host $Content.Count
$conn.Close()