-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUse.ps1
More file actions
472 lines (411 loc) · 14.9 KB
/
Copy pathFileUse.ps1
File metadata and controls
472 lines (411 loc) · 14.9 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<#
.SYNOPSIS
Personal file-use index: path + DTS + open count.
Watches the Recent Items folder (SHAddToRecentDocs), NOT the whole disk.
.DESCRIPTION
Windows already writes a .lnk into %APPDATA%\Microsoft\Windows\Recent
when Explorer or a common file dialog opens a file on purpose.
This script treats that as the "I clicked it" signal, upserts SQLite:
C:\Users\<you>\FileUse\fileuse.sqlite
It does NOT FileSystemWatcher your Documents/Downloads trees.
That would log Defender, Search, OneDrive, and thumbnail noise —
i.e. the event log you do not want a copy of.
.EXAMPLE
.\FileUse.ps1 -Init
.\FileUse.ps1 -Harvest
.\FileUse.ps1 -Watch
.\FileUse.ps1 -Top 40
.\FileUse.ps1 -Top 40 -Frecency
.\FileUse.ps1 -InstallTask
.\FileUse.ps1 -UninstallTask
#>
[CmdletBinding()]
param(
[switch]$Init,
[switch]$Harvest,
[switch]$Watch,
[int]$Top = 0,
[switch]$Frecency,
[string]$Like,
[switch]$InstallTask,
[switch]$UninstallTask,
[int]$PollSeconds = 2,
[string]$DbPath
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
if (-not $DbPath) {
$root = Join-Path $env:USERPROFILE 'FileUse'
$DbPath = Join-Path $root 'fileuse.sqlite'
}
$root = Split-Path -Parent $DbPath
$recentDir = Join-Path $env:APPDATA 'Microsoft\Windows\Recent'
$logPath = Join-Path $root 'fileuse.log'
# ---------------------------------------------------------------------------
# SQLite helpers (PSSQLite if present; else sqlite3.exe)
# ---------------------------------------------------------------------------
function Test-Pssqlite {
return [bool](Get-Module -ListAvailable -Name PSSQLite)
}
function Ensure-SqliteTool {
if (Test-Pssqlite) {
Import-Module PSSQLite -ErrorAction Stop
return 'pssqlite'
}
$sqlite3 = Get-Command sqlite3.exe -ErrorAction SilentlyContinue
if ($sqlite3) { return 'sqlite3' }
throw @"
No SQLite backend found.
Install one of:
Install-Module PSSQLite -Scope CurrentUser
winget install SQLite.SQLite
Then re-run.
"@
}
$script:SqliteBackend = $null
function Invoke-FileUseSql {
param(
[Parameter(Mandatory)][string]$Query,
[hashtable]$SqlParameters
)
if (-not $script:SqliteBackend) { $script:SqliteBackend = Ensure-SqliteTool }
if ($script:SqliteBackend -eq 'pssqlite') {
if ($SqlParameters) {
return Invoke-SqliteQuery -DataSource $DbPath -Query $Query -SqlParameters $SqlParameters
}
return Invoke-SqliteQuery -DataSource $DbPath -Query $Query
}
# sqlite3.exe fallback — no parameterized queries; sanitize single quotes
$q = $Query
if ($SqlParameters) {
foreach ($k in $SqlParameters.Keys) {
$val = [string]$SqlParameters[$k]
$val = $val.Replace("'", "''")
$q = $q -replace "@$k\b", "'$val'"
}
}
$raw = & sqlite3.exe -header -csv $DbPath $q 2>&1
if ($LASTEXITCODE -ne 0) { throw "sqlite3 failed: $raw" }
if (-not $raw) { return @() }
return $raw | ConvertFrom-Csv
}
function Write-FileUseLog {
param([string]$Message)
$line = '{0:yyyy-MM-dd HH:mm:ss} {1}' -f (Get-Date), $Message
try {
Add-Content -Path $logPath -Value $line -Encoding UTF8
} catch { }
}
# ---------------------------------------------------------------------------
# Schema
# ---------------------------------------------------------------------------
function Initialize-FileUseDb {
if (-not (Test-Path $root)) {
New-Item -ItemType Directory -Path $root -Force | Out-Null
}
$ddl = @'
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
CREATE TABLE IF NOT EXISTS files (
path TEXT PRIMARY KEY COLLATE NOCASE,
ext TEXT,
first_seen TEXT NOT NULL,
last_seen TEXT NOT NULL,
open_count INTEGER NOT NULL DEFAULT 0,
last_lnk_mtime INTEGER,
last_size INTEGER
);
CREATE TABLE IF NOT EXISTS opens (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL COLLATE NOCASE,
opened_at TEXT NOT NULL,
source TEXT NOT NULL,
lnk_mtime INTEGER
);
CREATE INDEX IF NOT EXISTS ix_files_count ON files(open_count DESC);
CREATE INDEX IF NOT EXISTS ix_files_last ON files(last_seen DESC);
CREATE INDEX IF NOT EXISTS ix_opens_dts ON opens(opened_at DESC);
CREATE INDEX IF NOT EXISTS ix_opens_path ON opens(path);
CREATE UNIQUE INDEX IF NOT EXISTS ix_opens_lnk
ON opens(path, lnk_mtime)
WHERE lnk_mtime > 0;
'@
if (-not $script:SqliteBackend) { $script:SqliteBackend = Ensure-SqliteTool }
if ($script:SqliteBackend -eq 'pssqlite') {
Invoke-SqliteQuery -DataSource $DbPath -Query $ddl | Out-Null
} else {
$ddl | & sqlite3.exe $DbPath
if ($LASTEXITCODE -ne 0) { throw 'sqlite3 schema init failed' }
}
Write-FileUseLog "Initialized $DbPath"
}
# ---------------------------------------------------------------------------
# Resolve a Recent .lnk to a real path
# ---------------------------------------------------------------------------
function Resolve-RecentLnk {
param([string]$LnkPath)
try {
$sh = New-Object -ComObject WScript.Shell
$sc = $sh.CreateShortcut($LnkPath)
$target = [string]$sc.TargetPath
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($sc)
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($sh)
if ([string]::IsNullOrWhiteSpace($target)) { return $null }
return $target
} catch {
return $null
}
}
# ---------------------------------------------------------------------------
# Record one purposeful open
# ---------------------------------------------------------------------------
function Add-FileUseOpen {
param(
[Parameter(Mandatory)][string]$Path,
[datetime]$When = $(Get-Date),
[string]$Source = 'recent-lnk',
[int64]$LnkMtime = 0,
[int64]$Size = 0
)
$pathNorm = $Path.Trim()
if (-not $pathNorm) { return $false }
$ext = [System.IO.Path]::GetExtension($pathNorm).TrimStart('.').ToLowerInvariant()
$dts = $When.ToString('yyyy-MM-dd HH:mm:ss')
# Skip if we already recorded this exact shortcut revision for this path.
# Checking the event ledger (rather than files.last_lnk_mtime) also handles
# two differently named Recent shortcuts that resolve to the same target.
if ($LnkMtime -gt 0) {
$dup = Invoke-FileUseSql -Query 'SELECT 1 AS x FROM opens WHERE path = @p AND lnk_mtime = @m LIMIT 1' -SqlParameters @{ p = $pathNorm; m = $LnkMtime }
if ($dup) { return $false }
}
Invoke-FileUseSql -Query @'
INSERT INTO files (path, ext, first_seen, last_seen, open_count, last_lnk_mtime, last_size)
VALUES (@p, @e, @d, @d, 1, @m, @s)
ON CONFLICT(path) DO UPDATE SET
last_seen = excluded.last_seen,
open_count = open_count + 1,
last_lnk_mtime = excluded.last_lnk_mtime,
last_size = COALESCE(excluded.last_size, last_size),
ext = COALESCE(excluded.ext, ext);
'@ -SqlParameters @{
p = $pathNorm
e = $ext
d = $dts
m = $LnkMtime
s = $Size
} | Out-Null
Invoke-FileUseSql -Query @'
INSERT INTO opens (path, opened_at, source, lnk_mtime)
VALUES (@p, @d, @src, @m);
'@ -SqlParameters @{
p = $pathNorm
d = $dts
src = $Source
m = $LnkMtime
} | Out-Null
return $true
}
# ---------------------------------------------------------------------------
# Harvest every .lnk currently in Recent\
# ---------------------------------------------------------------------------
function Invoke-FileUseHarvest {
if (-not (Test-Path $recentDir)) {
Write-Warning "Recent folder not found: $recentDir"
return 0
}
$added = 0
Get-ChildItem -LiteralPath $recentDir -Filter '*.lnk' -File -ErrorAction SilentlyContinue |
ForEach-Object {
$target = Resolve-RecentLnk -LnkPath $_.FullName
if (-not $target) { return }
$mtime = [int64]($_.LastWriteTimeUtc.Ticks)
$size = 0
if (Test-Path -LiteralPath $target) {
try { $size = [int64](Get-Item -LiteralPath $target -ErrorAction Stop).Length } catch { }
}
if (Add-FileUseOpen -Path $target -When $_.LastWriteTime -Source 'recent-lnk' -LnkMtime $mtime -Size $size) {
$added++
}
}
Write-FileUseLog "Harvest added $added new opens from $recentDir"
return $added
}
# ---------------------------------------------------------------------------
# Live watcher — Recent\ only
# ---------------------------------------------------------------------------
function Start-FileUseWatch {
if (-not (Test-Path $recentDir)) {
throw "Recent folder not found: $recentDir"
}
Write-Host "Watching $recentDir"
Write-Host "Database $DbPath"
Write-Host "Ctrl+C to stop."
$fsw = New-Object System.IO.FileSystemWatcher
$fsw.Path = $recentDir
$fsw.Filter = '*.lnk'
$fsw.IncludeSubdirectories = $false
$fsw.NotifyFilter = [IO.NotifyFilters]::FileName -bor [IO.NotifyFilters]::LastWrite
$fsw.EnableRaisingEvents = $true
$handler = {
$name = $Event.SourceEventArgs.Name
$full = $Event.SourceEventArgs.FullPath
if (-not $name -or -not $name.EndsWith('.lnk', [StringComparison]::OrdinalIgnoreCase)) { return }
# DestList compound files live in subfolders we are not watching,
# but skip anything that is not a real shortcut just in case.
Start-Sleep -Milliseconds 150 # let Explorer finish writing the lnk
try {
$item = Get-Item -LiteralPath $full -ErrorAction Stop
$target = Resolve-RecentLnk -LnkPath $full
if (-not $target) { return }
$mtime = [int64]$item.LastWriteTimeUtc.Ticks
$size = 0
if (Test-Path -LiteralPath $target) {
try { $size = [int64](Get-Item -LiteralPath $target).Length } catch { }
}
if (Add-FileUseOpen -Path $target -When $item.LastWriteTime -Source 'recent-lnk' -LnkMtime $mtime -Size $size) {
Write-Host ("{0:HH:mm:ss} + {1}" -f (Get-Date), $target)
Write-FileUseLog "open $target"
}
} catch {
Write-FileUseLog "watch error $($_.Exception.Message)"
}
}
$subs = @(
Register-ObjectEvent $fsw Created -Action $handler
Register-ObjectEvent $fsw Changed -Action $handler
Register-ObjectEvent $fsw Renamed -Action $handler
)
try {
while ($true) { Start-Sleep -Seconds $PollSeconds }
} finally {
$subs | ForEach-Object { Unregister-Event -SourceIdentifier $_.Name -ErrorAction SilentlyContinue }
$fsw.EnableRaisingEvents = $false
$fsw.Dispose()
}
}
# ---------------------------------------------------------------------------
# Queries
# ---------------------------------------------------------------------------
function Get-FileUseTop {
param(
[int]$N = 40,
[switch]$UseFrecency,
[string]$Pattern
)
$where = ''
$parms = @{ n = $N }
if ($Pattern) {
$where = 'WHERE path LIKE @pat'
$parms.pat = $Pattern
}
if ($UseFrecency) {
# Recency-weighted count: recent opens count more.
# Half-life ~14 days. Pure SQL so he can tweak the constant.
$sql = @"
SELECT
f.path,
f.ext,
f.open_count,
f.first_seen,
f.last_seen,
ROUND(
(SELECT COALESCE(SUM(
EXP(-0.0495 * (JULIANDAY('now','localtime') - JULIANDAY(o.opened_at)))
), 0)
FROM opens o WHERE o.path = f.path)
, 3) AS frecency
FROM files f
$where
ORDER BY frecency DESC, f.open_count DESC
LIMIT @n;
"@
} else {
$sql = @"
SELECT path, ext, open_count, first_seen, last_seen
FROM files
$where
ORDER BY last_seen DESC, open_count DESC
LIMIT @n;
"@
}
Invoke-FileUseSql -Query $sql -SqlParameters $parms
}
# ---------------------------------------------------------------------------
# Scheduled task — harvest every 2 minutes in the user session
# ---------------------------------------------------------------------------
function Install-FileUseTask {
$scriptPath = $PSCommandPath
if (-not $scriptPath) { $scriptPath = $MyInvocation.MyCommand.Path }
if (-not $scriptPath) { throw 'Cannot locate FileUse.ps1 to register the task.' }
$hostExe = (Get-Process -Id $PID).Path
if (-not $hostExe) { throw 'Cannot locate the current PowerShell executable.' }
$action = New-ScheduledTaskAction -Execute $hostExe -Argument (
"-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$scriptPath`" -Harvest"
)
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) `
-RepetitionInterval (New-TimeSpan -Minutes 2) `
-RepetitionDuration ([TimeSpan]::MaxValue)
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries `
-StartWhenAvailable -MultipleInstances IgnoreNew
$principal = New-ScheduledTaskPrincipal -UserId $env:USERNAME -LogonType Interactive -RunLevel Limited
Register-ScheduledTask -TaskName 'FileUse-Harvest' -Action $action -Trigger $trigger `
-Settings $settings -Principal $principal -Force | Out-Null
Write-Host "Registered scheduled task FileUse-Harvest (every 2 min)."
Write-FileUseLog 'Installed scheduled task FileUse-Harvest'
}
function Uninstall-FileUseTask {
Unregister-ScheduledTask -TaskName 'FileUse-Harvest' -Confirm:$false -ErrorAction SilentlyContinue
Write-Host 'Removed scheduled task FileUse-Harvest.'
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
$did = $false
if ($Init -or $Harvest -or $Watch -or $Top -gt 0 -or $InstallTask) {
Initialize-FileUseDb
}
if ($Init) {
Write-Host "Database ready: $DbPath"
$did = $true
}
if ($Harvest) {
$n = Invoke-FileUseHarvest
Write-Host "Harvested $n new open(s)."
$did = $true
}
if ($InstallTask) {
Install-FileUseTask
$did = $true
}
if ($UninstallTask) {
Uninstall-FileUseTask
$did = $true
}
if ($Top -gt 0) {
$pat = $null
if ($Like) { $pat = $Like }
Get-FileUseTop -N $Top -UseFrecency:$Frecency -Pattern $pat |
Format-Table -AutoSize
$did = $true
}
if ($Watch) {
$did = $true
Start-FileUseWatch
}
if (-not $did) {
Write-Host @"
FileUse — personal open-count index
.\FileUse.ps1 -Init
.\FileUse.ps1 -Harvest
.\FileUse.ps1 -Watch
.\FileUse.ps1 -Top 40
.\FileUse.ps1 -Top 40 -Frecency
.\FileUse.ps1 -Top 40 -Like '%\Documents\%'
.\FileUse.ps1 -InstallTask
Database: $DbPath
Sensor: $recentDir\*.lnk (not the whole disk)
"@
}