Replace a Setting in a text file (VBS)

Purpose: Replace a setting in a text file without sending the whole file (for example: INI-file).

You might have an application that includes a configuration file. This configuration file is deployed along with the application. As time passes, your users will have made their own changes through the application to this configuration file. When you need to update all of the configuration files yourself, you simply cannot replace the file. This would remove all the changes that your users have done. You will need to replace the single line in the file and leave everything else as it was.

Usage: cscript.exe IT_APP_VBS_ReplaceLineInATextFile.vbs (no parameters exist)
Configuration: You need to insert your own customization line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
' *********************************************************************************
' ** Script Name: IT_APP_VBS_ReplaceLineInATextFile.vbs
' ** Created on: 10.6.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Replace a setting in an INI-file (without sending the whole file)
' ** Usage: IT_APP_VBS_ReplaceLineInATextFile.vbs (no parameters exist)
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 10.6.2013 / Initial version.
' *********************************************************************************
 
' *****************************************************************************
' *** Configure Script ********************************************************
' ***************************************************************************** 
textFileLocation = " "
DetectThisLine = " "
InsertThisLine = " "
' ***************************************************************************** 
 
Const ForReading = 1
Const ForWriting = 2
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(textFileLocation, ForReading)
 
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, DetectThisLine, InsertThisLine)
 
Set objFile = objFSO.OpenTextFile(textFileLocation, ForWriting)
objFile.WriteLine strNewText
objFile.Close
' *********************************************************************************
' ** Script Name: IT_APP_VBS_ReplaceLineInATextFile.vbs
' ** Created on: 10.6.2013
' ** Author: Jyri Lehtonen / http://it.peikkoluola.net
' **
' ** Purpose: Replace a setting in an INI-file (without sending the whole file)
' ** Usage: IT_APP_VBS_ReplaceLineInATextFile.vbs (no parameters exist)
' **
' ** License: This program is free software: you can redistribute it and/or modify
' ** it under the terms of the GNU General Public License as published by
' ** the Free Software Foundation, either version 3 of the License, or
' ** (at your option) any later version.
' **
' ** This program is distributed in the hope that it will be useful,
' ** but WITHOUT ANY WARRANTY; without even the implied warranty of
' ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' ** GNU General Public License for more details.
' ** 
' ** History: 
' ** 1.0 / Jyri Lehtonen / 10.6.2013 / Initial version.
' *********************************************************************************

' *****************************************************************************
' *** Configure Script ********************************************************
' *****************************************************************************	
textFileLocation = " "
DetectThisLine = " "
InsertThisLine = " "
' *****************************************************************************	

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(textFileLocation, ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, DetectThisLine, InsertThisLine)

Set objFile = objFSO.OpenTextFile(textFileLocation, ForWriting)
objFile.WriteLine strNewText
objFile.Close

Leave a Reply

Your email address will not be published. Required fields are marked *


one + = two

Post Navigation