Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - how to write into a big textfile ?

Reply
 
Old 11-09-2007   #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 SpecsSystem Spec
Old 11-09-2007   #2 (permalink)
Don Jones [MVP]


 
 

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 SpecsSystem Spec
Old 11-09-2007   #3 (permalink)
Brian Muth


 
 

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 SpecsSystem Spec
Old 11-09-2007   #4 (permalink)
Jeffery Hicks [MVP]


 
 

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 SpecsSystem Spec
Old 11-09-2007   #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 SpecsSystem Spec
Old 11-09-2007   #6 (permalink)
Göran Andersson


 
 

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
>
The file size only matters if you read the entire file and then write it
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 SpecsSystem Spec
Old 11-09-2007   #7 (permalink)
Andrew Faust


 
 

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 ?
Yeah. There are various mechanisms you could use, however, the easiest one
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 SpecsSystem Spec
Old 11-09-2007   #8 (permalink)
Peter Ritchie [C# MVP]


 
 

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 SpecsSystem Spec
Old 11-17-2007   #9 (permalink)
Steven Hystad


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46