|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
[newbie] Need a script to creat a .csv file
I'm using an OCR software to output a PDF and TXT file from the same source file. The PDF and TXT files will have the same filename and will be stored in the same folder directory. I need a script that will look at this folder directory, find the PDF and TXT files with the same filename, and create a CSV file that has the following information
Index1,Document.PDF,Document.TXT This is needed to associate the 2 files with each other so they can be imported into our Document Mangement System using their proprietary software. I can clarify if this sounds a little confusing, and I would appreciate anyone that could point me in the right direction. Thanks, Jason |
|
#2
|
||||
|
||||
|
Although CSV files (much like XML files) are actually simple database formats, they are also nothing more than formatted text files. From a scripting perspective, this presents two way of creating/editing them.
The first approach is to edit them as simple text files using the FileSystemObject. You can simply write properly formatted strings to a flat file and save it when finished. This is the preferred option for creating new CSVs from scratch or simply appending new data. The second option is to connect to the file as a database using ADODB. This provides a lot more power and flexibility but results is overly complicated code in some cases. This is the preferred method of editing existing files. My question to you becomes, which one would you like to see?
__________________
Scripting problems? Windows questions? Ask the Windows Guru! Stay up to date with all of my latest content. Follow me on Twitter! Help us help you! Post your exact error message with these easy tips! |
|
#3
|
|||
|
|||
|
Thanks for the quick reply....
I would like to keep the code simple and I would only be creating new CSV files, not modifying existing ones. So the first method would be perfect. Thanks, Jason |
|
#4
|
|||||
|
|||||
|
Very good. Give this a try:
vb Code:
|
|
#5
|
|||
|
|||
|
I tested the script this morning and its pretty close to what I need. I created the following directory and placed 2 PDF files (TEST1.pdf & TEST2.pdf) and 2 TXT files (TEST1.txt & TEST2.txt) within it. I then specified this directory in the script you supplied.
C:\OCRTEST After running the script, this is how the CSV file appeared when opened in Notepad: C:\OCRTEST\TEST1.pdfTEST1.pdf , C:\OCRTEST\TEST1.pdfTEST1.txt C:\OCRTEST\TEST2.pdfTEST2.pdf , C:\OCRTEST\TEST2.pdfTEST2.txt Is there any way to have it listed as below? Index1,C:\OCRTEST\TEST1.PDF,C:\OCRTEST\TEST1.TXT Index2,C:\OCRTEST\TEST2.PDF,C:\OCRTEST\TEST2.TXT Thanks again for all your help. This has been extremely helpful so far! |
|
#6
|
|||
|
|||
|
Anybody?
|
|
#7
|
||||
|
||||
|
If looks as if the variable strPath contains the path and filename, so you might not need to add the filename again. You should be able to add a counter and pass this value to the function, try something like this, not tested:
Code:
strFileName = "myfile.csv"
strDirectory = "C:\somedirectory\"
' Create CSV file
Const ForWriting = 2
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objCSV = objFso.OpenTextFile(strFileName, ForWriting, True)
' Iterate through PDF files in directory
Set objFolder = objFso.GetFolder(strDirectory)
Set colFiles = objFolder.Files
Dim tempCounter
tempCounter = 1
For Each objFile In colFiles
If InStr(objFile.Name, ".pdf") > 0 Then
AddEntry objFile.Path, objFile.Name, tempCounter
End If
tempCounter = tempCounter + 1
Next
objCSV.Close
Sub AddEntry(strPath, strPdf, intCount)
objCSV.WriteLine Chr(32) & "Index" & CStr(intCount) & "," & UCase(strPath) & Chr(32) & "," & Chr(32) & UCase(Left(strPath, Len(strPath)-3)) & "TXT" & Chr(32)
End Sub
|
|
#8
|
||||
|
||||
|
I'm sorry, that's my mistake. Change all of the Chr(32)'s to Chr(34). That will fix it.
|
|
#9
|
|||||
|
|||||
|
Sorry again, I completely overlooked the file name problem. Ignore my last post. Here's the complete corrected code that fixes both issues.
vb Code:
|
|
#10
|
|||
|
|||
|
The script works perfectly. This will really help us save time. Thanks again for all your help Nilpo!
|
|
#11
|
||||
|
||||
|
Excellent. And you are very welcome.
|
![]() |
| Viewing: ASP Free Forums > System Administration > Windows Scripting > [newbie] Need a script to creat a .csv file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|