How do I monitor a specific file in a directory? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
J0RDAN
Posts: 17
Joined: 02 Nov 2022, 17:29

Re: How do I monitor a specific file in a directory?

Post by J0RDAN » 30 Mar 2023, 20:04

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.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: How do I monitor a specific file in a directory?

Post by flyingDman » 30 Mar 2023, 20:46

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

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How do I monitor a specific file in a directory?

Post by just me » 31 Mar 2023, 04:25

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
Why are you sorting the file twice?

J0RDAN
Posts: 17
Joined: 02 Nov 2022, 17:29

Re: How do I monitor a specific file in a directory?

Post by J0RDAN » 31 Mar 2023, 14:16

flyingDman wrote:
30 Mar 2023, 20:46
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.
Got it! Thank you and boiler so much for the help!!
just me wrote:

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
Why are you sorting the file twice?
The first time sorts it alphabetically, and the second removes duplicate lines.

just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How do I monitor a specific file in a directory?

Post by just me » 01 Apr 2023, 03:22

Code: Select all

Sort,Var, U
sorts the contents of Var alphabetically and removes duplicate items from the result.

The file has already been included before you sort and rewrite its contents. So what is your intension?

J0RDAN
Posts: 17
Joined: 02 Nov 2022, 17:29

Re: How do I monitor a specific file in a directory?

Post by J0RDAN » 02 Apr 2023, 00:58

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 := ""
}
Would this be a better way to write it?

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: How do I monitor a specific file in a directory?

Post by boiler » 02 Apr 2023, 03:38

Why did you specify options for both case sensitive and case insensitive sort?

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How do I monitor a specific file in a directory?

Post by GEV » 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?

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: How do I monitor a specific file in a directory?

Post by boiler » 02 Apr 2023, 09:27

For WatchFolder(), have your monitoring function look for and act only on the specific types of changes that you are interested in.

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How do I monitor a specific file in a directory?

Post by GEV » 02 Apr 2023, 09:45

boiler wrote:
02 Apr 2023, 09:27
For WatchFolder(), have your monitoring function look for and act only on the specific types of changes that you are interested in.
Thanks @boiler,
What I want, is to backup documents automatically, as soon as they change. What is the best way to do that?

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: How do I monitor a specific file in a directory?

Post by flyingDman » 02 Apr 2023, 12:34

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?
Not here (with my script). Only when I edit the file and save.
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
. (This might not work for apps that "lock" the file, in which case you'd have to exit that app first. It works though with .docx files)
Last edited by flyingDman on 02 Apr 2023, 12:44, edited 1 time in total.
14.3 & 1.3.7

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: How do I monitor a specific file in a directory?

Post by mikeyww » 02 Apr 2023, 12:43

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.

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: How do I monitor a specific file in a directory?

Post by GEV » 02 Apr 2023, 13:24

flyingDman wrote:
02 Apr 2023, 12:34
Not here (with my script). Only when I edit the file and save.
If I run this

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
By opening the first document the script is working normally.
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.

User avatar
boiler
Posts: 16771
Joined: 21 Dec 2014, 02:44

Re: How do I monitor a specific file in a directory?

Post by boiler » 02 Apr 2023, 13:49

GEV wrote:
02 Apr 2023, 09:45
Thanks @boiler,
What I want, is to backup documents automatically, as soon as they change. What is the best way to do that?
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.

Post Reply

Return to “Ask for Help (v1)”