Click here to Skip to main content
15,888,251 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have INI file and I want to get specific section. The items in the section that I choose are 24 items. I want to use all the item to write in a file. I tried this, It works, but it looks like bad way to write 24 times to do the process. Is there any other way to do that more beautiful? The section of my INI file like this

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


What I have tried:

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


My expectation, I can make this way shorter and the output is getting 24 file.

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900