1 Replies - 178 Views - Last Post: 16 June 2019 - 08:01 PM Rate Topic: -----

#1 Laurentziasbr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-June 19

How to handle looping to create file using powershell?

Posted 16 June 2019 - 07:53 PM

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
A1=12,34,56
A2=23,45,67
A3=34,56,78,9,10
...
A24=a1,b2,c3,d4,e5

My code
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 output file.

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

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


Is This A Good Question/Topic? 0
  • +

Replies To: How to handle looping to create file using powershell?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: How to handle looping to create file using powershell?

Posted 16 June 2019 - 08:01 PM

Also seen here:
https://www.codeproj...e-using-powersh

https://stackoverflo...sing-powershell
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1