![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | how to write into a big textfile ? Hello everybody, i need your help : i have to write some lines coming from a database into a textfile; Everything goes right while the filesize is quite small or medium; But it's different when the file becomes a bit large, as it returns the following error : "The process cannot access the file because it is being used" As my scripts were written in powershell using the function Add-Content, i though it would be resolved by using the dotnet functions, building and using StreamWriters or such possibilities... but no way; Actually, i thing the process of writing the stream into the textfile become to slow when the filesize is big, and the following data are coming before the first are on te file. i don't know how to resolve my problem; is it possible to wait for the first process to be finished before writing the new data into the file ? Thx Nico |
My System Specs![]() |
| | #2 (permalink) |
| | Re: how to write into a big textfile ? Welllll... I can't exactly duplicate this to try it out, but I can offer ideas. One would be to implement a trap for this exception - in it, you could just wait a second or two and try again. Another would be to append all of this info into an in-memory variable, rather than writing it out to the file, and then dump it into the file at the end. Dunno how much data we're talking about to determine if that's feasible or not. If we accept your theory that the file write isn't being completed, and is thus locking up the file (which I think is a viable theory), then these might be the only options (or, well, something along these lines). On Nov 9, 12:24 pm, <Nicopil@xxxxxx> wrote: Quote: > Hello everybody, i need your help : i have to write some lines coming from a > database into a textfile; > > Everything goes right while the filesize is quite small or medium; But it's > different when the file becomes a bit large, as it returns the following > error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, i > though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile become > to slow when the filesize is big, and the following data are coming before > the first are on te file. > > i don't know how to resolve my problem; is it possible to wait for the first > process to be finished before writing the new data into the file ? > > Thx > Nico |
My System Specs![]() |
| | #3 (permalink) |
| | Re: how to write into a big textfile ? General rule of thumb (with only special case exceptions): writing to a text file must be serialized, so you must design your application accordingly. In my view, the easiest way of ensuring this is to make sure only one thread is doing the writing. You haven't provided sufficient information to be more specific than that. Brian |
My System Specs![]() |
| | #4 (permalink) |
| | Re: how to write into a big textfile ? I'd like to try and test/duplicate this. How large is large? How many lines of text? What is an average line length in characters? Are you writing to a local or network drive? -- Jeffery Hicks Microsoft PowerShell MVP http://www.scriptinganswers.com http://www.powershellcommunity.org Now Available: WSH and VBScript Core: TFM Coming Soon: Windows PowerShell: TFM 2nd Ed. <Nicopil@xxxxxx> wrote in message news:4734c206$0$29626$426a74cc@xxxxxx Quote: > Hello everybody, i need your help : i have to write some lines coming from > a database into a textfile; > > Everything goes right while the filesize is quite small or medium; But > it's different when the file becomes a bit large, as it returns the > following error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, i > though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile > become to slow when the filesize is big, and the following data are coming > before the first are on te file. > > > i don't know how to resolve my problem; is it possible to wait for the > first process to be finished before writing the new data into the file ? > > Thx > Nico > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: how to write into a big textfile ? "Don Jones [MVP]" <scriptinganswers@xxxxxx> a écrit dans le message de news: 1194643132.013990.175850@xxxxxx... Quote: > Welllll... I can't exactly duplicate this to try it out, but I can > offer ideas. > > One would be to implement a trap for this exception - in it, you could > just wait a second or two and try again. > > Another would be to append all of this info into an in-memory > variable, rather than writing it out to the file, and then dump it > into the file at the end. Dunno how much data we're talking about to > determine if that's feasible or not. > > If we accept your theory that the file write isn't being completed, > and is thus locking up the file (which I think is a viable theory), > then these might be the only options (or, well, something along these > lines). > > On Nov 9, 12:24 pm, <Nicopil@xxxxxx> wrote: Quote: >> Hello everybody, i need your help : i have to write some lines coming >> from a >> database into a textfile; >> >> Everything goes right while the filesize is quite small or medium; But >> it's >> different when the file becomes a bit large, as it returns the following >> error : >> >> "The process cannot access the file because it is being used" >> >> As my scripts were written in powershell using the function Add-Content, >> i >> though it would be resolved by using the dotnet functions, building and >> using StreamWriters or such possibilities... >> but no way; >> >> Actually, i thing the process of writing the stream into the textfile >> become >> to slow when the filesize is big, and the following data are coming >> before >> the first are on te file. >> >> i don't know how to resolve my problem; is it possible to wait for the >> first >> process to be finished before writing the new data into the file ? >> >> Thx >> Nico > well, we speak about thousend of data records, each of them containing all the properties of each process running at a moment... so many data; too many for storing it into a variable at first, and write it on a second hand (i already tried it before sending this first post ;o) ) I guess the trap method could be very good, but i don't know realy how i could do it; would you have an exemple ? thx Nico |
My System Specs![]() |
| | #6 (permalink) |
| | Re: how to write into a big textfile ? Nicopil@xxxxxx wrote: Quote: > Hello everybody, i need your help : i have to write some lines coming from a > database into a textfile; > > Everything goes right while the filesize is quite small or medium; But it's > different when the file becomes a bit large, as it returns the following > error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, i > though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile become > to slow when the filesize is big, and the following data are coming before > the first are on te file. > > > i don't know how to resolve my problem; is it possible to wait for the first > process to be finished before writing the new data into the file ? > > Thx > Nico > back. If you only want to add text to the end of the file, you don't have to do that. Open the file for append, then just write what you want to add to the file. -- Göran Andersson _____ http://www.guffa.com |
My System Specs![]() |
| | #7 (permalink) |
| | Re: how to write into a big textfile ? If the files are expected to get extremely large you can speed the process up by preallocating the space. However, it means you can no longer do a simple append. You'd instead need to know where the last write left off. Quote: > i don't know how to resolve my problem; is it possible to wait for the > first process to be finished before writing the new data into the file ? I've found is to try to open the file with exclusive access inside a try...catch block. If it fails you can sleep for some period of time and then try again. Just make sure you check the exception type. You want to keep retrying so long as the exception is due to the file already being opened. There may be other exceptions you want raised up. -- Andrew Faust andrew[at]andrewfaust.com http://www.andrewfaust.com <Nicopil@xxxxxx> wrote in message news:4734c206$0$29626$426a74cc@xxxxxx Quote: > Hello everybody, i need your help : i have to write some lines coming > from a database into a textfile; > > Everything goes right while the filesize is quite small or medium; But > it's different when the file becomes a bit large, as it returns the > following error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, > i though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile > become to slow when the filesize is big, and the following data are > coming before the first are on te file. > > > i don't know how to resolve my problem; is it possible to wait for the > first process to be finished before writing the new data into the file ? > > Thx > Nico > |
My System Specs![]() |
| | #8 (permalink) |
| | RE: how to write into a big textfile ? If you have another applicatin writing to the file you'll have to wait for the other application to finish. If it is your appplication you could use a different sharing mode; but if you're writing to the start of the file and moving the rest of the data in the file you're probably out of luck. -- Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote. http://www.peterRitchie.com/blog/ Microsoft MVP, Visual Developer - Visual C# "Nicopil@xxxxxx" wrote: Quote: > Hello everybody, i need your help : i have to write some lines coming from a > database into a textfile; > > Everything goes right while the filesize is quite small or medium; But it's > different when the file becomes a bit large, as it returns the following > error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, i > though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile become > to slow when the filesize is big, and the following data are coming before > the first are on te file. > > > i don't know how to resolve my problem; is it possible to wait for the first > process to be finished before writing the new data into the file ? > > Thx > Nico > > > |
My System Specs![]() |
| | #9 (permalink) |
| | Re: how to write into a big textfile ? If all processes that are accessing the file are using Powershell or are otherwise programmable then you could use a named mutex to synchronize access to the file. Other than that, your only choice is to retry until you have access. <Nicopil@xxxxxx> wrote in message news:4734c206$0$29626$426a74cc@xxxxxx Quote: > Hello everybody, i need your help : i have to write some lines coming from > a database into a textfile; > > Everything goes right while the filesize is quite small or medium; But > it's different when the file becomes a bit large, as it returns the > following error : > > "The process cannot access the file because it is being used" > > As my scripts were written in powershell using the function Add-Content, i > though it would be resolved by using the dotnet functions, building and > using StreamWriters or such possibilities... > but no way; > > Actually, i thing the process of writing the stream into the textfile > become to slow when the filesize is big, and the following data are coming > before the first are on te file. > > > i don't know how to resolve my problem; is it possible to wait for the > first process to be finished before writing the new data into the file ? > > Thx > Nico > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| how to get powershell to write to event log or write a log file? | PowerShell | |||
| Re: split textfile to more textfiles | VB Script | |||
| Unable to retrieve value from drop down list (values read from textfile) | VB Script | |||
| The disk is write-protected. Remove the write-protection or... | Vista General | |||
| newline in Write-Debug or Write-Verbose | PowerShell | |||