How do I monitor a specific file in a directory? Topic is solved
How do I monitor a specific file in a directory?
viewtopic.php?t=76244
teadrinker's code does what I'm trying to go for, however, it monitors the entire directory. I want to store a frequently updated list in a directory with other files that also frequently change, but I only want it to monitor the list.
So say I have G:\. Inside of G:\ I have List1.txt and List2. I only want the script to trigger when List1.txt gets changed and not when List2.txt changes. How would I go about that?
teadrinker's code does what I'm trying to go for, however, it monitors the entire directory. I want to store a frequently updated list in a directory with other files that also frequently change, but I only want it to monitor the list.
So say I have G:\. Inside of G:\ I have List1.txt and List2. I only want the script to trigger when List1.txt gets changed and not when List2.txt changes. How would I go about that?
Re: How do I monitor a specific file in a directory?
How would I go about implementing this in my own script? I want my script to reload every time it detects a change in List1.txt and ignore all other files in said directory.
- flyingDman
- Posts: 2823
- Joined: 29 Sep 2013, 19:01
Re: How do I monitor a specific file in a directory?
If Watchfolder is overkill for 1 file, you can use this:
Code: Select all
settimer, lbl, 5000 ; checks every 5 seconds
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\List1.txt
if (oldtmstmp != tmstmp AND flag)
Msgbox The file has changed!
oldtmstmp := tmstmp, flag := 1
return
14.3 & 1.3.7
Re: How do I monitor a specific file in a directory?
Using WatchFolder:
Code: Select all
#Persistent
#Include WatchFolder.ahk
WatchFolder("G:\", "FileChanged",, 16) ; FILE_NOTIFY_CHANGE_LAST_WRITE = 16
return
FileChanged(folder, changes) {
for each, change in changes
if InStr(change.Name, "\List1.txt")
Reload
}
Re: How do I monitor a specific file in a directory?
This is the approach I'd like to go with. However, I'm getting 2 errors:flyingDman wrote: ↑28 Mar 2023, 21:21If Watchfolder is overkill for 1 file, you can use this:Code: Select all
settimer, lbl, 5000 ; checks every 5 seconds lbl: FileGetTime, tmstmp, %A_ScriptDir%\List1.txt if (oldtmstmp != tmstmp AND flag) Msgbox The file has changed! oldtmstmp := tmstmp, flag := 1 return
Warning: This variable has not been assigned a value.
Specifically: flag (a global variable)
Warning: This variable has not been assigned a value.
Specifically: oldtmstmp (a global variable)
Those are the errors I get when I put your code near the top of the script, and when I put it near the bottom it just doesn't do anything.
Here's the entire script without your code, where should I be placing it/what am I doing wrong? Sorry if I'm being/sounding dumb, I'm not skilled at this.
Code: Select all
settimer,updatedscript,500
#maxthreadsperhotkey 999999999
#noenv
#persistent
#singleinstance force
#warn
sendmode input
setworkingdir %a_scriptdir%
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
menu, tray, add , Pause, pause
menu, tray, add , Exit, exit
menu, tray, icon , AutoCorrect.ico,, 1
menu, tray, nostandard
menu, tray, tip, AutoCorrect
return
pause:
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
exit:
exitapp
#p::
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
#a::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Automatic entry,,,,,,,, :*:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#m::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Manual entry,,,,,,,, ::%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#/::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Mid text manual entry,,,,,,,, :?:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#include g:\List1.txt
updatedscript:
filegetattrib,attribs,%a_scriptfullpath%
ifinstring,attribs,a
{
filesetattrib,-a,%a_scriptfullpath%
sleep,500
reload
}
return
This is the method I'll fall back on if I can't get the other one to work, cheers mate and stay tuned.boiler wrote: Using WatchFolder:Code: Select all
#Persistent #Include WatchFolder.ahk WatchFolder("G:\", "FileChanged",, 16) ; FILE_NOTIFY_CHANGE_LAST_WRITE = 16 return FileChanged(folder, changes) { for each, change in changes if InStr(change.Name, "\List1.txt") Reload }
Re: How do I monitor a specific file in a directory?
Those are not errors, they are just warnings because you have #warn at the top of your script, and flyingDman is aware that they are not previously assigned a value and it is not a problem with his code. You can either leave the #warn directive in your script in case you want it to warn you for other parts of your script and ignore these two particular warnings, or you can remove #warn from the script. Or you could leave the #warn line in place and put these lines at the top of your script:
Code: Select all
flag := ""
oldtmstmp := ""
Re: How do I monitor a specific file in a directory?
I see. If they're not assigned a value, what is the purpose? Also I just realized I phrased my last reply bad and should've just said it doesn't work. Putting his code at the top of the script only gets it to generate those messages from #Warn, the functionality is still missing. I just was eager to include what I thought were "error" messages because it would get me closer to solving why it didn't work.boiler wrote: ↑29 Mar 2023, 14:31Those are not errors, they are just warnings because you have #warn at the top of your script, and flyingDman is aware that they are not previously assigned a value and it is not a problem with his code. You can either leave the #warn directive in your script in case you want it to warn you for other parts of your script and ignore these two particular warnings, or you can remove #warn from the script. Or you could leave the #warn line in place and put these lines at the top of your script:Code: Select all
flag := "" oldtmstmp := ""
Re: How do I monitor a specific file in a directory? Topic is solved
It's not that they're never assigned a value. You can see in the code that they are eventually assigned a value. It's just that they haven't already been assigned a value before the first time their contents are used. It's not a problem in this case because their un-initialized value, which is null, is fine.
You can't just put it all at the top or all at the bottom. You need the part that needs to be executed at the start of the script in the top section (auto-execute section) and the labled subroutine lbl: below the auto-execute section, like this:J0RDAN wrote: ↑ Also I just realized I phrased my last reply bad and should've just said it doesn't work. Putting his code at the top of the script only gets it to generate those messages from #Warn, the functionality is still missing. I just was eager to include what I thought were "error" messages because it would get me closer to solving why it didn't work.
Code: Select all
settimer,updatedscript,500
#maxthreadsperhotkey 999999999
#noenv
#persistent
#singleinstance force
#warn
sendmode input
setworkingdir %a_scriptdir%
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
menu, tray, add , Pause, pause
menu, tray, add , Exit, exit
menu, tray, icon , AutoCorrect.ico,, 1
menu, tray, nostandard
menu, tray, tip, AutoCorrect
settimer, lbl, 5000
return
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\List1.txt
if (oldtmstmp != tmstmp AND flag)
Msgbox The file has changed!
oldtmstmp := tmstmp, flag := 1
return
pause:
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
exit:
exitapp
#p::
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
#a::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Automatic entry,,,,,,,, :*:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#m::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Manual entry,,,,,,,, ::%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#/::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Mid text manual entry,,,,,,,, :?:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#include g:\List1.txt
updatedscript:
filegetattrib,attribs,%a_scriptfullpath%
ifinstring,attribs,a
{
filesetattrib,-a,%a_scriptfullpath%
sleep,500
reload
}
return
- flyingDman
- Posts: 2823
- Joined: 29 Sep 2013, 19:01
Re: How do I monitor a specific file in a directory?
Also add #Persisent at the top of your script (my bad)
edit: did not see @boiler 's response
Code: Select all
#Requires AutoHotkey v1.1.33
#Persistent
#Warn
flag := ""
oldtmstmp := ""
settimer, lbl, 5000
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\List1.txt
if (oldtmstmp != tmstmp AND flag)
Msgbox The file has changed!
oldtmstmp := tmstmp, flag := 1
return
14.3 & 1.3.7
Re: How do I monitor a specific file in a directory?
Code: Select all
settimer,updatedscript,500
#maxthreadsperhotkey 999999999
#noenv
#persistent
#singleinstance force
#warn
sendmode input
setworkingdir %a_scriptdir%
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
menu, tray, add , Pause, pause
menu, tray, add , Exit, exit
menu, tray, icon , AutoCorrect.ico,, 1
menu, tray, nostandard
menu, tray, tip, AutoCorrect
settimer, lbl, 5000
return
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\List1.txt
if (oldtmstmp != tmstmp AND flag)
Msgbox The file has changed!
oldtmstmp := tmstmp, flag := 1
return
pause:
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
exit:
exitapp
#p::
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
#a::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Automatic entry,,,,,,,, :*:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#m::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Manual entry,,,,,,,, ::%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#/::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Mid text manual entry,,,,,,,, :?:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#include g:\List1.txt
updatedscript:
filegetattrib,attribs,%a_scriptfullpath%
ifinstring,attribs,a
{
filesetattrib,-a,%a_scriptfullpath%
sleep,500
reload
}
return
@flyingDman I am using AHK v1.1.36.02, does that mean it won't work?
Re: How do I monitor a specific file in a directory?
Hm, I see. Well, do you have any ideas as to why it's not working then? Like I said I downloaded the version from your post and I haven't seen it trigger any message boxes.
Re: How do I monitor a specific file in a directory?
You ran the exact the script I posted and nothing else in that script (with WatchFolder.ahk as a separate file in the same folder as your script)? And then you manually changed the List1.txt file and saved it and it didn't show a MsgBox?
Re: How do I monitor a specific file in a directory?
Pretty much. I didn't get the original WatchFolder.ahk if that's what you're talking about, I simply have the exact script you posted, this one;
Code: Select all
settimer,updatedscript,500
#maxthreadsperhotkey 999999999
#noenv
#persistent
#singleinstance force
#warn
sendmode input
setworkingdir %a_scriptdir%
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
menu, tray, add , Pause, pause
menu, tray, add , Exit, exit
menu, tray, icon , AutoCorrect.ico,, 1
menu, tray, nostandard
menu, tray, tip, AutoCorrect
settimer, lbl, 5000
return
lbl:
FileGetTime, tmstmp, %A_ScriptDir%\List1.txt
if (oldtmstmp != tmstmp AND flag)
Msgbox The file has changed!
oldtmstmp := tmstmp, flag := 1
return
pause:
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
exit:
exitapp
#p::
suspend, toggle
if (a_issuspended)
menu, tray, icon , AutoCorrect pause.ico
else
menu, tray, icon , AutoCorrect.ico
return
#a::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Automatic entry,,,,,,,, :*:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#m::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Manual entry,,,,,,,, ::%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#/::
autotrim on
clipboardold = %clipboardall%
clipboard =
send ^c
clipwait 1
if errorlevel
return
stringreplace, hotstring, clipboard, ``, ````, all
stringreplace, hotstring, hotstring, `r`n, ``r, all
stringreplace, hotstring, hotstring, `n, ``r, all
stringreplace, hotstring, hotstring, %a_tab%, ``t, all
stringreplace, hotstring, hotstring, `;, ```;, all
clipboard = %clipboardold%
inputbox, hotstring, AutoCorrect, Mid text manual entry,,,,,,,, :?:%hotstring%::
if errorlevel <> 0
return
fileappend, `n%hotstring%, g:\List1.txt
reload
#include g:\List1.txt
updatedscript:
filegetattrib,attribs,%a_scriptfullpath%
ifinstring,attribs,a
{
filesetattrib,-a,%a_scriptfullpath%
sleep,500
reload
}
return
Untitled.ahk and it's other files pictured in the screenshot are in a folder on the Desktop.
Re: How do I monitor a specific file in a directory?
That’s not what I meant. I meant the WatchFolder-based script. I only showed you how to add flyingDman’s script to what you had. Make sure you know how to get either flyingDman’s original script or the one I posted to work before trying to incorporate it into your larger script.
Re: How do I monitor a specific file in a directory?
Oh, my mistake. I've only been working with flyingDman's script this entire time which I can't seem to make work. Even the version you posted where you took the code I posted and flyingDman's code and combined them doesn't show a MsgBox. Does it work on your system? I'm running Windows 10.boiler wrote: ↑29 Mar 2023, 19:43That’s not what I meant. I meant the WatchFolder-based script. I only showed you how to add flyingDman’s script to what you had. Make sure you know how to get either flyingDman’s original script or the one I posted to work before trying to incorporate it into your larger script.
Re: How do I monitor a specific file in a directory?
I'm not trying to get your full code working. That's not the way to troubleshoot. As I said before:
When you can't figure out why something's not working, the first step isn't to try to get the full-length, more complex code to work. It's to isolate the part you're adding and make sure you get that to work on its own. I still haven't heard you say that you ran either my code or flyingDman's code and produced the expected results.
Re: How do I monitor a specific file in a directory?
Good advice. I tried running flyingDman's code in a blank .ahk all by itself and the H icon that pops up in the tray on the bottom right came up and went away in a flash, instantly closing again.boiler wrote: ↑30 Mar 2023, 15:25I'm not trying to get your full code working. That's not the way to troubleshoot. As I said before:
When you can't figure out why something's not working, the first step isn't to try to get the full-length, more complex code to work. It's to isolate the part you're adding and make sure you get that to work on its own. I still haven't heard you say that you ran either my code or flyingDman's code and produced the expected results.
- flyingDman
- Posts: 2823
- Joined: 29 Sep 2013, 19:01
Re: How do I monitor a specific file in a directory?
Did you insert the #persistent at the top of your script?
14.3 & 1.3.7