June 22, 2004 at 2:46 pm
Trying to loop through all the files in a directory and split each file into separate pieces based on the number of header rows in the file.
The text file read works.
The tso.WriteLine(strText) line fails.
Suggestions on what I am doing wrong would be appreciated.
'**********************************************************************
' Split the file if it has more than one header row
'************************************************************************
Function Main()
Const ForReading = 1
Const ForWriting = 2
Const HeaderLine = "-HEADER"
' our variables
Dim objFSODirectory
Dim myFile
Dim objTextStream
Dim strText
Dim strInFileName
Dim strOutFileName
Dim sFiles
Dim sTargetFolder
Dim oFSO
Dim outFileName
Dim tso
' Create File System Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
' Get the root directory name from global variable
Set sTargetFolder = oFSO.GetFolder(DTSGlobalVariables"ToDirectory").Value)
Set sFiles = sTargetFolder.Files
For Each sFiles In sFiles
sFileName = sFiles.Name
sFilesPath = sFiles.Path
'open text file
Set objTextStream = oFSO.OpenTextFile(sFiles , ForReading , False)
outFileName = sFileName + ".1"
Set tso = oFSO.OpenTextFile(outFileName, ForWriting, True)
If not objTextStream.AtEndOfStream Then
Do While not objTextStream.AtEndOfStream
strText = objTextStream.ReadLine
If InStr(strText, HeaderLine) Then
strMsg = strText + " is a header line."
msgBox strMsg
End If
tso.WriteLine(strText)
tso.Close
Loop
Else
tso.Close
End If
NEXT
' objTextStream.Close
Set objTextStream = Nothing
Set tso = Nothing
Main = DTSTaskExecResult_Success
End Function
June 22, 2004 at 3:25 pm
changed the location of closing the out text stream, it was closing after every writing a line and not being reopened. Check the error number and description after each operation, reset the err after each operation.
If not objTextStream.AtEndOfStream Then
Do While not objTextStream.AtEndOfStream
strText = objTextStream.ReadLine
tso.WriteLine(strText)
Loop
End If
tso.Close
June 22, 2004 at 5:50 pm
Thanks for the help.
Trying to learn this from an online tutorial which indicated the close had to come after each write to get the text to appear in the file. Odd, but at the moment I am too lost to argue.
How / where do the return codes get back to the program? That's the other thing I haven't been able to find yet. I don't see where the "SET tso = " yields a checkable return.
June 23, 2004 at 7:54 am
To get any error info on the operation
err.clear
Set tso = oFSO.OpenTextFile(outFileName, ForWriting, True)
if err.number <> 0 then
msgbox err.desc
End if
Try this - when the tso object is closed you get an error.
If not objTextStream.AtEndOfStream Then
Do While not objTextStream.AtEndOfStream
strText = objTextStream.ReadLine
tso.WriteLine(strText)
Loop
End If
tso.Close
The default file path if none is set in outfilename is
..\..\microsoft sql server\80\tools\bin
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply