-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadsFileSync.ps1
More file actions
68 lines (54 loc) · 2.49 KB
/
DownloadsFileSync.ps1
File metadata and controls
68 lines (54 loc) · 2.49 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
$sourcePath = "C:\Users\jonas\Downloads"
$destinationPath = "\\synology-ds720\DS720\Software"
$fileExtensions = @("exe", "msi", "iso", "img")
$pattern = "[A-Za-z]+"
$timeSpan = New-TimeSpan -Seconds 600
while ($true) {
$now = Get-Date
$counter = 0
$files = Get-ChildItem -Path $sourcePath -File | Where-Object {
$_.LastWriteTime -gt $now.Subtract($timeSpan) -and
$fileExtensions -contains $_.Extension.TrimStart('.').ToLower()
}
foreach ($file in $files) {
$extensionFolder = Join-Path -Path $destinationPath -ChildPath $file.Extension.ToLower()
if(-not (Test-Path -Path $extensionFolder))
{
New-Item -ItemType Directory -Path $extensionFolder
}
if ($file.BaseName -match $pattern)
{
$subfolder = [regex]::Match($file.Name, $pattern).Value
$destFolder = Join-Path -Path $extensionFolder -ChildPath $subfolder
if (-not (Test-Path -Path $destFolder))
{
New-Item -ItemType Directory -Path $destFolder
}
}
else
{
$destFolder = $extensionFolder
}
Copy-Item -Path $file.FullName -Destination "$($destFolder)\$($now.ToString("yyyy-MM-dd"))_$($file.Name)" -Force
$counter++
}
if($files.Count -gt 0)
{
#NOTIFICATION
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").Item(0).AppendChild($toastXml.CreateTextNode("Files synchronized")) > $null
$toastXml.GetElementsByTagName("text").Item(1).AppendChild($toastXml.CreateTextNode("$($counter) of $($files.Count)")) > $null
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "DownloadsFileSync"
$toast.Group = "DownloadsFileSync"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("DownloadsFileSync")
$notifier.Show($toast);
}
#START-SLEEP
Start-Sleep -Seconds $timeSpan.TotalSeconds
}