将文件名的一部分插入到csv的(第一个)列中

我有以下格式的文件名数以百计的csv文件:yyyymmdd_something.csv,例如20131213_something.csv …即下划线将date从文件名的其余部分分开。

每个都有以下字段:品牌,型号,指标1,指标2等

我想要:

1)将文件名中的date插入(最好是第一个)列,如下所示:

date,品牌,型号,公制1,公制2等

但实际上它可以是任何列,因为一旦在Excel中,重新排列它是一件简单的事情。 date应该被添加为mm / dd / yyyy,因为它是Excel可以理解的。

2)插入date后,我想将所有的csv合并成一个大的csv文件。

我使用的是Win7机器,所以一个DOSbatch file可能是最简单的方式来运行它,但如果我不得不,我可以安装Perl或其他东西来做到这一点。 任何帮助将深表谢意。 有一个线程谈到插入整个文件名,但我真的只需要添加date部分。

嗨,这可以通过VBScript来完成,代码如下:

创build一个VBS文件(记事本可以做到这一点,只需粘贴以下代码并将文件另存为.vbs)

你可以同时把20-50个文件(yyyymmdd_something.csv)放到这个vbs文件的图标上,它会按你的意愿处理:)

请先创buildSummary.csv文件,并将其完整path更新为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