在文本文件中查找图片文件的路径,将文件路径交给poshgram

Find the path of an image file in a text file and hand over the file path to poshgram

背景:我正在使用文件和文件夹观察器“DiskPulse”(Win 10) 来识别几个文件夹中的新图像文件 (.png)。 DiskPulse 保存一个新的文本文件(C:\diskpulse.txt) 包含图像文件路径,并调用 powershell (ps) 脚本将新文件发送到电报。我正在使用 poshgram 模块将新文件发送到电报。

问题一:
要发送新文件,需要首先从文件和文件夹 wather DiskPulse (C:\diskpulse.txt)) 存放的文本文件中识别其文件路径和名称。我只知道文件扩展名 (*.png) 和新图像文件的文件路径 (C:\*) 的开头。此外,我不确定是否应该使用 -SimpleMatch 或 -Pattern 进行搜索。diskpulse.txt 的内容如下所示:

"03-Oct-2020 11:38:04 Modified 29.54 KB administrator C:\local\files\snapshots\nameofimagefile.png"

问题2:
识别出的文件路径需要交给poshgram($photo变量指定poshgram的文件路径)

这是我目前想出的 ps 文件的代码:

Select-String -Path C:\diskpulse.txt -SimpleMatch "*.png"|select FileName

$token = 
$chat_id = 
$photo = here, the file path from the search should be inserted
Send-TelegramLocalPhoto -BotToken $token -ChatID $chat_id -PhotoPath $photo

非常感谢任何指点,我在这里的学习道路很陡峭。


Doug 发布的解决方案效果很好。另外,这是我在评论让我走上正确的道路后找到的完整解决方案(从文本文件中选择文本“administrator”之后的子字符串):

$token = " "
$chat_id = " "

$photo = (Select-String -Path C:\diskpulse.txt -Pattern "administrator (.*)").Matches.Groups[1].Value

#in case a substring should be extracted according to the character position ("61" in this particular case), use this instead:
#$photo = (Get-content -Path C:\diskpulse.txt).Substring(61)

Send-TelegramLocalPhoto -BotToken $token -ChatID $chat_id -PhotoPath $photo

您可以使用具有适当正则表达式模式的 select 字符串来提取文件名。

$photo = (Select-String -path 'c:\diskpulse.txt' -Pattern 'c:.+\.png' -AllMatches).matches.value

因为您知道它将始终是 C:\,所以我们可以将开头与 c: 匹配,当然也可以将结尾与 .png 匹配。正则表达式中的 . 将匹配任何字符,因此我们必须使用 \ 将其转义以表示“字面意思是句号”