Powershell Foreach(PS)中有多个重命名项

Powershell Foreach(PS)中有多个重命名项,powershell,get-childitem,rename-item-cmdlet,foreach-object,Powershell,Get Childitem,Rename Item Cmdlet,Foreach Object,我在一个文件夹中有多个文件,我正在尝试制作一个脚本来执行以下操作: 将文件扩展名更改为txt 将所有句点替换为下划线。 获取文本文件中的内容,并根据内容对其进行重命名 我想让脚本一个文件一个文件地完成 注意:所有文件扩展名都不同 初试 getchilditem-排除“*.ps1”、“.txt”|重命名item-新名称{$\u.Name+“.txt”} Get-ChildItem“*.txt”|重命名项-NewName{$\.Basename.Replace('.','.''''+$\.Ex

我在一个文件夹中有多个文件,我正在尝试制作一个脚本来执行以下操作:

  • 将文件扩展名更改为txt

  • 将所有句点
    替换为下划线

  • 获取文本文件中的内容,并根据内容对其进行重命名

  • 我想让脚本一个文件一个文件地完成

    注意:所有文件扩展名都不同

    初试
    getchilditem-排除“*.ps1”、“.txt”|重命名item-新名称{$\u.Name+“.txt”}
    Get-ChildItem“*.txt”|重命名项-NewName{$\.Basename.Replace('.','.''''+$\.Extension}
    获取子项“*.txt”-过滤器*.txt|
    Foreach对象{
    $content=Get content-Raw$\.FullName
    
    如果($content-match')(?不要重命名多次。根据您的条件构造新文件名并重命名一次。不要重命名多次。根据您的条件构造新文件名并重命名一次。
    Get-ChildItem -Exclude "*.ps1",".txt" | rename-item -NewName  { $_.Name + ".txt" }
    
    Get-ChildItem "*.txt" | Rename-Item -NewName { $_.Basename.Replace('.', '_') + $_.Extension }
    
    Get-ChildItem "*.txt" -Filter *.txt  |
    Foreach-Object {
        $content = Get-Content -Raw $_.FullName
    
        if ($content -match '(?<=serie=")(.*)(?=" folioFiscal)') {
            $Matches[1]
         }
    
         $string1 = $Matches[1]
    
        if ($content -match '(?<=folioFiscal=")(.*)(?=" UUID)') {
            $Matches[1]
            }
    
        $string2 = $Matches[1]
    
        Rename-Item -Path $_.FullName -NewName  ($string1+"-"+$string2+"_"+$_.Name)
    
    }
    
    Get-ChildItem -Exclude "*.ps1","*.txt"  |
    Foreach-Object {
    
        Rename-Item -Path $_.FullName -NewName  ( $_.Name + ".txt" )
        
        Rename-Item -Path $_.FullName -NewName  ($_.Basename.Replace('.', '_') + $_.Extension)
    
        $content = Get-Content -Raw $_.FullName 
    
        if ($content -match '(?<=serie=")(.*)(?=" folioFiscal)') {
            $Matches[1]
        }
            
        $string1 = $Matches[1]
            
        if ($content -match '(?<=folioFiscal=")(.*)(?=" UUID)') {
            $Matches[1]
        }
            
        $string2 = $Matches[1]
            
        Rename-Item -Path $_.FullName -NewName  ($string1+"-"+$string2+"_"+$_.Name)
    }