InterviewSolution
| 1. |
Solve : need script (*.cvs file items export to other files) [solved]? |
|
Answer» Hi there, More questions than answers for now. Are these HTML templated files being created from scratch, or do they exist and you want to insert records? Do all the records in the csv file have have same number of data fields? How many?Not as elegant as I hoped, but serviceable: Code: [Select]Const ForReading = 1 Const ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") Set fs = fso.OpenTextFile("c:\temp\data.csv", ForReading) 'user change Set fldr = fso.GetFolder("c:\temp") 'user change Set fc = fldr.Files For Each fi In fc If fso.GetExtensionName(fi) = "html" Then Set f = fso.OpenTextFile(fi, ForReading) strContent = f.ReadAll f.Close Set f = fso.OpenTextFile(fi, ForWriting) strData = fs.ReadLine arrData = Split(strData, ",") loc = InStr(1, strContent, "Best Regards,") loc = InStr(loc, strContent, vbCrLf) f.WriteLine Mid(strContent, 1, loc) For n = 0 To UBound(arrData) If n = 0 Then f.WriteLine arrData(n) & " " & arrData(n+1) n = 1 Else f.WriteLine arrData(n) End if Next f.WriteLine Mid(strContent, loc + 2) f.Close End if Next fs.Close I flagged the lines where you need to make changes for your environment. Made some assumptions: The templates are in a single directory with a html extension and missing data in the CSV file creates a blank line in the template. This could have been done directly from the spreadsheet, but since you went to the effort of creating a CSV file, I used it. The script overwrites the original template files. Can be modified to create a new output file. Save script with a vbs extension and run from the command line as cscript scriptname.vbs There is a shameful line of code where the array index inside a loop is tampered with. Keep this to yourself. If it gets out I may be banned from Don's Bar & Grill. Big thanks helping me, but script don`t work When I run the script I get this: Code: [Select]C:\temp\run.vbs(13, 3) Microsoft VBScript runtime error: Input past end of fileDid you test it or maybe I`m doing something wrong though I did as you said hmm...Quote Did you test it Yes, it was tested. All posted snippets are tested unless otherwise indicated. I cannot duplicate the error. Quote C:\temp\run.vbs(13, 3) Microsoft VBScript runtime error: Input past end of file Error can be caused by a file being open in another process or the file is empty. Judging from the location in the code, most likely a template file. The solution was based on each template file containing: Quote <html> Post the code you are ACTUALLY using. You'd be surprised at how much gets lost in translation. Yes. You was right. My template html file was empty. (my mistake) Now script works. In fact I`ve just realized that I need some changes to VB script, because my real html template file should look like this: Code: [Select]<html> ... Best Regards, <b>"name surname from csv file"</b><br /> "position from csv file"<br /> phone:+777 "number from csv file"<br /> mobile:+777 "number from csv file"<br /> fax:+777 "number from csv file"<br /> ... </html> As you can see name and surname for example should go exactly between tags, etc. Is it possible? Can you help me with it? And of course in the very perfect and comfortable way there is only one html template and before changes to it script creates the copy of it, then changes 1st one taking info from csv file line 1, then again creates 3rd copy from the 2nd, than again script changes 2nd one taking info from csv file line 2 and so on... And each TIME html file is named by Name Surname values which it contains. Of course there is no problem for me to copy many templates as I need and numerate them Quote In fact I`ve just realized that I need some changes to VB script, because my real html template file should look like this: In the future, take a deep breath, think about what you need and then post your request. Posting code on the installment plan is not very efficient. Code: [Select]Const ForReading = 1 Const ForWriting = 2 Set fso = CreateObject("Scripting.FileSystemObject") Set fs = fso.OpenTextFile("c:\temp\data.csv", ForReading) Set f = fso.OpenTextFile("c:\temp\template.html", ForReading) strContent = f.ReadAll loc = InStr(1, strContent, "Best Regards,") loc = InStr(loc, strContent, vbCrLf) f.Close Do Until fs.AtEndOfStream strData = fs.ReadLine arrData = Split(strData, ",") Set fo = fso.OpenTextFile("c:\temp\" & arrData(0) & " " & arrData(1) & ".html", ForWriting, True) ' user change fo.WriteLine Mid(strContent, 1, loc) For n = 0 To UBound(arrData) Select Case n Case 0 fo.WriteLine "<br>" & arrData(n) & " " & arrData(n+1) & "</b><br />" n = 1 Case 2 fo.WriteLine arrdata(n) & "<br />" Case 3 fo.WriteLine "phone:+777 " & arrdata(n) & "<br />" Case 4 fo.WriteLine "mobile:+777 " & arrdata(n) & "<br />" Case 5 fo.WriteLine "fax:+777 " & arrData(n) & "<br />" Case Else fo.WriteLine arrData(n) End Select Next fo.WriteLine Mid(strContent, loc + 2) fo.Close Loop fs.Close Made some assumptions: 1. Template file is labeled template.html 2. CSV file is labeled data.csv 3. New templates are labeled firstname lastname taken from the CSV file 4. Uses a single template file (format in earlier post) as the basis of the new templates You can change any of these values. Missing data in the CSV file produces a template line with the literal only. Run as instructed in an earlier post. Good luck. Quote In the future, take a deep breath, think about what you need and then post your request I will. Thank you very very much. You really helped me. The script is perfect - exactly what I was looking for. |
|