/build/static/layout/Breadcrumb_cap_w.png

Search and replace conundrum - VBscript

I have an application where the INI files are "dumb" and always write the path c:\ to a couple of INI files. "vwcmim.ini" and "vwgsmm.ini" in the windows directory.

I want to list drives (like xternal), check them for data directory, and if it's found replace c:\ with <drive:\> in the INI files.

I've got to the point where it does process the files but instead of putting in the drive letter it just erases the old drive letter c: with a null string.

I am wondering what I'm doing wrong, or if anybody's got a better idea for simple file text search/replace. VBscript code follows. It has to run from a command prompt using cscript <scriptname>



' This script finds and replaces the drive C: with the correct drive letter in the ' INI see below (see SubFolder)
' This allows vACBI to run even where the data drive letter is not c:
' Owen Gilmore - owen.gilmore@united.com
' aogilmore@sbcglobal.net
' 7:20 AM 1/10/2005

Option Explicit
Dim WshProcessEnvironment
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshProcessEnvironment = WshShell.Environment("Process")
'Define Object Variables
Dim FSys
Dim xFolder
Dim xFiles
Dim xFile
Dim tFile
Dim wshShell
'On Error Resume Next
Dim strComputer
Dim objWMIService
Dim colItems
Dim objFSO
Dim objShell
Dim strmsg
Dim u_Drv
Dim u_MedPath
Dim objItem
Dim EnvWinDir

' Define Working Variables
Dim SubFolder
Dim FileText

'Get some necessary environment variables
EnvWinDir = WshProcessEnvironment("windir")

'Define Constants
CONST U_File1 = "vwcmim.ini"
CONST U_File2 = "vwgsmm.ini"
Const ForReading = 1
Const ForWriting = 2
Const TristateUseDefault = -2

'On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
u_MedPath = "\United\VWCMI\DATA"

For Each objItem in colItems

strmsg=strmsg&"DeviceID: " & objItem.DeviceID&vbcrlf

If objFSO.FolderExists(objItem.DeviceID & u_MedPath) Then
u_Drv = objItem.DeviceID
End If

SubFolder = EnvWinDir

'Verify that the script runs in console mode
If UCase(Right(WScript.FullName, Len(WScript.FullName)-InStrRev(WScript.FullName,"\"))) <> "CSCRIPT.EXE" Then
Set wshShell = WScript.CreateObject("WScript.Shell")
wshShell.Run "CSCRIPT.EXE " & WScript.ScriptFullName, 1, False
Set wshShell = Nothing
WScript.Quit
End If

'Set Objects
Set FSys = WScript.CreateObject("Scripting.FileSystemObject")
Set xFolder = FSys.GetFolder(SubFolder)
Set xFiles = xFolder.Files
WScript.Echo SubFolder
WScript.Echo "Directive to process " & xFiles.Count & " files."

'Parse Each file and perform replacements
For Each xFile in xFiles
if xFile.Name = u_File1 OR xFile.Name = u_File2 Then

WScript.Echo " --> " & xFile.Name
WScript.Echo " Reading in contents"
WScript.Echo u_Drv

Set tFile = xFile.OpenAsTextStream(ForReading, TristateUseDefault)
FileText = tFile.ReadAll
tFile.Close

WScript.Echo " Performing Replacement" & FileText & " with " & u_Drv
FileText = REPLACE(FileText, "C:", u_Drv)

WScript.Echo " Saving new file"
Set tFile = xFile.OpenAsTextStream(ForWriting, TristateUseDefault)
tFile.Write FileText
tFile.Close

Else
WScript.Echo " --> " & xFile.Name & " NOT APPLICABLE"
' FileText = ""
End If
Next

Next


'Clean Environment and exit
On Error Resume Next

Set tFile = Nothing
Set xFolder = Nothing
Set xFiles = Nothing
Set xFile = Nothing
Set FSys = Nothing

0 Comments   [ + ] Show comments

Answers (2)

Posted by: nmi 19 years ago
Orange Belt
0
ORIGINAL: aogilmor

Set tFile = xFile.OpenAsTextStream(ForReading, TristateUseDefault)
FileText = tFile.ReadAll
tFile.Close

WScript.Echo " Performing Replacement" & FileText & " with " & u_Drv
FileText = REPLACE(FileText, "C:", u_Drv)

WScript.Echo " Saving new file"
Set tFile = xFile.OpenAsTextStream(ForWriting, TristateUseDefault)
tFile.Write FileText
tFile.Close




I've found problems with the OpenAsTextStream method in the past, especially when you consider it's reading the whole file into a buffer somewhere (that's undefined).

Personally, I'd use OpenAsTextFile method and read each line in at a time and do a replace on that.

HTH

nmi
Posted by: aogilmor 19 years ago
9th Degree Black Belt
0
thanks, I'll give that a try.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ