From d9a63e25fd149c5c2b6767d0117f64a745fb6bca Mon Sep 17 00:00:00 2001 From: "jonas@geiger-natur.de" Date: Mon, 24 Jun 2024 22:21:47 +0200 Subject: [PATCH 1/2] powershell file added --- DownloadsFileSync.ps1 | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 DownloadsFileSync.ps1 diff --git a/DownloadsFileSync.ps1 b/DownloadsFileSync.ps1 new file mode 100644 index 0000000..51d8cee --- /dev/null +++ b/DownloadsFileSync.ps1 @@ -0,0 +1,42 @@ +$sourcePath = "Downloads" + +$destinationPath = "\\YourServer\YourShare" +$fileExtensions = @("exe", "msi", "iso", "img") +$pattern = "[A-Za-z]+" +$timeSpan = New-TimeSpan -Minutes 600 + +while ($true) { + $now = Get-Date + + $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 + } + + Start-Sleep -Seconds 600 +} \ No newline at end of file From 34b1ac0a5f10bf43ff0fc93a6e3fe77dcee3821f Mon Sep 17 00:00:00 2001 From: "jonas@geiger-natur.de" Date: Thu, 27 Jun 2024 21:13:13 +0200 Subject: [PATCH 2/2] sourcePath and copy-item edited --- DownloadsFileSync.ps1 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/DownloadsFileSync.ps1 b/DownloadsFileSync.ps1 index 51d8cee..0292df6 100644 --- a/DownloadsFileSync.ps1 +++ b/DownloadsFileSync.ps1 @@ -1,5 +1,4 @@ -$sourcePath = "Downloads" - +$sourcePath = "C:\Users\YourUsername\Downloads" $destinationPath = "\\YourServer\YourShare" $fileExtensions = @("exe", "msi", "iso", "img") $pattern = "[A-Za-z]+" @@ -35,7 +34,7 @@ while ($true) { $destFolder = $extensionFolder } - Copy-Item -Path $file.FullName -Destination "$($destFolder)\$($now.ToString("yyyy-mm-dd"))_$($file.Name)" -Force + Copy-Item -Path $file.FullName -Destination "$($destFolder)\$($now.ToString("yyyy-MM-dd"))_$($file.Name)" -Force } Start-Sleep -Seconds 600