 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
PistolPete
Joined: 20 Jan 2009 Posts: 22 Location: Oklahoma City
|
Posted: Fri Oct 02, 2009 5:08 pm Post subject: Question regarding FileAppend |
|
|
Hi everyone,
I wrote a VERY SIMPLE script the other day to read the mapped drives a user has and write the findings to a file on a server. After a few days my CSV file isn’t what I expected – although if I hand test it on a few PC’s it works great.
My question: When I use FileAppend, I assume the file is opened & locked (by the server os), appended, closed and unlocked. Is this assumption wrong? {about 4,000 users ran this script over a few days}
I’m beginning to think the .exe became slightly corrupt when I put it out in the field (pushed it to the environment using SCCM).
Thank you for your time!
| Code: |
EnvGet, FQDN, USERDNSDOMAIN ;Environment Variable.. found by typing set then enter at cmd..
FormatTime, Time, A_Now, HH:mm:ss
FormatTime, Date, A_Now, MM/dd/yy
TheMessage =
Loop, HKCU, Network, 1, 1
{
if (A_LoopRegName = "RemotePath")
{
StringRight, NetDrive, A_LoopRegSubKey, 1
RegRead, RemotePath
StringUpper, NetDrive, NetDrive
StringUpper, RemotePath, RemotePath
TheString = %A_ComputerName%,%FQDN%,%A_IPAddress1%,%Date%,%Time%,%A_UserName%,%NetDrive%,%RemotePath%`n
TheMessage := TheMessage TheString
}
}
loop, 1000 ;I'm assuming the file is locked and may need to try appending more than once.
{
FileAppend, %TheMessage%, \\ServerName\ClientLogs\ServerConsolidation\MappedDrives.log
if (ErrorLevel = 0)
Break
Random, rand, 250, 10000
Sleep, rand
}
ExitApp, 0
|  |
|
| Back to top |
|
 |
TchnclFl
Joined: 29 Jul 2009 Posts: 48
|
|
| Back to top |
|
 |
PistolPete
Joined: 20 Jan 2009 Posts: 22 Location: Oklahoma City
|
Posted: Mon Oct 05, 2009 6:00 pm Post subject: |
|
|
| TchnclFl wrote: | | You do realize that FileAppend will add the supplied text (or variable) to the end of the file each time. Eg, if you're added "this is a test" to the end of a file (and you loop 1000 times), you'll end up with "this is a testthis is a testthis is a testthis is a test..." etc. |
Yes, that's correct - that's why I have the break command in the loop in the event the file was appended without an error. As for the line feed, I have the `n in the text to incorporate the CRLF. In the event there is an error then I'm assuming it's because the file is locked so I sleep for a random amount of time then try again.. and again, and again unless it tries 1001 times in which case it should exit the loop and then exit the application.
The code works perfectly but once in a while it appears that the file was appended at the same time another append operation occurred, which is not what I expected. Looking at the output I have thousands of lines that are correct and a few hundred where we had a glare (two appends with one overwriting a portion of the other). |
|
| Back to top |
|
 |
aaffe
Joined: 17 May 2007 Posts: 1002 Location: Germany - Deutschland
|
Posted: Tue Oct 06, 2009 11:34 am Post subject: |
|
|
Im not sure if several users write to the same file, the file is locked by the first one.
You could have all users write to an own file like %A_Username%%A_Computername%.log.
Then you have a masterscript on the server which puts all these files together into one big logfile... |
|
| Back to top |
|
 |
Z_Gecko Guest
|
Posted: Tue Oct 06, 2009 11:54 am Post subject: |
|
|
another idea would be using a second file to display the lock state of the main file. So your script could e.g.:
check if the "status-file" contains the word "locked"
if yes, it sleeps for a random time
if not it sets the contents of the "status-file" to "locked"
then it writes to the log-file, and when done
it sets the contents of the "status-file" to "unlocked"
this would reduce overlapped writing to a minimum.
additionally you could generally randomize the times, when you write the log-file, if appropriate. |
|
| Back to top |
|
 |
MasterFocus
Joined: 08 Apr 2009 Posts: 3035 Location: Rio de Janeiro - RJ - Brasil
|
Posted: Tue Oct 06, 2009 1:21 pm Post subject: |
|
|
| aaffe wrote: | Im not sure if several users write to the same file, the file is locked by the first one.
You could have all users write to an own file like %A_Username%%A_Computername%.log.
Then you have a masterscript on the server which puts all these files together into one big logfile... |
Seems a nice workaround.
Here's what I usually do considering a log file containing timestamps:
| Code: | varTime := A_Hour ":" A_Min ":" A_Sec "." Mod(Round((Round(A_MSec,-2)/100)),10)
FileAppend, %time% text here...`n, %varFile% |
_________________ "Read the manual. Read it again. Search the forum.
Try something before asking. Show what you've tried."
Antonio França
My stuff: Google Profile |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|