【问题标题】:insert part of file name into (first) column of csv将部分文件名插入 csv 的(第一)列
【发布时间】:2014-09-01 14:51:59
【问题描述】:

我有数百个文件名格式如下的 csv 文件:yyyymmdd_something.csv,例如20131213_something.csv ...即,下划线将日期与文件名的其余部分分开。

每个都有以下字段: 品牌、型号、公制 1、公制 2 等。

我想:

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

日期、品牌、型号、公制 1、公制 2 等

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

2) 插入日期后,我想将所有 csv 合并为 1 个大 csv 文件。

我使用的是 Win7 机器,因此 dos 批处理文件可能是运行它的最简单方法,但如果必须,我可以安装 perl 或其他工具来执行此操作。任何帮助将不胜感激。有一个帖子谈到插入整个文件名,但我真的只需要添加的日期部分。

【问题讨论】:

  • 个人 .csv 有很多记录还是只有一条记录??
  • 每个csv有很多记录,通常>100。

标签: excel csv


【解决方案1】:

你好,这可以通过vbscript来完成,代码如下:

创建一个VBS文件(记事本可以,只需粘贴以下代码并将文件保存为.vbs)

您可以同时将 20-50 个文件 (yyyymmdd_something.csv) 拖放到此 vbs 文件的图标上,它会按照您的意愿处理:)

请先创建 Summary.csv 文件并将其完整路径更新为 pSummaryCSV = "......\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

【讨论】:

  • 谢谢,成功了!我编辑了代码以在 sLine 中添加一个“/”,以便 excel 识别日期。 Excel 无法识别 mmddyyyy,只能识别 mm/dd/yyyy。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2015-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多