Archives de catégorie : Scripting

vbscript – RegExp.Test

**METHOD:** `re.Test(string)`

**Description**
Teste si une chaîne correspond au pattern.

**Syntax**
`re.Test(string)`

**Paramètres**
– `string` : Chaîne à tester.

**Retour**
True/False.

**Exemples**
« `vb
Dim re
Set re = CreateObject(« VBScript.RegExp »)
re.Pattern = « ^[A-Z]{2}d{2}$ »
WScript.Echo re.Test(« AB12 »)
« `

**Voir aussi**
`RegExp.Replace`, `RegExp.Execute`

vbscript – RegExp.Replace

**METHOD:** `re.Replace(string, replaceWith)`

**Description**
Remplace les correspondances du pattern par un texte.

**Syntax**
`re.Replace(string, replaceWith)`

**Paramètres**
– `string` : Chaîne source.
– `replaceWith` : Remplacement.

**Retour**
Chaîne modifiée.

**Exemples**
« `vb
Dim re
Set re = CreateObject(« VBScript.RegExp »)
re.Pattern = « s+ »
re.Global = True
WScript.Echo re.Replace(« a b »,  » « )
« `

**Voir aussi**
`Replace`

vbscript – With…End With

**STATEMENT:** `With…End With`

**Description**
Réduit la répétition sur un même objet.

**Syntax**
`With…End With`

**Paramètres**
– *(aucun)*

**Notes**
– Astuce : active `Option Explicit` dans tes scripts pour éviter les variables mal orthographiées.

**Exemples**
« `vb
Dim re
Set re = CreateObject(« VBScript.RegExp »)
With re
.Pattern = « ^d+$ »
.Global = False
End With
WScript.Echo re.Test(« 123 »)
« `

vbscript – CDate

**FUNCTION:** `CDate(expression)`

**Description**
Convertit une expression date/heure valide en valeur de type Date (Variant subtype Date). L’affichage dépend des paramètres régionaux Windows.

**Syntax**
`CDate(expression)`

**Paramètres**
– `expression` : Chaîne, nombre ou expression représentant une date/heure.

**Retour**
Une valeur de type Date.

**Notes**
– Toujours tester avec `IsDate()` avant de convertir si l’entrée est incertaine.
– Le format final dépend de la locale (format court/long Windows).

**Exemples**
« `vb
Dim anydate
anydate = « June 26, 1943 »
WScript.Echo CDate(anydate)
« `

**Output :** `26.06.1943 (selon locale)`

« `vb
Dim anytime
anytime = « 2:23:59 PM »
WScript.Echo CDate(anytime)
« `

**Output :** `14:23:59 (selon locale)`

**Voir aussi**
`IsDate`, `DateValue`, `TimeValue`, `VarType`

vbscript – CreateObject(« Scripting.FileSystemObject »)

**HOWTO:** `Set fso = CreateObject(« Scripting.FileSystemObject »)`

**Description**
Crée un FileSystemObject (FSO) pour manipuler fichiers/dossiers (lecture, écriture, copie, suppression…).

**Syntax**
`Set fso = CreateObject(« Scripting.FileSystemObject »)`

**Paramètres**
– *(aucun)*

**Retour**
Objet `Scripting.FileSystemObject`.

**Exemples**
« `vb
Dim fso
Set fso = CreateObject(« Scripting.FileSystemObject »)
WScript.Echo fso.GetSpecialFolder(2) ‘ TemporaryFolder
« `

vbscript – CInt

**FUNCTION:** `CInt(expression)`

**Description**
Convertit une expression en entier (type Integer). Arrondit à l’entier le plus proche.

**Syntax**
`CInt(expression)`

**Paramètres**
– `expression` : Valeur numérique ou chaîne convertible en nombre.

**Retour**
Un entier (Integer).

**Notes**
– Pour une conversion plus “large” (32-bit), utiliser `CLng()`.
– Pour tronquer au lieu d’arrondir, utiliser `Fix()` ou `Int()` selon le besoin.

**Exemples**
« `vb
WScript.Echo CInt(2.4)
WScript.Echo CInt(2.5)
WScript.Echo CInt(-2.5)
« `

**Output :** `2, 2 (ou 3 selon règles d’arrondi), -2`

**Voir aussi**
`CLng`, `CDbl`, `Fix`, `Int`

vbscript – FileSystemObject.OpenTextFile

**METHOD:** `fso.OpenTextFile(path, iomode[, create[, format]])`

**Description**
Ouvre un fichier texte et retourne un `TextStream` pour lire/écrire.

**Syntax**
`fso.OpenTextFile(path, iomode[, create[, format]])`

**Paramètres**
– `path` : Chemin du fichier.
– `iomode` : 1=ForReading, 2=ForWriting, 8=ForAppending

**Retour**
Un objet `TextStream`.

**Exemples**
« `vb
Dim fso, ts
Set fso = CreateObject(« Scripting.FileSystemObject »)
Set ts = fso.OpenTextFile(« C:tempdemo.txt », 1)
WScript.Echo ts.ReadAll
ts.Close
« `

**Voir aussi**
`FileSystemObject.CreateTextFile`, `TextStream.ReadLine`, `TextStream.WriteLine`

vbscript – CStr

**FUNCTION:** `CStr(expression)`

**Description**
Convertit une expression en chaîne (String).

**Syntax**
`CStr(expression)`

**Paramètres**
– `expression` : Valeur à convertir (nombre, date, booléen, etc.).

**Retour**
Une chaîne.

**Notes**
– Pour formater un nombre, préférer `FormatNumber()`/`FormatCurrency()` plutôt que `CStr()`.

**Exemples**
« `vb
WScript.Echo CStr(123)
WScript.Echo CStr(Now)
« `

**Output :** ` »123″, date/heure selon locale`

**Voir aussi**
`FormatNumber`, `FormatCurrency`, `FormatDateTime`