【问题标题】:How to handle looping to create file using powershell?如何处理循环使用powershell创建文件?
【发布时间】:2019-06-17 01:35:01
【问题描述】:

我有 INI 文件,我想获取特定部分。我选择的部分中的项目是 24 项。我想使用所有项目写入文件。我试过这个,它可以工作,但是写 24 次来完成这个过程看起来很糟糕。有没有其他更漂亮的方法?我的 INI 文件部分是这样的

输入ini:

[Code]
A1=12,34,56
A2=23,45,67
A3=34,56,78,9,10
...
A24=a1,b2,c3,d4,e5

脚本:

Function F_ML
{
    $FilePath = "C:\Users\File.ini"
    $section = "Code"
    $R_1 = "A1"
    $R_2 = "A2"
    $R_3 = "A3"
    $R_4 = "A4"
    $R_5 = "A5"
    $R_6 = "A6"
    $R_7 = "A7"
    $R_8 = "A8"
    $R_9 = "A9"
    $R_10 = "A10"
    $R_11 = "A11"
    $R_12 = "A12"
    $R_13 = "A13"
    $R_14 = "A14"
    $R_15 = "A15"
    $R_16 = "A16"
    $R_17 = "A17"
    $R_18 = "A18"
    $R_19 = "A19"
    $R_20 = "A20"
    $R_21 = "A21"
    $R_22 = "A22"
    $R_23 = "A23"
    $R_24 = "A24"

    $store = "C:\Users\"

    $FilePath
    $input_file = $FilePath
    $ini_file = @{}

    Get-Content $input_file | ForEach-Object {
    $_.Trim()
    } | Where-Object {


    $_ -notmatch '^(;|$)'
    } | ForEach-Object {
    if ($_ -match '^\[.*\]$') {
        $section = $_ -replace '\[|\]'
        $ini_file[$section] = @{}
    } else {
        $key, $value = $_ -split '\s*=\s*', 2
        $ini_file[$section][$key] = $value
    }
    }

     #--------
     $Path_Store = $store
     #---------
     $Get_1 = $ini_file.($section).($R_1)
     $L_1 = $Get_1.Substring(0,3)

     $Get_2 = $ini_file.($section).($R_2)
     $L_2 = $Get_2.Substring(0,3)

     $Get_3 = $ini_file.($section).($R_3)
     $L_3 = $Get_3.Substring(0,3)


     #---------
     $Outer = ";********************"
     $Header = ";*******************"
     $ML = "12345"
     $FB = ";Initial=1a2b"
     #----------
     $B_ID_1 = ";Build=" + $ML + "#" + "S" + $L_1 + "#" + "D" + $L_1
     $CRM_1 = ";CRM="  + $R_1
     $Output_1 = $Header, $B_ID_1, $FB, $CRM_1 , $Outer | Out-File $Path_Store\A1

     $B_ID_2 = ";Build=" + $ML + "#" + "S" + $L_2 + "#" + "D" + $L_2
     $CRM_2 = ";CRM="  + $R_2
     $Output_2 = $Header, $B_ID_2, $FB, $CRM_2 , $Outer | Out-File $Path_Store\A2

     $B_ID_3 = ";Build=" + $ML + "#" + "S" + $L_3 + "#" + "D" + $L_3
     $CRM_3 = ";CRM="  + $R_3
     $Output_3 = $Header, $B_ID_3, $FB, $CRM_3 , $Outer | Out-File $Path_Store\A3


     #---------
    }

    $call = F_ML

我的期望是,我可以使这种方式更短,输出得到 24 个输出文件。

Output Sample
Output File 1
;********************
;Build=12345#S12#D12
;Initial=1a2b
;CRM=A1
;********************

Output File 2
;********************
;Build=12345#S23#D23
;Initial=1a2b
;CRM=A2
;********************

【问题讨论】:

  • 这里的"Build=12345""Initial=1a2b" 是什么。您的脚本已硬编码,这里唯一动态的是 CRM=<value> 和 #SD. Will Build` 和 Initial 对于所有文件总是相同的?
  • 它只是一个字符串。是的,所有文件都将始终相同。 @PrasoonKarunanV

标签: powershell


【解决方案1】:

试试下面...

$IniContent = Get-Content -Path $IninPath
$IniContent | ForEach-Object -Process {
  $Split          = $_ -split '=';
  $OutPutfilePath = "c:\temp\$($Split[0]).txt"
  $first          = ($Split[1] -split ',')[0]
  $append         = "#S{0}D{1}" -f $first,$first

  # create a herestring to build the output
  @"
  ;********************
  ;Build=$Ml$append
  $FB
  ;CRM=$($Split[0])    
  ;********************
  "@ | Out-File -Path $OutPutfilePath -Force
 }

【讨论】:

  • 它不考虑我的问题。
【解决方案2】:

使用ForEach 操作字符串,使用Invoke Expression

1..24 | ForEach-Object { Invoke-Expression -Command (
    '$R_{0} = "A{0}"' -f $_
)}
.
.
.
1..24 | ForEach-Object { Invoke-Expression -Command (
    '$Get_{0} = $ini_file.($section).($R_{0}) `
     $L_{0} = $Get_{0}.Substring(0,3)' -f $_
)}
.
.
.
1..24 | ForEach-Object { Invoke-Expression -Command (
    '$B_ID_{0} = ";Build=" + $ML + "#" + "S" + $L_{0} + "#" + "D" + $L_{0}
     $CRM_{0} = ";CRM="  + $R_{0}
     $Output_{0} = $Header, $B_ID_{0}, $FB, $CRM_{0} , $Outer | Out-File $Path_Store\A{0}' -f $_
)} 

{0} 将被替换为 -f 后面的值,因此它将被替换为从 1 到 24 的 Foreach 数字...

【讨论】:

    【解决方案3】:

    这行得通吗?

    我在这里所做的只是使用您现有的代码在从 1 到 24 的循环中运行所有内容,删除重复的代码。我把它重新格式化了一点,这样我就更容易阅读了。

    本质上,变量$i 将是一个从124 的数字,而变量$R 将是“A”以及$i 中的任何数字(本质上是A + $i)

    Function F_ML
    {
        $section = "Code"
        $store = "C:\Users\"
        $input_file = "C:\Users\File.ini"
        $ini_file = @{}
    
        for ($i=1; $i -le 24; $i++) 
        {
            $R = "A$($i)"
    
            Get-Content $input_file | 
    
            ForEach-Object `
            {
                $_.Trim()
            } | 
    
            Where-Object `
            {
                $_ -notmatch '^(;|$)'
            } | 
    
            ForEach-Object `
            {
                if ($_ -match '^\[.*\]$') 
                {
                    $section = $_ -replace '\[|\]'
                    $ini_file[$section] = @{}
                } 
    
                else 
                {
                    $key, $value = $_ -split '\s*=\s*', 2
                    $ini_file[$section][$key] = $value
                }
            }
    
             #--------
             $Path_Store = $store
             #---------
             $Get = $ini_file.($section).($R)
             $L = $Get.Substring(0,3)
    
             #---------
             $Outer = ";********************"
             $Header = ";*******************"
             $ML = "12345"
             $FB = ";Initial=1a2b"
             #----------
             $B_ID = ";Build=" + $ML + "#" + "S" + $L + "#" + "D" + $L
             $CRM = ";CRM="  + $R
             $Output = $Header, $B_ID, $FB, $CRM , $Outer | Out-File $Path_Store\$R
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-06
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 1970-01-01
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多