Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I
used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath &
"\Update.txt")
The first time my Sub runs it works fine. But the second time it fails
telling me "Cannot write to a closed TextWriter. I am guessing that this is
because the Public section is only read once instance of the application. I
have to close the TextWriter so that I can use the file in another process.
Any idea how I can either ensure that the Public section is re-read, or,
provide the TextWriter to the 2nd Sub?
--
Bob Hollness
-------------------------------------
I'll have a B please Bob 10 2123
I think I'm a little confused at what you need to do. But your problem lies
in
If you declare like this:
Public Writer As StreamWriter
Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub
Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub
Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub
Now this way of doing it opens the file if it was closed. And uses the same
file if it was already opened. I think this is what you are talking about
doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
I think I'm a little confused at what you need to do. But your problem lies
in
If you declare like this:
Public Writer As StreamWriter
Public Sub1()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub
Public Sub2()
if Writer is Nothing then
Wrier = File.CreateText(Application.StartupPath & "\Update.txt")
end if
'Do Something
End Sub
Public Sub DoSomethingThatMustCloseFile
Writer.Close
Writer = Nothing
End Sub
Now this way of doing it opens the file if it was closed. And uses the same
file if it was already opened. I think this is what you are talking about
doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors on
line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush
WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With
End Sub
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:e0*************@TK2MSFTNGP12.phx.gbl... I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
Thanks for the reply. This is my code. As you can see, it recurses by
calling itself repeatedly. And then the sub ReadAllFiles tidies up by
closing the stream. My problem is that the 2nd time it runs it errors on
line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush
WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With
End Sub
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:e0*************@TK2MSFTNGP12.phx.gbl... I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
You are talking about "2nd time" meaning the second time you call the
function ReadAllFiles right. If that is the case then my solution in my
previous post solves the problem. After you close WriteTextUpdate you have
to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs
2. (Probably a better way for this case, as I stated before) Define then
object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call.
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
You are talking about "2nd time" meaning the second time you call the
function ReadAllFiles right. If that is the case then my solution in my
previous post solves the problem. After you close WriteTextUpdate you have
to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs
2. (Probably a better way for this case, as I stated before) Define then
object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call.
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
How can I declare it and then pass it to the Sub? This is new to me...
sorry..... :-(
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:ul**************@TK2MSFTNGP14.phx.gbl... You are talking about "2nd time" meaning the second time you call the function ReadAllFiles right. If that is the case then my solution in my previous post solves the problem. After you close WriteTextUpdate you have to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs 2. (Probably a better way for this case, as I stated before) Define then object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call. Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
How can I declare it and then pass it to the Sub? This is new to me...
sorry..... :-(
--
Bob Hollness
-------------------------------------
I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote
in message news:ul**************@TK2MSFTNGP14.phx.gbl... You are talking about "2nd time" meaning the second time you call the function ReadAllFiles right. If that is the case then my solution in my previous post solves the problem. After you close WriteTextUpdate you have to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs 2. (Probably a better way for this case, as I stated before) Define then object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call. Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl...I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... Hi all.
I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below.
Public Writer As StreamWriter = File.CreateText(Application.StartupPath & "\Update.txt")
The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is because the Public section is only read once instance of the application. I have to close the TextWriter so that I can use the file in another process. Any idea how I can either ensure that the Public section is re-read, or, provide the TextWriter to the 2nd Sub?
-- Bob Hollness
------------------------------------- I'll have a B please Bob
Like I said, I'd probably declare it like the code I showed since you are
doing it recursive and all.
But...
Sub ReadAllFiles
Dim WriteTxtUpdateForThisSub as StreamWriter = File.Create(....)
Call ReadDirs(WHATEVER_PATH, True, WriteTxtUpdateForThisSub)
WriteTxtUpdateForThisSub.Flush
WriteTxtUpdateForThisSub.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean, ByRef
WriteTxtUpdate as StreamWriter)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive, WriteTxtUpdate)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With
End Sub
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl... How can I declare it and then pass it to the Sub? This is new to me... sorry..... :-(
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:ul**************@TK2MSFTNGP14.phx.gbl... You are talking about "2nd time" meaning the second time you call the function ReadAllFiles right. If that is the case then my solution in my previous post solves the problem. After you close WriteTextUpdate you have to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs 2. (Probably a better way for this case, as I stated before) Define then object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call. Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl... I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... > Hi all. > > I have a Sub that calls another sub. Both subs use a common object, > so I used Public to declare it at the top of the module, as below. > > Public Writer As StreamWriter = > File.CreateText(Application.StartupPath & "\Update.txt") > > The first time my Sub runs it works fine. But the second time it > fails telling me "Cannot write to a closed TextWriter. I am guessing > that this is because the Public section is only read once instance of > the application. I have to close the TextWriter so that I can use the > file in another process. Any idea how I can either ensure that the > Public section is re-read, or, provide the TextWriter to the 2nd Sub? > > -- > Bob Hollness > > ------------------------------------- > I'll have a B please Bob >
Like I said, I'd probably declare it like the code I showed since you are
doing it recursive and all.
But...
Sub ReadAllFiles
Dim WriteTxtUpdateForThisSub as StreamWriter = File.Create(....)
Call ReadDirs(WHATEVER_PATH, True, WriteTxtUpdateForThisSub)
WriteTxtUpdateForThisSub.Flush
WriteTxtUpdateForThisSub.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean, ByRef
WriteTxtUpdate as StreamWriter)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then
For Each sDir In Directory.GetDirectories(Sourcedir)
sDirInfo = New DirectoryInfo(sDir)
ReadDirs(sDirInfo.FullName, fRecursive, WriteTxtUpdate)
sDirInfo = Nothing
Next
End If
With WriteTxtUpdate
For Each sFile In Directory.GetFiles(Sourcedir)
.WriteLine(sFile)
Next
End With
End Sub
"Bob Hollness" <bo*@blockbuster.com> wrote in message
news:en**************@TK2MSFTNGP12.phx.gbl... How can I declare it and then pass it to the Sub? This is new to me... sorry..... :-(
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:ul**************@TK2MSFTNGP14.phx.gbl... You are talking about "2nd time" meaning the second time you call the function ReadAllFiles right. If that is the case then my solution in my previous post solves the problem. After you close WriteTextUpdate you have to open it again before you use it. Two ways to solve it.
1. Declare WriteTextUpdate inside ReadAllFiles and pass it into ReadDirs 2. (Probably a better way for this case, as I stated before) Define then object WriteTextUpdate as public but open it in ReadAllFiles
Public WriteTxtUpdate as StreamWriter
Sub ReadAllFiles
WriteTxtUpdate = File.CreateTextFile(........)
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
You won't get an error this way, but the old file is destoyed w/ every call. Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:uk**************@TK2MSFTNGP15.phx.gbl... Thanks for the reply. This is my code. As you can see, it recurses by calling itself repeatedly. And then the sub ReadAllFiles tidies up by closing the stream. My problem is that the 2nd time it runs it errors on line ".WriteLine(sFile) in the ReadDirs Sub.
Sub ReadAllFiles
Call ReadDirs(WHATEVER_PATH, True)
WriteTxtUpdate.Flush WriteTxtUpdate.Close
End Sub
Sub ReadDirs(ByVal Sourcedir As String, ByVal recursive As Boolean)
Sourcedir &= Path.DirectorySeparatorChar
If fRecursive Then For Each sDir In Directory.GetDirectories(Sourcedir) sDirInfo = New DirectoryInfo(sDir) ReadDirs(sDirInfo.FullName, fRecursive) sDirInfo = Nothing Next End If
With WriteTxtUpdate For Each sFile In Directory.GetFiles(Sourcedir) .WriteLine(sFile) Next End With
End Sub
-- Bob Hollness
------------------------------------- I'll have a B please Bob
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com> wrote in message news:e0*************@TK2MSFTNGP12.phx.gbl... I think I'm a little confused at what you need to do. But your problem lies in
If you declare like this:
Public Writer As StreamWriter
Public Sub1() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub2() if Writer is Nothing then Wrier = File.CreateText(Application.StartupPath & "\Update.txt") end if 'Do Something End Sub
Public Sub DoSomethingThatMustCloseFile Writer.Close Writer = Nothing End Sub
Now this way of doing it opens the file if it was closed. And uses the same file if it was already opened. I think this is what you are talking about doing. If not, can you post some sample code that shows the problem more?
Chris
"Bob Hollness" <bo*@blockbuster.com> wrote in message news:Ok**************@TK2MSFTNGP14.phx.gbl... > Hi all. > > I have a Sub that calls another sub. Both subs use a common object, > so I used Public to declare it at the top of the module, as below. > > Public Writer As StreamWriter = > File.CreateText(Application.StartupPath & "\Update.txt") > > The first time my Sub runs it works fine. But the second time it > fails telling me "Cannot write to a closed TextWriter. I am guessing > that this is because the Public section is only read once instance of > the application. I have to close the TextWriter so that I can use the > file in another process. Any idea how I can either ensure that the > Public section is re-read, or, provide the TextWriter to the 2nd Sub? > > -- > Bob Hollness > > ------------------------------------- > I'll have a B please Bob >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nel |
last post by:
I have a question related to the "security" issues posed by Globals ON.
It is good programming technique IMO to initialise variables, even if it's
just
$foo = 0;
$bar = "";
Surely it would...
|
by: Craig Bailey |
last post by:
Anyone recommend a good script editor for Mac OS X?
Just finished a 4-day PHP class in front of a Windows machine, and liked
the editor we used. Don't recall the name, but it gave line numbers as...
|
by: Chris |
last post by:
Sorry to post so much code all at once but I'm banging my head against
the wall trying to get this to work!
Does anyone have any idea where I'm going wrong?
Thanks in advance and sorry again...
|
by: James |
last post by:
My form and results are on one page.
If I use :
if ($Company) {
$query = "Select Company, Contact
From tblworking
Where ID = $Company
Order By Company ASC";
}
|
by: Alan Walkington |
last post by:
Folks:
How can I get an /exec'ed/ process to run in the background on an XP box?
I have a monitor-like process which I am starting as
'exec("something.exe");' and, of course the exec function...
|
by: John Ryan |
last post by:
What PHP code would I use to check if submitted sites to my directory
actually exist?? I want to use something that can return the server
code to me, ie HTTP 300 OK, or whatever. Can I do this with...
|
by: James |
last post by:
What is the best method for creating a Web Page that uses both
PHP and HTML ?
<HTML>
BLA
BLA
BLA
BLA
BLA
|
by: Beowulf |
last post by:
Hi Guru's,
I have a query regarding using PHP to maintain a user profiles list. I
want to be able to have a form where users can fill in their profile
info (Name, hobbies etc) and attach an...
|
by: joost |
last post by:
Hello,
I'm kind of new to mySQL but more used to Sybase/PHP
What is illegal about this query or can i not use combined query's in mySQL?
DELETE FROM manufacturers
WHERE manufacturers_id ...
|
by: presspley |
last post by:
I have bought the book on advanced dreamweaver and PHP recently. I
have installed MySQL and PHP server but am getting an error on the
$GET statement show below. It says there is a problem with...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |