如何使用Powershell处理循环以创建文件?

时间:2019-06-17 01:35:01

标签: powershell

我有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
;********************

3 个答案:

答案 0 :(得分:0)

尝试以下...

foot = (input ("what is the square footage of your house: "))  

price = float(foot * .075)    

print (price)

答案 1 :(得分:0)

使用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后面的值替换,因此它将Foreach的数字替换为1到24 ...

答案 2 :(得分:0)

这项工作可以吗?

我在这里所做的只是使用您现有的代码从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
    }
}