-1

(I've reviewed the other answers but didn't find it match my question) I have an INI file that looks like that:

[DATA]
installerDir=C:\installerDir
scriptsDir=C:\Scripts
TargetDirName=MSI
[DB]
DB_Name="Database_2211"
DistribDir=DataDist
TargetDir=TarMSI

I want to get as an output 2 files that will look like that: First file will contain the following data:

installerDir=C:\installerDir
scriptsDir=C:\Scripts
TargetDirName=MSI

Second file will contain the following data:

DB_Name="Database_2211"
DistribDir=DataDist
TargetDir=TarMSI

Files names should be something_DATA.ini & something_DB.ini accordingly. Files content should not include the block name (i.e.: DATA -or- DB...) I found that code for bash but cannot find it for PowerShell.

ShaiO
  • 153
  • 1
  • 10
  • Welcome to StackOverflow. Your question was relatively simple so i've offered a possible solution but FYI in future you are more likely to get help here if you have attempted to write some code first (StackOverflow is not a code writing service). – Mark Wragg Jul 12 '17 at 10:16

1 Answers1

0

You could do this:

$Ini = Get-Content 'Something.ini'

$Split = $Ini -Replace '\[DATA\]' -Split '\[DB\]'
$Split[0].Trim() | Out-file 'something_DATA.ini'
$Split[1].Trim() | Out-file 'something_DB.ini'

Explanation:

  • Reads the contents of the .ini file with Get-Content
  • Replaces the [DATA] text part with blank (to remove it) and splits the content based on the [DB] part (which is also removes it)
  • Removes blank lines from each part of the split (accessing them with their array index [0] and [1]) and redirects them to the files.
Mark Wragg
  • 22,105
  • 7
  • 39
  • 68