嗨,這可以通過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
個人**。** CSV是否有大量的記錄或只有一個記錄? –
每個csv都有很多記錄,通常> 100。 – user3348706