-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFolder_Watcher.bat
More file actions
74 lines (63 loc) · 2.95 KB
/
Folder_Watcher.bat
File metadata and controls
74 lines (63 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
:: Author: Scott Grivner
:: Website: linktr.ee/scottgriv
@echo off
setlocal EnableExtensions EnableDelayedExpansion
REM ================= CONFIG =================
set "WATCH_DIR=C:\FolderWatcher\Watch"
set "LOG_FILE=C:\FolderWatcher\Logs\Watch-Folder-Log.log"
set "STATE_FILE=C:\FolderWatcher\Logs\Watch-Folder-State.tsv"
set "INTERVAL_SECONDS=5"
REM =========================================
if not exist "%WATCH_DIR%" exit /b 2
for %%I in ("%LOG_FILE%") do (
if not exist "%%~dpI" mkdir "%%~dpI" >nul 2>&1
)
echo [%date% %time%] START watching "%WATCH_DIR%" (poll %INTERVAL_SECONDS%s)>>"%LOG_FILE%"
REM Snapshot format: fullpath<TAB>size<TAB>lastwrite
:loop
> "%STATE_FILE%.new" (
for /f "delims=" %%F in ('dir /s /b /a:-d "%WATCH_DIR%" 2^>nul') do (
for %%A in ("%%F") do (
echo %%~fF %%~zA %%~tA
)
)
)
REM First run baseline
if not exist "%STATE_FILE%" (
move /y "%STATE_FILE%.new" "%STATE_FILE%" >nul
timeout /t %INTERVAL_SECONDS% /nobreak >nul
goto loop
)
REM ------------------ CREATED ------------------
for /f "usebackq delims=" %%P in (`
powershell -NoProfile -Command ^
"$old = @(if(Test-Path -LiteralPath '%STATE_FILE%'){ Get-Content -LiteralPath '%STATE_FILE%' } else { @() }) | %%{ ($_ -split \"\t\")[0] };" ^
"$new = @(if(Test-Path -LiteralPath '%STATE_FILE%.new'){ Get-Content -LiteralPath '%STATE_FILE%.new' } else { @() }) | %%{ ($_ -split \"\t\")[0] };" ^
"Compare-Object -ReferenceObject $old -DifferenceObject $new -PassThru | ?{ $_.SideIndicator -eq '=>' }" ^
2^>nul
`) do (
echo [%date% %time%] CREATED %%P>>"%LOG_FILE%"
)
REM ------------------ DELETED ------------------
for /f "usebackq delims=" %%P in (`
powershell -NoProfile -Command ^
"$old = @(if(Test-Path -LiteralPath '%STATE_FILE%'){ Get-Content -LiteralPath '%STATE_FILE%' } else { @() }) | %%{ ($_ -split \"\t\")[0] };" ^
"$new = @(if(Test-Path -LiteralPath '%STATE_FILE%.new'){ Get-Content -LiteralPath '%STATE_FILE%.new' } else { @() }) | %%{ ($_ -split \"\t\")[0] };" ^
"Compare-Object -ReferenceObject $old -DifferenceObject $new -PassThru | ?{ $_.SideIndicator -eq '<=' }" ^
2^>nul
`) do (
echo [%date% %time%] DELETED %%P>>"%LOG_FILE%"
)
REM ------------------ MODIFIED ------------------
for /f "usebackq delims=" %%P in (`
powershell -NoProfile -Command ^
"$old=@{}; if(Test-Path -LiteralPath '%STATE_FILE%'){ Get-Content -LiteralPath '%STATE_FILE%' | %%{ $s=$_ -split \"\t\"; if($s.Length -ge 3){ $old[$s[0]]=$s[1]+'|'+$s[2] } } };" ^
"$new=@{}; if(Test-Path -LiteralPath '%STATE_FILE%.new'){ Get-Content -LiteralPath '%STATE_FILE%.new' | %%{ $s=$_ -split \"\t\"; if($s.Length -ge 3){ $new[$s[0]]=$s[1]+'|'+$s[2] } } };" ^
"$new.Keys | %%{ if($old.ContainsKey($_) -and $old[$_] -ne $new[$_]){ $_ } }" ^
2^>nul
`) do (
echo [%date% %time%] MODIFIED %%P>>"%LOG_FILE%"
)
move /y "%STATE_FILE%.new" "%STATE_FILE%" >nul
timeout /t %INTERVAL_SECONDS% /nobreak >nul
goto loop