How do I monitor a specific file in a directory? Topic is solved
Re: How do I monitor a specific file in a directory?
I tried it on the bigger script initially with no success, tried it on your code and it made your code work, and then here's the baffling part: I made no changes to the bigger script and it just starts working. Out of nowhere. It's the same one I was using yesterday, I haven't touched it. It has #Persistent on line 5 like it has this entire time but for some reason it didn't work last night and now works today without modifying it. I'm absolutely baffled. Happy, but baffled.
P.S. is there any reason I should have it wait 5 seconds? Like performance issues or eating away at a device like a hard drive or an SSD? What's stopping me from just setting it to 1? Not 1000 (1 second) 1.
P.S. is there any reason I should have it wait 5 seconds? Like performance issues or eating away at a device like a hard drive or an SSD? What's stopping me from just setting it to 1? Not 1000 (1 second) 1.
- flyingDman
- Posts: 2791
- Joined: 29 Sep 2013, 19:01
Re: How do I monitor a specific file in a directory?
There is no reason to set it specifically at 5 seconds. You can set at much lower than that. 1 seems a bit excessive but I don't think it will impact the performance of your PC much. A sensible value would something like 100 to 250. It will appear instantaneous.
14.3 & 1.3.7
Re: How do I monitor a specific file in a directory?
Code: Select all
fileread, contents, g:\List1.txt
if not errorlevel
{
sort, contents
filedelete, g:\List1.txt
fileappend, %contents%, g:\List1.txt
contents := ""
}
sleep 300
fileread, outputvar, g:\List1.txt
sort, outputvar, u
filedelete, g:\List1.txt
sleep 300
fileappend, %outputvar%,g:\List1.txt
Re: How do I monitor a specific file in a directory?
Got it! Thank you and boiler so much for the help!!flyingDman wrote: ↑30 Mar 2023, 20:46There is no reason to set it specifically at 5 seconds. You can set at much lower than that. 1 seems a bit excessive but I don't think it will impact the performance of your PC much. A sensible value would something like 100 to 250. It will appear instantaneous.
The first time sorts it alphabetically, and the second removes duplicate lines.just me wrote:Why are you sorting the file twice?Code: Select all
fileread, contents, g:\List1.txt if not errorlevel { sort, contents filedelete, g:\List1.txt fileappend, %contents%, g:\List1.txt contents := "" } sleep 300 fileread, outputvar, g:\List1.txt sort, outputvar, u filedelete, g:\List1.txt sleep 300 fileappend, %outputvar%,g:\List1.txt
Re: How do I monitor a specific file in a directory?
Code: Select all
Sort,Var, U
The file has already been included before you sort and rewrite its contents. So what is your intension?
Re: How do I monitor a specific file in a directory?
Code: Select all
fileread, contents, g:\List1.txt
if not errorlevel
{
sort, contents, c cl u,
filedelete, g:\List1.txt
fileappend, %contents%, g:\List1.txt
contents := ""
}
Re: How do I monitor a specific file in a directory?
Why did you specify options for both case sensitive and case insensitive sort?
Re: How do I monitor a specific file in a directory?
@flyingDman,
@boiler
As soon as I open a MS Office document in the specific folder or the specific file is a MS Office document the message "The folder has changed!"/"The file has changed!" comes up both in WatchFolder() and in flyingDman's code.
The same happens if I close a MS Office document without having changed it.
Why is that? Can be prevented?
@boiler
As soon as I open a MS Office document in the specific folder or the specific file is a MS Office document the message "The folder has changed!"/"The file has changed!" comes up both in WatchFolder() and in flyingDman's code.
The same happens if I close a MS Office document without having changed it.
Why is that? Can be prevented?
Re: How do I monitor a specific file in a directory?
For WatchFolder(), have your monitoring function look for and act only on the specific types of changes that you are interested in.
- flyingDman
- Posts: 2791
- Joined: 29 Sep 2013, 19:01
Re: How do I monitor a specific file in a directory?
Not here (with my script). Only when I edit the file and save.GEV wrote: ↑02 Apr 2023, 07:52@flyingDman,
@boiler
As soon as I open a MS Office document in the specific folder or the specific file is a MS Office document the message "The folder has changed!"/"The file has changed!" comes up both in WatchFolder() and in flyingDman's code.
The same happens if I close a MS Office document without having changed it.
Why is that? Can be prevented?
To back-up as soon as the file has changed, you could use this:
Code: Select all
#Requires AutoHotkey v1.1.33
#persistent
settimer, lbl, 100
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\test.docx
if (oldtmstmp != tmstmp AND flag)
{
Msgbox The file has changed!
FileCopy, %A_ScriptDir%\test.docx, %A_ScriptDir%\test_bak.docx, 1
}
oldtmstmp := tmstmp, flag := 1
return
Last edited by flyingDman on 02 Apr 2023, 12:44, edited 1 time in total.
14.3 & 1.3.7
Re: How do I monitor a specific file in a directory?
There are some situations where it appears that Office itself alters a file. Although I have experienced it, I have never figured out what the conditions are.
Re: How do I monitor a specific file in a directory?
If I run thisflyingDman wrote: ↑02 Apr 2023, 12:34Not here (with my script). Only when I edit the file and save.
Code: Select all
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
#Persistent
GroupAdd, Office_Documents, ahk_exe WINWORD.EXE
GroupAdd, Office_Documents, ahk_exe EXCEL.EXE
flag := ""
oldtmstmp := ""
Path := ""
Path2 := ""
SetTimer, backup_document, 2000
Return
backup_document:
If !WinActive("ahk_group Office_Documents")
Return
If WinActive("ahk_exe WINWORD.EXE")
{
oWord := ComObjActive("Word.Application")
Path := oWord.ActiveDocument.FullName
}
else
{
oExcel := ComObjActive("Excel.Application")
Path := oExcel.ActiveWorkbook.FullName
}
FileGetTime, tmstmp, %Path%
if (oldtmstmp != tmstmp AND flag)
{
Msgbox "%Path%"`nhas changed!
FormatTime, CurrentDate,, yyyy_MM_dd
FormatTime, CurrentDateTime,, yyyy_MM_dd HH.mm
SplitPath, Path, name, dir, ext, name_no_ext, drive
dir := StrReplace(dir, ":\", "_")
dir := StrReplace(dir, "\", "_")
name_no_ext := StrReplace(name_no_ext, "\", "_")
Path2 := StrReplace(Path, ":\", "_")
Path2 := StrReplace(Path2, "\", "_")
FileCreateDir, D:\DOKUMENT-Backups\%Path2%
Sleep, 300
FileCopy, %Path%, D:\DOKUMENT-Backups\%Path2%\%dir%_%name_no_ext%%A_Space%%CurrentDateTime%.%ext% ,1
Sleep, 300
Run D:\DOKUMENT-Backups\%Path2%
}
oldtmstmp := tmstmp, flag := 1
Return
As soon as I open a second document (and have more than one opened) the message "%Path%"has changed! appears and the backup is created every time I change the active document.
OK, that happens because by activating a second document the variable tmstmp changes. Is there a way to get the script working for more than one opened document?
Last edited by GEV on 02 Apr 2023, 13:52, edited 1 time in total.
Re: How do I monitor a specific file in a directory?
You would look for the Action property of the change object to equal 3, then act on it. If Word itself is changing the file, as mikeyww suggested, then you’ll probably get that message more often than you were expecting. I would think that would happen when you have auto-save on, which I believe is the default.