Board logo

标题: [文件操作] [已解决]bat文件夹变更提示 [打印本页]

作者: LJY4.0    时间: 2022-6-11 20:54     标题: [已解决]bat文件夹变更提示

本帖最后由 LJY4.0 于 2022-6-16 18:33 编辑

就是如果文件夹内文件变更或新增文件发出警告(如果可以拦截操作[move{指拦截时用的指令}])
我想做一个系统主动防御,想问一下怎么做效率才最好,wmic了解不够
作者: idwma    时间: 2022-6-12 14:46

FileSystemWatcher事件
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $fsw=New-Object IO.FileSystemWatcher 'c:\' -Property @{IncludeSubdirectories=1;EnableRaisingEvents=1}
  3. 'Changed','Created'|%{Register-ObjectEvent $fsw -EventName $_ -Action {
  4. write-host '如果c:\文件夹内文件变更或新增文件发出警告'
  5. write-host $event.SourceEventArgs.FullPath
  6. }}
  7. while(1){Wait-Event -Timeout 1}
复制代码

作者: LJY4.0    时间: 2022-6-12 19:24

本帖最后由 LJY4.0 于 2022-6-12 19:32 编辑

回复 2# idwma
他的显示方法是怎样,是显示文件路径吗?
为什么每个文件路径都显示
作者: LJY4.0    时间: 2022-6-12 19:35

看错了,不过能不能改成移到temp,弹窗输出路径,显示Y/N
Y从temp移到原来的路径,N删除
作者: idwma    时间: 2022-6-12 22:07

只监听这个文件夹取不到原路径
按这个方法要取得移动前的路径要转个弯
加个全盘监听删除事件
这样就不友好了再看看有没有别的方式
作者: idwma    时间: 2022-6-13 21:40

回复 4# LJY4.0
  1. #@&cls&powershell "type %~s0|out-string|iex"&pause&exit
  2. $path='c:\temp'
  3. $global:a=@{}
  4. [Environment]::GetLogicalDrives()|%{
  5.     $fsw=New-Object IO.FileSystemWatcher $_ -Property @{IncludeSubdirectories=1;EnableRaisingEvents=1}
  6.     'Created','Deleted'|%{Register-ObjectEvent $fsw -EventName $_ -Action {$a[$event.EventIdentifier]=$event}}
  7. }
  8. while(1){
  9.     Wait-Event -Timeout 1
  10.     $a.clone().keys|?{$a[$_].SourceEventArgs.FullPath -match [regex]::Escape($path) -and $a[$_].SourceEventArgs.ChangeType -eq 'Created'}|%{
  11.         $d=$a[$_].SourceEventArgs.FullPath
  12.         if($d -match '[^\\]*$'){
  13.             $a.clone().keys|?{
  14.                 $a[$_].SourceEventArgs.ChangeType -eq 'Deleted' -and
  15.                 $a[$_].SourceEventArgs.FullPath -match [regex]::Escape($matches[0]) -and
  16.                 $a[$_].SourceEventArgs.FullPath -notmatch [regex]::Escape($path)
  17.             }|%{
  18.                 $s=$a[$_].SourceEventArgs.FullPath
  19.                 $a.remove($_)
  20.             }
  21.             if($s -ne $null -and $d -ne $null){
  22.                 "$s -> $d"
  23.                 $yn=read-host 'Y/N'
  24.                 if($yn -eq 'Y'){move $d $s}elseif($yn -eq 'N'){del $d}
  25.             }
  26.             $a.remove($_)
  27.         }
  28.     }
  29.     rv d,s -ErrorAction SilentlyContinue
  30. }
复制代码

作者: LJY4.0    时间: 2022-6-16 18:39

回复 6# idwma
还有一个问题想问,他监听的路径在哪,我想修改一下




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2