Skip to content

Instantly share code, notes, and snippets.

@Philmist
Last active January 6, 2025 12:08
Show Gist options
  • Save Philmist/ba57a1e1f9d989ffddbe0449e836cb15 to your computer and use it in GitHub Desktop.
Save Philmist/ba57a1e1f9d989ffddbe0449e836cb15 to your computer and use it in GitHub Desktop.
ファイルが変更されるたびにコピーするスクリプト
function Remove-User-Job() {
Param (
[string]$Name = ""
)
if ([string]::IsNullOrEmpty($Name)) {
return
}
Unregister-Event $Name
Remove-Job $Name
return
}
function Start-Changed-Watcher() {
Param (
[string]$Path,
[String]$Filter = "*.*"
)
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $Path
$watcher.Filter = $Filter
$watcher.IncludeSubdirectories = $false
$watcher.InternalBufferSize = 8192
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$watcher.EnableRaisingEvents = $true
$file_copy_block = {
Param($sender, $ev)
Write-Host "DISABLE WATCHER"
$sender.EnableRaisingEvents = $false
Write-Host "DETECT FILE NAME"
$OldName = ($ev.FullPath).ToString()
$dt = (Get-Date -Format "yyyy_MM_dd_HH_mm_ss").ToString()
$NameRegex = $OldName -match "(.+)(\..+)$"
$Name = ($Matches[1]).ToString()
$Ext = ($Matches[2]).ToString()
$NewPath = $Name + "_" + $dt + $Ext
Write-Host $OldName "->" $NewPath
Copy-Item $OldName -Destination $NewPath
Write-Host "ENABLE WATCHER"
$sender.EnableRaisingEvents = $true
}
$si = ("UserFileChanged_" + $Path + $Filter)
$ret = Register-ObjectEvent -InputObject $watcher -EventName Changed -SourceIdentifier $si -Action $file_copy_block
return {
Remove-User-Job $si
}.GetNewClosure()
}
# 使い方 (PowerShellで動きます)
# まずPowerShellの名前空間に関数を取りこみます。
# PS c:\> . .\WatchFileChanged.ps1
# 次にバックアップを保存しておきたいディレクトリに移動します。
# PS c:\> cd c:\shortcuts
# そして、おもむろにファイルの監視を開始します。
# PS c:\shortcuts> $watcher_job = Start-Changed-Watcher (Get-Location) "*.txt"
# そうすると保存するたびに日時が入ったファイルがコピーされます。
# 監視を終了したい時は次を使ってください。
# PS c:\shortcuts> &$watcher_job
# なおこのファイルはZIPで右上からダウンロードできますよ
Param([string]$FileFilter = "*.*")
function Remove-User-Job() {
Param (
[string]$Name = ""
)
if ([string]::IsNullOrEmpty($Name)) {
return
}
Unregister-Event $Name
Remove-Job $Name
return
}
function Start-Changed-Watcher() {
Param (
[string]$Path,
[String]$Filter = "*.*"
)
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $Path
$watcher.Filter = $Filter
$watcher.IncludeSubdirectories = $false
$watcher.InternalBufferSize = 8192
$watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite
$watcher.EnableRaisingEvents = $true
$file_copy_block = {
Param($sender, $ev)
Write-Host "DISABLE WATCHER"
$sender.EnableRaisingEvents = $false
Write-Host "DETECT FILE NAME"
$OldName = ($ev.FullPath).ToString()
$dt = (Get-Date -Format "yyyy_MM_dd_HH_mm_ss").ToString()
$NameRegex = $OldName -match "(.+)(\..+)$"
$Name = ($Matches[1]).ToString()
$Ext = ($Matches[2]).ToString()
$NewPath = $Name + "_" + $dt + $Ext
Write-Host $OldName "->" $NewPath
Copy-Item $OldName -Destination $NewPath
Write-Host "ENABLE WATCHER"
$sender.EnableRaisingEvents = $true
}
$si = ("UserFileChanged_" + $Path + $Filter)
$ret = Register-ObjectEvent -InputObject $watcher -EventName Changed -SourceIdentifier $si -Action $file_copy_block
return {
Remove-User-Job $si
}.GetNewClosure()
}
$watcher_job = Start-Changed-Watcher (Get-Location) $FileFilter
Write-Host "Start Watching... PRESS CTRL-C TO STOP"
try {
while($true) {}
} finally {
&$watcher_job
}
# スクリプトがあるディレクトリにあるファイルを監視して、
# ファイルが変更されたら、日付を付加してコピーします。
# このファイルはZIPで右上からダウンロードできますよ
@Philmist
Copy link
Author

Philmist commented Jun 26, 2017

FIX ME: なぜか2回走る。
FIXED?: 1回になったかも。

@Philmist
Copy link
Author

@Philmist
Copy link
Author

FIX ME: 何で動いているかわからない

@Philmist
Copy link
Author

TODO: 同じディレクトリにファイルを保存するのではなく、違うディレクトリにもファイルを保存させたい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment