【发布时间】:2020-09-08 16:06:20
【问题描述】:
我有 400 多个 .vcf 文件,我想用文件名替换“FN:”行(第 4 行)。我查看了多种解决方案,但我似乎无法找到可以实现我正在寻找的东西,即使我知道有办法做到这一点。
这是我目前拥有的
文件名:姓氏第一名
BEGIN:VCARD
VERSION:3.0
N:lastName;firstName;;;
FN:firstName lastName
ADR:;;111 Main Rd;Columbia;MO;65202;
TEL;TYPE=mobile:(111) 222-3333
EMAIL;TYPE=work:email@gmail.com
BDAY:20000101
END:VCARD
这就是我想要实现的目标
保留“FN:”并将其后的文本替换为文件名文本。
BEGIN:VCARD
VERSION:3.0
N:lastName;firstName;;;
FN:LastNamefirstName
ADR:;;111 Main Rd;Columbia;MO;65202;
TEL;TYPE=mobile:(111) 222-3333
EMAIL;TYPE=work:email@gmail.com
BDAY:20000101
END:VCARD
这个 Powershell 脚本确实做了我想要的一半,但我真的很想把文件名输入到 replacementLineText 中。
# Set by user to their needs.
$filesToCheck = "C:\path\*.vcf"
$lineToChange = 4
$replacementLineText = "New Text"
# Gather list of files based on the path (and mask) provided by user.
$files = gci $filesToCheck
# Iterate over each file.
foreach ($file in $files) {
# Load the contents of the current file.
$contents = Get-Content $file
# Iterate over each line in the current file.
for ($i = 0; $i -le ($contents.Length - 1); $i++) {
# Are we on the line that the user wants to replace?
if ($i -eq ($lineToChange - 1)) {
# Replace the line with the Replacement Line Text.
$contents[$i] = $replacementLineText
# Save changed content back to file.
Set-Content $file $contents
}
}
}
任何意见或指导将不胜感激!
【问题讨论】:
标签: powershell notepad++