2014-02-24 4 views
1

파일 이름이 수백 개이고 파일 이름이 yyyymmdd_something.csv입니다. 예 : 20131213_something.csv ... 즉 밑줄은 파일 이름의 나머지 부분과 날짜를 구분합니다.파일 이름의 일부를 csv의 (첫 번째) 열에 삽입합니다.

다음 필드가 각각 : , 모델, 측정 1, 측정 2 등

내가하고 싶은 :

1)에 파일 이름과 날짜를 삽입 (바람직하게는 첫 번째 그래서 같은) 열 :

날짜, 메이크업, 모델, 측정 1, 측정 2 등

하지만 정말 때문에 한 번 Excel에서 모든 열을 할 수 있습니다,을 다시 준비하는 간단한 문제입니다 . 날짜는 mm/dd/yyyy로 Excel이 이해하는대로 추가해야합니다.

2) 날짜를 삽입 한 후 모든 csv를 1 개의 큰 csv 파일로 병합하고 싶습니다.

이 작업을 수행하기 위해 펄이나 뭐 내가 그렇게 도스 배치 파일은 아마 나를 위해 그것을 실행할 수있는 간단한 방법이 될 것 Win7에 기계를 사용하고 있습니다하지만 난에 있었다면, 내가 설치할 수 있습니다. 어떤 도움을 주시면 감사하겠습니다. 전체 파일 이름을 삽입하는 것에 대해 이야기하는 스레드가 있었지만 실제로는 날짜 부분 만 추가하면됩니다.

+0

개인 **. CSV **는 기록을 많이 또는 단 하나의 기록을 가지고 있습니까? –

+0

각 CSV 레코드의 많은 보통> (100)이있다. – user3348706

답변

1

안녕이이 VBScript를 수행 할 수있는이 코드는 다음과 같습니다 :

는 VBS 파일을 만듭니다 (메모장, 그것을 단지 코드를 다음 붙여 .VBS 등의 파일을 저장할 수 있습니다)

당신은 20을 드롭 할 수 있습니다 다음은이 VBS 파일의 아이콘이 "당신이 = :

가 Summary.csv 먼저 파일 및 pSumaryCSV에 전체 경로를 업데이트 만드십시오 원하는대로 처리 할에 동시에 -50 파일 (yyyymmdd_something.csv) ...... \ 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은 mmddyyyy를 인식하지 못하지만 mm/dd/yyyy 만 인식합니다. 다시 한번 감사드립니다. – user3348706

관련 문제