不知行否- $pathSource='D:\SCETC\dataXml'
- $pathCopy='D:\交易失败流水'
-
- $fileType='*603.xml'
- $matchString='"CardLocalFee":"0","CardLocalPayfee":"0"'
-
- if( -not (Test-Path -LiteralPath $pathSource)){cmd /c pause;exit}
- if( -not (Test-Path -LiteralPath $pathCopy)){cmd /c pause;exit}
- $fsw=new-object System.IO.FileSystemWatcher
- $fsw.path=$pathSource
- $fsw.filter=$fileType
-
- $theMain={
- if($args.count){
- $xmlFile=$args[0]
- }else{
- $xmlFile=Get-ChildItem -LiteralPath $pathSource -Filter $fileType |Sort {$_.LastWriteTime} |Select -Last 1 -Wait
- $xmlFile=$xmlFile.fullname
- }
- $xmlContent=[xml](Get-Content -LiteralPath $xmlFile -Encoding "UTF8")
- $r=$xmlContent.datas.data.attribute.value |foreach{$_ -match $matchString}
- if($true -in [object[]]$r){
- $thePath=$pathCopy+'\'+(Get-Date).ToString("yyyy-MM-dd")+'\'
- if( -not (Test-Path -LiteralPath $thePath)){$null=New-Item -Type Directory -Path $thepath}
- write-host "copy $xmlFile to $thePath" -ForegroundColor Green
- Copy-Item -LiteralPath $xmlFile -Destination $thePath
- }else{write-host "not match , do noting" -ForegroundColor Green}
- }
-
- "`n running... `n"
- $theMain.invoke()
-
- $whenCreateDo={
- $evArgs=$event.SourceEventArgs
- $createTime=$event.TimeGenerated
- $msg="{0} was {1} at {2}" -f $evArgs.FullPath,$evArgs.ChangeType,$createTime
- Write-Host $msg -ForegroundColor Yellow
- Start-Sleep -seconds 2
- $t=0
- while($t -lt 8){
- try{
- $s=Get-Content -LiteralPath ($evArgs.FullPath) -Encoding "UTF8"
- $t=128
- }catch{
- $t+=2
- write-host "open the newfile fail (maybe using) , retry $t seconds later" -ForegroundColor Red
- start-sleep -seconds $t
- }
- }
- $theMain.invoke($evArgs.FullPath)
- }
-
- Register-ObjectEvent -InputObject $fsw -EventName Created -Action $whenCreateDo -SourceIdentifier fileCreate
- if(!$fsw.enableraisingevents){$fsw.enableraisingevents=$true}
- "`n press CTRL+C to exit `n"
- try{
- Wait-Event -SourceIdentifier fileCreate
- }finally{
- $fsw.enableraisingevents=$false
- Unregister-Event -SourceIdentifier fileCreate
- $fsw.dispose()
- "`n done `n"
- cmd /c pause
- }
复制代码
|