运行 powershell 时无法将已添加书签的行复制到剪贴板/新页面 - 命令以在记事本中为数据行添加书签++

问题描述 投票:0回答:1

我有一个 PowerShell 脚本,我打算用它在 Notepad++ 中打开特定的 csv 文件,然后搜索 -> 基于列 [Location] = 22102 之一的书签行并将所有书签行复制到新工作表。

我仍处于此过程的开始阶段,一直在寻找 PowerShell 的方法:

  1. 在记事本++中打开csv文件
  2. 位置列 = 22102 的书签行
  3. 然后,打开功能区中的“搜索”选项卡以“复制书签行”
  4. 将书签行粘贴到新工作表中

我能够执行步骤 1 和 2,我正在尝试引入 ("^9") 作为复制书签行的快捷方式,但我仍在努力执行步骤 3 复制书签行并将其粘贴到新工作表中.有人可以帮忙吗?

代码:

 $file = "C:\Files\Timings.csv"
 $notepad = "C:\Program Files\Notepad++\notepad++.exe"

 # Open the file in Notepad++
 Start-Process notepad++ $file

 # Wait for Notepad++ to open and become active
 Start-Sleep -Milliseconds 500

 Add-Type -AssemblyName System.Windows.Forms

 # Find the rows with Location 22102 and bookmark them
 Add-Type -AssemblyName Microsoft.VisualBasic
 [Microsoft.VisualBasic.Interaction]::AppActivate("Notepad++")
 [System.Windows.Forms.SendKeys]::SendWait("^f")
 [System.Windows.Forms.SendKeys]::SendWait("22102")
 [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
 [System.Windows.Forms.SendKeys]::SendWait("^m")
 [System.Windows.Forms.SendKeys]::SendWait("{TAB}")
 [System.Windows.Forms.SendKeys]::SendWait("{ADD}")
 [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
 
 # Get the bookmarked lines and copy them to the clipboard
 [System.Windows.Forms.SendKeys]::SendWait("^9")

一旦我复制了书签行,我希望打开一个新的记事本++文件并将它们全部粘贴到新的记事本++文档中:

 # Open a new Notepad++ document and paste the bookmarked lines
 Start-Process notepad++
 [Microsoft.VisualBasic.Interaction]::AppActivate("Notepad++")
 Start-Sleep -Milliseconds 500
 [System.Windows.Forms.SendKeys]::SendWait("^n")
 [System.Windows.Forms.SendKeys]::SendWait("^v")

 # Output bookmarked lines to a new CSV file
 $outputFile = $file -replace "\.csv$", "-modified.csv"
 $bookmarkedLines | Out-File -FilePath $outputFile -Encoding UTF8

我正在处理的数据示例:

Timing Event Sequence,Date,Operator,Operator Name,Operator Full Name,Operator Holding Company,Operator Type,Operator Franchised,TSC,TSC Description,TSC PfPI Flag,T Schedule Type,T ID,Location,Timing Event,Planned WTT Time,Actual,WTT Lateness,Delay
"0001","02/03/2023","HA","Manufacturing","Manufacturing","Ab CO.","C","FRANCHISED","9003","Gotham","Y","LTP","L27","01100","Originate","09:08","09:08","0","2"
"1002","02/03/2023","HA","Manufacturing","Manufacturing","Ab CO.","C","FRANCHISED","9003","Gotham","Y","LTP","L27","22012","Pass","09:50","","","2"
"2003","02/03/2023","HA","Manufacturing","Manufacturing","Ab CO.","C","FRANCHISED","9003","Gotham","Y","LTP","L27","22012","Pass","09:51","","","2"
powershell notepad++ sendkeys
1个回答
0
投票

为什么要通过记事本++来做?

要创建一个新的 csv 文件,该文件将仅包含“位置”列中值为“22102”的行,它很简单:

$file    = "C:\Files\Timings.csv"
$outFile = $file -replace '\.csv$', '-modified.csv'
Import-Csv -Path $file -Encoding UTF8 | 
Where-Object { $_.Location -eq '22102' } |   # filter the data on column Location
Export-Csv -Path $outFile -NoTypeInformation -Encoding UTF8

在不指定其他字符的情况下,Import/Export-Csv cmdlet 假定 comma 作为字段分隔符。
如果在您的文件中使用不同的字符,请将

-Delimiter <yourChar>
添加到这些 cmdlet


例子

如果您输入的 csv 文件看起来像这样:

"User","Location"
"Jim","12345"
"John","22102"
"James","22102"

然后运行上面的代码将创建一个新的 csv 文件,其中包含:

"User","Location"
"John","22102"
"James","22102"
© www.soinside.com 2019 - 2024. All rights reserved.