![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | lock file while Add-Content Ok, I have one PS1 file with about 170 lines like this: $body = "something" Add-Content "$path$filename" $body $body = "something" Add-Content "$path$filename" $body .... it takes about 1 or 2 sec to complete whole operation. But, during this operation, "$path$filename" filename is open for edit by other process, and that’s the problem. Sometimes, somebody just delete the file in the middle of the Add-Content process. I want to "lock" (or something like that) file until PS1 script finish his job. tnx |
| | #2 (permalink) | ||||||||||||
| Guest | Re: lock file while Add-Content You can accumulate the content into a variable and then write it to the file. $body += "something" Add-Content "$path$filename" $body If its a large content it is better to use the StringBuilder: $body = new-object System.Text.StringBuilder [void]$body.Append("text") (...) Add-Content "$path$filename" $body ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
| ||||||||||||
| | #3 (permalink) | ||||||||||||
| Guest | Re: lock file while Add-Content Tnx, but it isn’t perfect solution. I can group 170 Add-Content into 3 large Add-Content commands (using $body += accumulation), but file is still vulnerable to edit/delete during that (now shorter) time. There is only one perfect solution – lock file until last Add-Content. help "Shay Levi" wrote:
| ||||||||||||
| | #4 (permalink) | ||||||||||||||||||||||||
| Guest | Re: lock file while Add-Content You can write your content to a temporary file in a safe place and when you finish switch the files. Why would someone delete the file? One can delete the file even if you'll successfully update its content regardless of the method used to update it. ----- Shay Levi $cript Fanatic http://scriptolog.blogspot.com Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
| ||||||||||||||||||||||||
| | #5 (permalink) | ||||||||||||
| Guest | Re: lock file while Add-Content Ok, I’ll be more detailed. Nobody edit/delete file in the middle of creation process. The thing is that my PS1 script export data from the SQL server into text files on remote shared folder. But, that folder is monitored every 10 seconds by one EXE program, and he import that text files in the other SQL server database. Problem is: every 10 seconds EXE program imports every single txt file, including the one in the middle of the creation! That leads to bad import data. To make things even worse, EXE take that “half” file and import it, next PS1 add-content create ANOTHER file with SAME filename and put next half of content, and in a 10 seconds EXE will terminate himself with error: There can’t be 2 files with same filename (unique column in database). Your suggestion to make file in temp folder + move item is bypass of the problem, but it creates unnecessary overhead and HDD disk operations. System (PS1 script) must be very, very fast, it creates several hundreds files per second. Lock file in the creation process is great solution, but how? Sorry for my bad English. "Shay Levi" wrote:
| ||||||||||||
| | #6 (permalink) | ||||||||||||||||||||||||
| Guest | Re: lock file while Add-Content No, your best solution is likely not to use a lock. The lock itself may cause the other process to crash. The best solution would be to first generate the entire body, then write it to file all at once to avoid any blocking. Set-Content $path $entirebody However, if you do insist that you want to use a locked file, and if that lock will not cause the other process to crash, then it is possible to create the file with an initial lock. # Create the file, it is also locked. $fs = [IO.File]::Create($filename) # Make a StreamWriter for ease of access $sw = New-Object IO.StreamWriter($fs) # Write some text to it $sw.WriteLine("some text") $sw.WriteLine("some more text") # Close the file, don't forget this or it will remain locked $sw.Close() "MiroslavK" <MiroslavK@xxxxxx> wrote in message news:56D84B88-2C6E-4BD2-BFC1-AD2A149B8BE4@xxxxxx
| ||||||||||||||||||||||||
| | #7 (permalink) | ||||||||||||||||||||||||||||||||||||
| Guest | Re: lock file while Add-Content tnx, tnx a lot !!! "Steven Hystad" wrote:
| ||||||||||||||||||||||||||||||||||||
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| lock single file/folders | pageyxr8 | Vista security | 1 | 12-15-2007 07:13 AM |
| Searching for file content on CD | Ludwig | Vista file management | 0 | 06-26-2007 05:42 PM |
| Set-Content not updating file after get-content and forEach-Object | Tolli | PowerShell | 1 | 06-14-2007 09:01 PM |
| About adding content to the same file using "add-content" | =?Utf-8?B?ZGFuY2UyZGll?= | PowerShell | 3 | 08-14-2006 11:59 AM |
| Weirdness with get-content | replace | set-content - file content is deleted!! | Andrew Watt [MVP] | PowerShell | 4 | 05-23-2006 05:59 PM |