从PowerShell中注册的事件输出中拆分字符串

时间:2018-05-23 18:26:22

标签: powershell filesystemwatcher

我刚开始使用IO.FIleSystemWatcher。我当前的代码工作并提醒我在所需位置创建的文件,但是我想管出一些变量并拆分字符串。我无法让分割部分发挥作用。

功能代码:

$folder = 'D:\Output'
$filter = '*.jpg'

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green
Out-File D:\Output\scans\$name.txt    
}

$name的格式总是如此' file.name.fn_xxx.jpg'我想用split来提取文件'来自$name,例如:

$name.split('.')[0]

然而,就我所知,这没有任何作用。例如,我可以输出一个$name作为文件名的文件,但如果我尝试拆分它,那么首先没有输出。

非功能性代码:

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green

$name = $name.split('.')[0]    
Out-File D:\Output\scans\$name.txt    
}

这有点过头了,所以对任何建议或建议表示赞赏。

由于

0 个答案:

没有答案