我有数百个带有文件名的csv文件,格式如下:yyyymmdd_something.csv,例如: 20131213_something.csv ...即,下划线将日期与文件名的其余部分分开。
每个都有以下字段: 制作,模型,指标1,指标2等
我想:
1)将文件名中的日期插入(最好是第一列)列,如下所示:
日期,品牌,型号,指标1,指标2等
但实际上它可以是任何专栏,因为一旦在Excel中,重新安排它是一件简单的事情。日期应该添加为mm / dd / yyyy,因为它是Excel理解的。
2)插入日期后,我想将所有csv合并为1个大csv文件。
我正在使用Win7机器,因此dos批处理文件可能是为我运行它的最简单方法,但如果必须,我可以安装perl或其他东西来做这件事。任何帮助将深表感谢。有一个线程讨论插入整个文件名,但我真的只需要添加日期部分。
答案 0 :(得分: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