2014-02-24 45 views
1

我有數以百計的文件名爲csv的文件,格式如下:yyyymmdd_something.csv,例如: 20131213_something.csv ...即,下劃線將日期與文件名的其餘部分分開。將文件名的一部分插入csv的(第一個)列中

每個具有下列字段: ,型號,度量1,度量2等

我想:

1)插入從文件名中的日期到(優選第一)柱,就像這樣:

日期,品牌,型號,指標1,指標2等

但實際上它可以是任何列,因爲在Excel中一次,這是一個簡單的事情,重新安排一下。日期應該以mm/dd/yyyy的形式添加,因爲Excel是可以理解的。

2)插入日期後,我想將所有的csv合併到一個大的csv文件中。

我使用的是Win7的機器這麼一個DOS批處理文件很可能是運行對我來說最簡單的方式,但如果要我,我可以安裝Perl或東西做到這一點。任何幫助將深表感謝。有一個討論插入整個文件名的線程,但我真的只需要添加日期部分。

+0

個人**。** CSV是否有大量的記錄或只有一個記錄? –

+0

每個csv都有很多記錄,通常> 100。 – user3348706

回答

1

嗨,這可以通過VBScript中來完成,代碼如下:

創建一個VBS文件(記事本可以做到這一點,只需粘貼以下代碼並保存文件.VBS)

可以下降20 -50文件(yyyymmdd_something.csv)作爲同時該VBS文件的圖標,它會處理你的願望:)

請創建Summary.csv文件第一和更新到pSumaryCSV其完整路徑=「 ...... \ Summary.csv「

'USAGE: 
'CREATE A VBSCRIPT FILE .VBS WITH THIS CONTENT 
'CREATE SUMMARY CSV FILE AND UPDATE ITS FULL PATH IN pSumaryCSV 
'DRAG AND DROP YOUR ORIGINAL CSV FILE TO THIS VBS FILE ICON, IT CAN PROCESS MULTIPLE FILE (BUT DON'T PUT TOO MANY AS ONE) 
'THIS CODE WILL CREATE A NEW CSV FILE <ORIGINAL FILE NAME>_DATE_ADDED.csv 
'AND UPDATE Summary.csv file. 


Set objArgs = WScript.Arguments 

Set objFso = createobject("scripting.filesystemobject") 

dim objOrgFile 
dim arrStr ' an array to hold the text content 
dim sLine ' holding text to write to new file 

'Location of the summary file - Full path. If it is not exist then create it first. 
'The summary one should have all column lable since following code will not add label to it. 
pSumaryCSV = "......\Summary.csv" 

'Open the summary file to append data 
    set aSummaryFile = objFso.OpenTextFile(pSumaryCSV, 8) '2=Open for writing 8 for appending 


'Looping through all dropped file 
For t = 0 to objArgs.Count - 1 
    ' Input Path 
    inPath = objFso.GetFile(wscript.arguments.item(t)) 
    inName = objFso.GetFileName(inPath) 

    ' OutPut Path 
    outPath = replace(inPath, objFso.GetFileName(inPath), left(inName, InStrRev(objFso.GetFileName(inPath),".") - 1) & "_DATE_ADDED.csv") 

    ' The original file 
    set objOrgFile = objFso.OpenTextFile(inPath) 


    'Now Creating the file can overwrite exiting file with same name 
    set aNewFile = objFso.CreateTextFile(outPath, True) 
    aNewFile.Close 


    'Open the new file (...._DATE_ADDED.csv) to appending data 
    set aNewFile = objFso.OpenTextFile(outPath, 8) '2=Open for writing 8 for appending 



    '======================================================================= 
    'Process first line, this firstline will not be added to SummaryCSV File 
    If Not objOrgFile.AtEndOfStream Then 
    arrStr = split(objOrgFile.ReadLine,",") 
    sLine = "Date,"    'This will add Date label for 

    For i=lbound(arrStr) to ubound(arrStr) 
      sLine = sLine + arrStr(i) + "," 
     Next 

    'Writing first line to new file but not the summary one. 
     aNewFile.WriteLine left(sLine, len(sLine)-1) 'Get rid of that extra comma from the loop 
    end if 

    '======================================================================= 

    ' Reading subsequent line and writing it to new file 
    Do Until objOrgFile.AtEndOfStream 
     arrStr = split(objOrgFile.ReadLine,",") 

    'Get the mm/dd/yyyy path from file name yyyymmdd 
    sLine = "" 
    sLine = sLine + Mid(inName,5,2) + "/" 'Get mm from file name 
    sLine = sLine + Mid(inName,7,2) + "/" 'Get dd from file name 
    sLine = sLine + Mid(inName,1,4) + "/" 'Get yyyy from file name 
    sLine = Sline + ","   'This will add a column 

     For i=lbound(arrStr) to ubound(arrStr) 
      sLine = sLine + arrStr(i) + "," 
     Next 

     'Writing data to new file 
     aNewFile.WriteLine left(sLine, len(sLine)-1) 'Get rid of that extra comma from the loop 

    'Writing data to summary file 
    aSummaryFile.WriteLine left(sLine, len(sLine)-1) 

    Loop 

    'Closing new file 
    aNewFile.Close 

Next ' This is for next file 

'Close Summary File 
aSummaryFile.Close 

set aSummaryFile=nothing 
set aNewFile=nothing 
set objFso = nothing 
set objArgs = nothing 
+0

謝謝,這工作!我編輯了代碼以在sLine中添加「/」,以便excel識別日期。 Excel不會識別mmddyyyy,只有mm/dd/yyyy。再次感謝。 – user3348706

相關問題