powershell - 使用powershell移动文件时如何执行记事本
问题描述
嗨,我是 powershell 新手
目前我正在尝试在 powershell 上编写一个脚本。
监视特定文件夹中的更改 (C:\Users\CELINE\Desktop\Testing4)
如果检测到新文件,它会自动将文件移动到另一个文件夹(C:\Users\CELINE\Desktop\PowerShellChanges2)
自动将移动的文件重命名为“Request.csv”
将新文件移动到文件夹时,在文件名上附加递增数字(例如,Request(1).csv、Request(2).csv、Request(3).csv 等)
移动文件后打开记事本
我现在坚持第 5 点。我不知道为什么在移动文件时,会连续打开多个记事本,但我只想打开一个空记事本
有谁能帮忙吗?
这是我的代码
$watcher = New-Object System.IO.FileSystemWatcher
#Path of folder to watch
$watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
#Action executed when changes detected
$action = {
#SourcePath
$path = $Event.SourceEventArgs.FullPath
$changeType = $Event.SourceEventArgs.ChangeType
$logline = "$(Get-Date), $changeType, $path"
#Destination path
$dest = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request.csv"
#Add Log file to monitor the changes
Add-content "C:\Users\CELINE\Desktop\logtest5.txt" -value $logline
#Move Files
$actionMove = Move-Item -Path $path -Destination $dest
#Append number to filename
If (Test-Path $dest) {
$i = 0
While (Test-Path $dest) {
$i += 1
$renamefile "C:\Users\CELINE\Desktop\PowerShellChanges2\Request($i).csv"
Rename-Item -Path $dest -NewName $renamefile
}
}
#Execute Notepad
$FileExists = Test-Path $dest
If ($FileExists -eq $True) {C:\Windows\notepad.exe}
}
#Event Trigger
Register-ObjectEvent $watcher "Changed" -Action $action
while ($true) {Sleep 0}
解决方案
您可以检查记事本是否已经在运行,如果没有,则启动一个新实例。例如,更改脚本的这一部分:
$FileExists = Test-Path $dest
If ($FileExists -eq $True) {C:\Windows\notepad.exe}
}
类似于:
if ((Test-Path $dest) -and ((Get-Process | Where Id -eq $procId) -eq $null))
{
$procId = (Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru).Id
}
这仅检查您之前从脚本启动的记事本的特定实例,但忽略通过其他方式启动的任何副本。您可以轻松更改它以查找记事本的任何实例。
即使没有进行此更改,我的代码也存在一些问题,导致它停止工作,因此我对其进行了微调。下面的代码应该可以正常工作:
$watcher = New-Object System.IO.FileSystemWatcher
# Path of folder to watch
$watcher.Path = "C:\Users\CELINE\Desktop\Testing4"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$procId = -1
$i = 1
# Action executed when changes detected
$action =
{
# SourcePath
$path = $Event.SourceEventArgs.FullPath
#Destination path
$targetFile = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request.csv"
# Add Log file to monitor the changes
Add-content -Path "C:\Users\CELINE\Desktop\logtest5.txt" `
-value "$(Get-Date), $($Event.SourceEventArgs.ChangeType), $path"
# Move file
Move-Item -Path $path -Destination $targetFile
# Rename file
If (Test-Path $targetFile)
{
$renamefile = "C:\Users\CELINE\Desktop\PowerShellChanges2\Request($i).csv"
Rename-Item -Path $targetFile -NewName $renamefile
}
# Execute Notepad
if ((Test-Path $renamefile) -and ((Get-Process | Where Id -eq $procId) -eq $null))
{
$procId = (Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru).Id
}
$i++
}
# Register event
Register-ObjectEvent $watcher "Changed" -Action $action | Out-Null
# Wait
while ($true)
{
Sleep 0
}
推荐阅读
- python - 关于最小排序算法的建议
- fluentvalidation - FluentValidation 与嵌套类、泛型和 MediatR 管道
- roku - 将自定义视图从缩略图更改为文本 - Roku Scenegraph Developer Extension SGDEX
- python - 测试 SSH 私钥密码(暴力破解)
- flutter - 在 Flutter 中的 TextField 列表中检测焦点
- c++ - 附加到 C++ 数组
- ios - 如何在同一设备上运行 2 个版本的应用程序而不覆盖以前的版本?
- c# - 不能在此范围内声明名为“nextjunction”的本地或参数,因为该名称在封闭的本地范围中用于定义本地
- botframework - 如何根据常见问题知识库动态生成 LUIS 意图、实体、模式、话语
- python-3.x - 矩阵位置