Skip to content

Instantly share code, notes, and snippets.

@dolphinotaku
Created June 21, 2020 14:42
Show Gist options
  • Save dolphinotaku/a3345e470d6527f79549195edbd61cf1 to your computer and use it in GitHub Desktop.
Save dolphinotaku/a3345e470d6527f79549195edbd61cf1 to your computer and use it in GitHub Desktop.
this routine shows how to replace text in particular file
call ReplaceText.vbs "C:\20200619\context.xml" "%oldpwd%" "%newpwd%" "%cd%" %_STAMP%
call ReplaceText.vbs "C:\20200619\email_config.properties" "%oldpwd%" "%newpwd%" "%cd%" %_STAMP%
call ReplaceText.vbs "C:\20200619\config.bat" "%oldpwd%" "%newpwd%" "%cd%" %_STAMP%
'ReplaceText.vbs
Option Explicit
Const ForAppending = 8
Const TristateFalse = 0 ' the value for ASCII
Const Overwrite = True
Const WindowsFolder = 0
Const SystemFolder = 1
Const TemporaryFolder = 2
Dim FileSystem
Dim FilePath, FileName, OldText, NewText
Dim CurrentFolder
Dim objFSO, objFile
Dim OriginalFile, TempFile, Line
Dim TempFilePath
Function LPad (str, pad, length)
LPad = String(length - Len(str), pad) & str
End Function
' the date time should not generate from this file
' it should pass from the bat script to make it consistency
Dim dd, mmm, mm, yyyy, hh, nn, ss
Dim datevalue, timevalue, dtsnow, dtsvalue
dtsnow = Now()
dd = Right("00" & Day(dtsnow), 2)
mmm = MonthName(Month(Now), True)
mm = Right("00" & Month(dtsnow), 2)
yyyy = Year(dtsnow)
hh = Right("00" & Hour(dtsnow), 2)
nn = Right("00" & Minute(dtsnow), 2)
ss = Right("00" & Second(dtsnow), 2)
Dim TimeStamp
If WScript.Arguments.Count = 5 Then
FilePath = WScript.Arguments.Item(0)
OldText = WScript.Arguments.Item(1)
NewText = WScript.Arguments.Item(2)
CurrentFolder = WScript.Arguments.Item(3)
TimeStamp = WScript.Arguments.Item(4)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(FilePath)
FileName = objFSO.GetFileName(objFile)
'Wscript.Echo "Absolute path: " & objFSO.GetAbsolutePathName(objFile)
'Wscript.Echo "Parent folder: " & objFSO.GetParentFolderName(objFile)
'Wscript.Echo "File name: " & objFSO.GetFileName(objFile)
'Wscript.Echo "Base name: " & objFSO.GetBaseName(objFile)
'Wscript.Echo "Extension name: " & objFSO.GetExtensionName(objFile)
'e.g
'Absolute path: C:\scripts\test.txt
'Parent folder: C:\scripts
'File name: test.txt
'Base name: test
'Extension name: txt
Else
Wscript.Echo "Usage: ReplaceText.vbs <FilePath> <OldText> <NewText> <CurrentFolder>"
Wscript.Quit
End If
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Dim tempFolder: tempFolder = FileSystem.GetSpecialFolder(TemporaryFolder)
TempFilePath = FileSystem.GetTempName
If FileSystem.FileExists(TempFilePath) Then
FileSystem.DeleteFile TempFilePath
End If
Set TempFile = FileSystem.CreateTextFile(TempFilePath, Overwrite, TristateFalse)
Set OriginalFile = FileSystem.OpenTextFile(FilePath)
Do Until OriginalFile.AtEndOfStream
Line = OriginalFile.ReadLine
'MsgBox("Old: "+Line)
If InStr(Line, OldText) > 0 Then
Line = Replace(Line, OldText, NewText)
End If
'MsgBox("New: "+Line)
TempFile.WriteLine(Line)
Loop
OriginalFile.Close
TempFile.Close
FileSystem.MoveFile FilePath, CurrentFolder+"/history/"+FileName+".bak." + dd & mmm & yyyy & "_" & hh & nn & ss
FileSystem.MoveFile TempFilePath, FilePath
Wscript.Quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment