 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Mar 14, 2006 10:52 pm Post subject: Erasing an active log file. Possible? |
|
|
Hello all, I have ran into an issue where I need to erase an active logfile. It grows huge over time and you cannot just delete this file. You can how ever open it with a text editor, erase it, then save it and that will erase it. Being the file is in use you cannot delete or move it.
Anyone have a suggestion on the easiest way to automate this issue using ahk? |
|
| Back to top |
|
 |
Micha
Joined: 15 Nov 2005 Posts: 437 Location: Germany
|
Posted: Wed Mar 15, 2006 12:11 pm Post subject: |
|
|
There's a Tool "Unlocker".
If a program opens a file, it has to ask the OS for a handle. While a program has a handle to the file, you cannot delete the file, but sometimes you can rename the file (funny behavior, but you can reproduce it)
If you want to unlock a file to delete it you can use the tool unlocker. The software snatches the handle and frees it. After the system has the handle back, you can delete the file.
WARNING: This could destabilize the other program which means it still is owner of an valid handle.
If there's no better solution, you could automate Unlocker with Autohotkey
Ciao
Micha |
|
| Back to top |
|
 |
jroad
Joined: 06 Feb 2006 Posts: 26
|
Posted: Wed Mar 15, 2006 1:11 pm Post subject: Re: Erasing an active log file. Possible? |
|
|
| Anonymous wrote: | Hello all, I have ran into an issue where I need to erase an active logfile. It grows huge over time and you cannot just delete this file. You can how ever open it with a text editor, erase it, then save it and that will erase it. Being the file is in use you cannot delete or move it.
Anyone have a suggestion on the easiest way to automate this issue using ahk? |
It's not necessarily elegant, but with AHK you could automate the clearing of the log file by automating the tasks you describe: open log file with editor, select all text, delete text, save file, close editor. |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Mar 15, 2006 3:19 pm Post subject: |
|
|
A simple binary access to the file would be enough.
Since AHK doesn't allow this yet, you have to use DllCall.
Derivating code from my BinReadWrite:
| Code: | INVALID_HANDLE_VALUE = -1
filename = E:\tmp\A-B.C d.txt
handle := DllCall("CreateFile"
, "Str", filename ; lpFileName
, "UInt", 0x40000000 ; dwDesiredAccess (GENERIC_WRITE)
, "UInt", 3 ; dwShareMode (FILE_SHARE_READ|FILE_SHARE_WRITE)
, "UInt", 0 ; lpSecurityAttributes
, "UInt", 5 ; dwCreationDisposition (TRUNCATE_EXISTING: truncates an existing file)
, "UInt", 0 ; dwFlagsAndAttributes
, "UInt", 0) ; hTemplateFile
If (handle = INVALID_HANDLE_VALUE or handle = 0)
{
MsgBox Cannot truncate the file '%filename%'
Exit
}
DllCall("CloseHandle", "UInt", handle)
|
It may not work if the lock on the file is too strong. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5873
|
Posted: Wed Mar 15, 2006 3:34 pm Post subject: |
|
|
| Guest wrote: | | You can how ever open it with a text editor, erase it, then save it and that will erase it. |
| jroad wrote: | | It's not necessarily elegant, but with AHK you could automate the clearing of the log file by automating the tasks you describe: open log file with editor, select all text, delete text, save file, close editor. |
| Code: | LogFile=C:\FileList.txt
IfNotExist, %LogFile%
ExitApp
DetectHiddenWindows,ON
FileGetSize, OldSize, %LogFile%, K
Run, TheGun.exe "%LogFile%",,Hide
WinWait,%LogFile% ahk_class TheGun30_Class,,10
WinMenuSelectItem, %LogFile% ahk_class TheGun30_Class, , Edit, Select All
WinMenuSelectItem, %LogFile% ahk_class TheGun30_Class, , Edit, Delete
WinMenuSelectItem, %LogFile% ahk_class TheGun30_Class, , File, Save
WinMenuSelectItem, %LogFile% ahk_class TheGun30_Class, , File, Exit
WinWaitClose,%LogFile% ahk_class TheGun30_Class,,10
FileGetSize, NewSize, %LogFile%, K
MsgBox,64,LogFile Truncation,
(
LogFile `t: %LogFile%
OldSize `t: %OldSize% KB
NewSize `t: %NewSize% KB
)
,6 |
Note: The job gets done in the background! you will never see the Editor
The above code requires TheGun.exe which maybe extracted from this download: gun30f.zip
You can easily modfy the code to suit Notepad.exe, but Notepad cannot handle large text files.
For more Info on TheGun.exe: Visit this post: * TheGun.exe * Ultra Light-Weight Text Editor [Only 6k!]
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Mar 15, 2006 3:56 pm Post subject: |
|
|
Thank you so much for the replies. Both of the suggested work arounds will truncate an active log file.
 |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Wed Mar 15, 2006 3:56 pm Post subject: |
|
|
I know TheGun (still around, it seems), written in assembly langage, very compact... It is indeed useful for such task.
I didn't knew the WinMenuSelectItem, it is more friendly than the solution often given in this forum to use SendMessage to activate menu items...
I guess I still have portions of the manual to read... That's the usefulness of helping others, we learn a lot while doing that.
Note: AFAIK, the Notepad being limited to 64KB was a Win9x limitation. In WinNT/2k/XP, there is no more limitation (I think).
Just tought of another simple solution:
Run %comspec% /c echo . > TheBigLogFile.log, , Hide
I am not sure if there is a mean to send "nothing": this writes a dot and a newline to reset the file. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Guest
|
Posted: Wed Mar 15, 2006 4:20 pm Post subject: |
|
|
| Awesome, that one liner seems to work as well. So great to see so many work arounds and they get better and better. Less code, less bugs. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5873
|
Posted: Wed Mar 15, 2006 4:27 pm Post subject: |
|
|
Dear PhiLho,
| Quote: | | That's the usefulness of helping others, we learn a lot while doing that. |
Yes! I agree.. This is the first time I tried WinMenuSelectItem after getting mad trying to ControlSend
| Quote: | | Note: AFAIK, the Notepad being limited to 64KB was a Win9x limitation. In WinNT/2k/XP, there is no more limitation (I think). |
Oh! Thats News to me.. I have replaced Notepad with Metapad in both OS.
| Quote: | Just tought of another simple solution:
Run %comspec% /c echo . > TheBigLogFile.log, , Hide
I am not sure if there is a mean to send "nothing": this writes a dot and a newline to reset the file. |
Plain DOS:
If we have a null.txt sized 0 bytes
then
Type null.txt > TheBigLogFile.txt
Should reset the file to 0 Bytes
Thanks & Regards,  _________________ SKAN - Suresh Kumar A N |
|
| 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
|