Powershell 用文件名替换特定行号处的文本

Powershell 用文件名替换特定行号处的文本,powershell,notepad++,Powershell,Notepad++,我有400多个.vcf文件,我想用文件名替换“FN:”行(第4行)。我已经研究了多种解决方案,但我似乎找不到能够实现我所寻找的目标的方法,即使我知道有一种方法可以做到这一点 这就是我目前拥有的 文件名:lastnamfirstname BEGIN:VCARD VERSION:3.0 N:lastName;firstName;;; FN:firstName lastName ADR:;;111 Main Rd;Columbia;MO;65202; TEL;TYPE=mobile:(111) 222

我有400多个.vcf文件,我想用文件名替换“FN:”行(第4行)。我已经研究了多种解决方案,但我似乎找不到能够实现我所寻找的目标的方法,即使我知道有一种方法可以做到这一点

这就是我目前拥有的

文件名:lastnamfirstname

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
        }
    }
}
如有任何意见或指导,将不胜感激

我真的希望获取文件名并将其输入到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
        }
    }
}
要接受所有目标文件的路径,只需声明一个参数:

我对变量名做了一点修改-
Path
是描述可扩展路径的字符串的惯用参数名,参数名通常应为大写

$Path
关联的
[Parameter()]
属性中的
强制
标志意味着调用者必须提供一个值-否则PowerShell将提示输入该值:

PS C:\> .\script.ps1

cmdlet script.ps1 at command pipeline position 1
Supply values for the following parameters:
Path:

PS C:\> .\script.ps1 -Path "C:\path\*.vcf" # now it won't prompt
有关参数的更多信息,请参阅和帮助主题-尽管文档是关于函数的,但脚本文件的参数规则及其声明是相同的(您可以将脚本文件视为恰好位于文件系统而不是内存中的函数)


gci
(或
Get ChildItem
)cmdlet返回
[FileInfo]
对象和所有文件元数据,因此要使用文件名作为循环内的替换值,只需执行
$file.name

$contents[$i] = "FN:$($file.Name)"
# or using the -f format operator:
$contents[$i] = "FN:{0}" -f $file.Name

由于您已经知道要修改的索引(行号减1),因此可以跳过内部循环,而是执行以下操作:

param(
[参数(必需=$true)]
[string[]]$Path
)
$lineToChange=4
#根据用户提供的路径(和掩码)收集文件列表。
$files=获取子项-路径$Path
#迭代每个文件。
foreach($files中的文件){
#加载当前文件的内容。
$contents=获取内容$file
如果($contents.Count-ge$lineToChange){
#用替换行文本替换该行。
$contents[$lineToChange-1]=“FN:$($file.Name)”
#将更改的内容保存回文件。
设置内容$file$contents
}
}

谢谢您的评论!但是,我不确定如何提取文件名(减去扩展名)并替换第四行的当前文本。我需要拉取文件名并用FN:[filename]之类的内容替换第四行,然后保存文件并移动到下一行,依此类推。@KyleW所以您实际上不需要替换文本,只需要当前文件名?是的!对于**[string]$Replacement=“New Text”**而不是“New Text”,我想用一个变量来替换它,这个变量实际上就是PS正在打开的文件名。@KyleW明白了,我一开始没抓住那个部分。答复updated@KyleW您可以执行类似的操作:
$file.BaseName-替换'-*$'
,这将从基名称中第一次出现的
“-”
中删除任何内容