2015-01-10 65 views
0

我有一個每天生成3個文本(.txt)文件的系統,每個文件中都有1000個條目。在文本文件中拆分列

一旦生成文本文件,我們運行一個vbscript(下),通過在特定列位置輸入數據來修改文件。

我現在需要這個vbscript來做一個額外的任務,就是在一個文本文件中分隔一列。

因此,例如TR201501554s.txt文件看起來像這樣:

6876786786 GFS8978976  I 
6786786767 DDF78676   I 
4343245443 SBSSK67676  I 
8393372263 SBSSK56565  I 
6545434347 DDF7878333  I 
6757650000 SBSSK453   I 

隨着分離該列的額外任務,數據現在這個樣子,與在特定位置分隔列。

6876786786 GFS 8978976  I 
6786786767 DDF 78676  I 
4343245443 SBSSK 67676  I 
8393372263 SBSSK 56565  I 
6545434347 DDF 7878333  I 
6757650000 SBSSK 453   I 

我想也許我可以添加其他「的情況下」有可能使用「正則表達式」模式來做到這一點,因爲這個模式將只有3家公司找到 (DDF,GFS和SBSSK)。

但看了很多例子後,我不知道從哪裏開始。

有人能讓我知道如何完成這個額外的任務在我們的VBScript(如下)?

Option Explicit 
Const ForReading = 1 
Const ForWriting = 2 


Dim objFSO, pFolder, cFile, objWFSO, objFileInput, objFileOutput,strLine 
Dim strInputPath, strOutputPath , sName, sExtension 
Dim strSourceFileComplete, strTargetFileComplete, objSourceFile, objTargetFile 
Dim iPos, rChar 
Dim fileMatch 


'folder paths 
strInputPath = "C:\Scripts\Test" 
strOutputPath = "C:\Scripts\Test" 

'Create the filesystem object 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Get a reference to the processing folder 
Set pFolder = objFSO.GetFolder(strInputPath) 

'loop through the folder and get the file names to be processed 
For Each cFile In pFolder.Files 
ProcessAFile cFile 
Next 

Sub ProcessAFile(objFile) 
fileMatch = false 

Select Case Left(objFile.Name,2) 
    Case "MV" 
     iPos = 257 
     rChar = "YES" 
     fileMatch = true 
    Case "CA" 
     iPos = 45 
     rChar = "OCCUPIED" 
     fileMatch = true 
    Case "TR" 
     iPos = 162 
     rChar = "EUR" 
     fileMatch = true 
End Select 

If fileMatch = true Then 

    Set objWFSO = CreateObject("Scripting.FileSystemObject") 
    Set objFileInput = objWFSO.OpenTextFile(objFile.Path, ForReading) 
    strSourceFileComplete = objFile.Path 
    sExtension = objWFSO.GetExtensionName(objFile.Name) 
    sName = Replace(objFile.Name, "." & sExtension, "") 

    strTargetFileComplete = strOutputPath & "\" & sName & "_mod." & sExtension 
    Set objFileOutput = objFSO.OpenTextFile(strTargetFileComplete, ForWriting, True) 

     Do While Not objFileInput.AtEndOfStream 
     strLine = objFileInput.ReadLine 
     If Len(strLine) >= iPos Then 
      objFileOutput.WriteLine(Left(strLine,iPos-1) & rChar) 
     End If 

    Loop 
    objFileInput.Close 
    objFileOutput.Close 
    Set objFileInput = Nothing 
    Set objFileOutput = Nothing 

    Set objSourceFile = objWFSO.GetFile(strSourceFileComplete) 
    objSourceFile.Delete 
    Set objSourceFile = Nothing 

    Set objTargetFile = objWFSO.GetFile(strTargetFileComplete) 
    objTargetFile.Move strSourceFileComplete  
    Set objTargetFile = Nothing 
    Set objWFSO = Nothing 
End If 
End Sub 
+0

我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

+0

您發佈的代碼的確切問題是什麼? –

回答

0

你可以添加一個regular expressionreplacement您輸入處理循環。既然你想重新格式化列,我會用replacement function來做。同時定義正則表達式,並在全球範圍內的功能:

... 
Set pFolder = objFSO.GetFolder(strInputPath) 

Set re = New RegExp re.Pattern = " ([A-Z]+)(\d+)(+)" Function ReFormatCol(m, g1, g2, g3, p, s) ReFormatCol = Left(" " & Left(g1 & " ", 7) & g2 & g3, Len(m)+2) End Function 

'loop through the folder and get the file names to be processed 
For Each cFile In pFolder.Files 
...

和修改輸入處理循環是這樣的:

... 
Do While Not objFileInput.AtEndOfStream 
    strLine = re.Replace(objFileInput.ReadLine, GetRef("ReFormatCol")) 
    If Len(strLine) >= iPos Then 
    objFileOutput.WriteLine(Left(strLine,iPos-1) & rChar) 
    End If 
Loop 
...

請注意,您可能需要改變你的iPos值,因爲分裂和重新設置列的格式會將行的長度增加2個字符。

回調函數ReFormatCol具有以下(必需)參數:

  • m:正則表達式的匹配(用於確定匹配的長度)
  • g1g2g3:所述來自表達式的三組
  • p:匹配在源字符串中的起始位置(但在此處未使用)
  • s:源串(但不是在這裏使用的)

的函數構造的比賽從3組這樣的置換:

  • Left(g1 & " ", 7)追加4位於所述第一基團(例如GFS)並將其修剪爲7個字符。這是基於第一組總是3-5個字符的假設。
    GFS    
  • " " & ... & g2 & g3預先考慮用2位以上操作的結果和附加其他2組(8978976 &         )。
      GFS    8978976        
  • Left(..., Len(m)+2)然後修剪結果字符串到原來的匹配的長度加上2個字符(以考慮附加的2位插入到新的第二列從前者第二分開,現在第三,列)。
      GFS    8978976      
+0

感謝您的回覆。你分享,工作,但還沒有。我需要了解該功能的工作原理。你能告訴我ReformatCol = Left(「.....什麼代碼在做什麼? – Andrea

+0

@Andrea查看更新的答案,更多詳細信息請參閱我引用的文檔 –

+0

非常感謝你。 – Andrea

0

在第一替代由正則表達式模式(\d+)\s+([A-Z]+)(\d+)\s+(\w+)$1 $2 $3 $4

替換和分裂,通過+。然後好的。

Live demo