Question Déplacer lignes de texte dans un .ini

Plus d'informations
il y a 8 ans 5 mois #24363 par Enouf
Bonjour à tous,

Je viens vers vous car j'ai besoin d'aide sur un script que je n'arrive pas à construire.
J'ai des fichiers .ini qui se présente toujours comme suit :

[Paragraphe1:TESTER]
Instruction1
Instruction2
Instruction3

[Paragraphe2:TESTER]
Instruction1
Instruction2
Instruction3
Instruction4

[Paragraphe3:Config]
Instruction4
Instruction5
Instruction6

J'ai besoin que l'instruction 1 s’exécute en dernier. Je dois donc déplacer l'instruction1 des paragraphes tester en dernière position dans le paragraphe.

Or, je n'ai pas trouvé la formule pour faire cela.

Je vous remercie pour votre aide car je ne me vois pas faire les 100 scripts à la main .

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 5 mois #24364 par Laurent Dardenne
Salut,
il s'agit d'un fichier .ini ou d'un fichier texte structuré ?
Un fichier .ini à une ou des sections contenant une ou des clés et des valeurs :

[section]
clé=valeur


Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 5 mois #24365 par Enouf
bonsoir,

il s'agit bien d'un fichier .ini avec des valeurs pour la machine de production.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 5 mois #24366 par Laurent Dardenne
Peux-tu éditer l'exemple de fichier du premier post en ajoutant les noms de clé ?

Ce que tu veux faire est bien de réécrire le fichier :

[Paragraphe1:TESTER]
clé1=Instruction1
clé2=Instruction2
clé3=Instruction3

en ceci :

[Paragraphe1:TESTER]
clé2=Instruction2
clé3=Instruction3
clé1=Instruction1

ou comme cela ?

[Paragraphe1:TESTER]
clé1=Instruction2
clé2=Instruction3
clé3=Instruction1

L’application utilisant ton fichier ordonne-t-elle les lignes selon un numéro de clé ou selon l'insertion des lignes ?

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 5 mois #24368 par Enouf
Bonjour,

L’application utilisant mon fichier fonctionne selon l'insertion des lignes

la présentation doit donc être comme suit :

[Paragraphe1:TESTER]

clé2=Instruction2

clé3=Instruction3

clé1=Instruction1

Je vous remercie de votre réponse

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 8 ans 5 mois #24376 par Laurent Dardenne
Le plus souvent le contenu des fichiers .ini n'est pas ordonné ce qui fait que les fonctions de lecture de ces fichiers que l'on trouve, renvoient le plus souvent une hashtable qui par défaut n'est pas ordonnée.

Voici une ébauche basée sur les API de lecture d'un fichier .Ini. Elle renvoient des tableaux ce qui facilite ton traitement :
[code:1]
#API : msdn.microsoft.com/en-gb/library/ms724875(VS.85).aspx
$code=@'
/* ======================================================================

C# Source File -- Created with SAPIEN Technologies PrimalScript 2011

gallery.technet.microsoft.com/scriptcent...shioned-INI-f8fbc067


NAME:

AUTHOR: James Vierra, DSS
DATE : 8/30/2012

COMMENT:

Examples:
add-type -Path profileapi.cs

$sb = New-Object System.Text.StringBuilder(256)
[profileapi]::GetPrivateProfileString('section1', 'test1', 'dummy', $sb, $sb.Capacity, \"c:\Temp\test.ini\"«»)
Write-Host ('Returned value is {0}.' -f $sb.ToString()) -ForegroundColor green

[profileapi]::WritePrivateProfileString('section2', 'test5', 'Some new value', \"c:\temp\test.ini\"«»)

[profileapi]::GetPrivateProfileString('section2', 'test5', 'dummy', $sb, $sb.Capacity, \"C:\temp\test.ini\"«»)
Write-Host ('Returned value is {0}.' -f $sb.ToString()) -ForegroundColor green

====================================================================== */
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
public class ProfileAPI{

[DllImport(\"kernel32.dll\"«»)]
public static extern bool WriteProfileSection(
string lpAppName,
string lpString);

[DllImport(\"kernel32.dll\"«»)]
public static extern bool WriteProfileString(
string lpAppName,
string lpKeyName,
string lpString);

[DllImport(\"kernel32.dll\", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool WritePrivateProfileString(
string lpAppName,
string lpKeyName,
string lpString,
string lpFileName);

[DllImport(\"kernel32.dll\", CharSet = CharSet.Ansi, SetLastError = true)]
public static extern uint GetPrivateProfileSectionNames(
IntPtr lpReturnedString,
uint nSize,
string lpFileName);

[DllImport(\"kernel32.dll\", CharSet = CharSet.Ansi, SetLastError = true)]
static extern uint GetPrivateProfileSection(string lpAppName, IntPtr lpReturnedString, uint nSize, string lpFileName);

[DllImport(\"kernel32.dll\", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
public static string[] GetSectionNames(string iniFile) {
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);
if (bytesReturned == 0) {
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
char[] c = new char[1];
c[0] = '\x0';
return local.Split(c, System.StringSplitOptions.RemoveEmptyEntries);
//Marshal.FreeCoTaskMem(pReturnedString);
//use of Substring below removes terminating null for split
//char[] c = local.ToCharArray();
//return MultistringToStringArray(ref c);
//return c;
//return local; //.Substring(0, local.Length - 1).Split('\0');
}

public static string[] GetSection(string iniFilePath, string sectionName) {
uint MAX_BUFFER = 32767;
IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER);
uint bytesReturned = GetPrivateProfileSection(sectionName, pReturnedString, MAX_BUFFER, iniFilePath);
if (bytesReturned == 0) {
Marshal.FreeCoTaskMem(pReturnedString);
return null;
}
string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
char[] c = new char[1] { '\x0' };
return local.Split(c, System.StringSplitOptions.RemoveEmptyEntries);
}

}
'@


add-type $code

$iniFile='c:\temp\test.ini'
$NewIniFile='c:\temp\test2.ini'
@'
[Paragraphe1:TESTER]
cle1=Instruction1
cle2=Instruction2
cle3=Instruction3

[Paragraphe2:TESTER]
cle1=Instruction1
cle2=Instruction2
cle3=Instruction3
cle4=Instruction4

[Paragraphe3:Config]
cle1=Instruction4
cle2=Instruction5
cle3=Instruction6

[Paragraphe4:TESTER]
cle1=Instruction1
cle2=Instruction2

[Paragraphe5:TESTER]
cle1=Instruction1

[Paragraphe6:TESTER]
'@ > $Inifile


$sections=[ProfileAPI]::GetSectionNames($iniFile)
$ofs=\"`r`n\"
del $NewIniFile
foreach ($section in $sections)
{
\"[${Section}]\">> $NewIniFile
$iniSections=[ProfileAPI]::GetSection($iniFile,$section)
if ($null -ne $iniSections)
{
if ($iniSections.count -gt 1)
{
$Count=$iniSections.count-1
\"$($inisections[1..$count])\">> $NewIniFile
\"$($inisections[0])\">> $NewIniFile
}
else
{ \"$iniSections\">> $NewIniFile }
}
}
type $iniFile
type $NewIniFile
[/code:1]
Il reste à tester qq cas, lignes commentées par exemple. Voir ici

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.051 secondes
Propulsé par Kunena