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
--
MSI packaging
Scripting
Deployments

Re: VBscript Search and Replace conundrum by Roland

Roland
Fri Mar 11 14:00:56 CST 2005

"Owen Gilmore" <OwenGilmore@discussions.microsoft.com> wrote in message
news:B0EBBC40-054B-4CEE-A930-0589B7716D21@microsoft.com...
: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.
<!-- SNiP -->

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

According to the above, you're trying to replace C:, not c:. If you want to
replace C:, then I suggest changing the above line to:

FileText = Replace(UCase(FileText),"C:", u_Drv)

or

FileText = Replace(LCase(FileText),"c:", u_Drv)

or

FileText = Replace(Replace(FileText,"C:", u_Drv),"c:", u_Drv)

I generally prefer the 2nd option. Your mileage may vary.

However, if the drive identifier is being removed and not replace with the
value of u_Drv, then you should verify u_Drv is not null/empty. Is it
possible some are C: and some are c: and some are getting replaced?

--
Roland Hall
/* This information 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. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp




Re: VBscript Search and Replace conundrum by OwenGilmore

OwenGilmore
Sat Mar 12 02:31:03 CST 2005

Roland, thanks for that bit of code, but I don't think it's that, but I'll
check it out. I'm inclined to think it's a timing issue. Too tired to test
anymore tonite.

O


"Roland Hall" wrote:

> "Owen Gilmore" <OwenGilmore@discussions.microsoft.com> wrote in message
> news:B0EBBC40-054B-4CEE-A930-0589B7716D21@microsoft.com...
> :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.
> <!-- SNiP -->
>
> : WScript.Echo " Performing Replacement" & FileText & " with " & u_Drv
> : FileText = REPLACE(FileText, "C:", u_Drv)
>
> According to the above, you're trying to replace C:, not c:. If you want to
> replace C:, then I suggest changing the above line to:
>
> FileText = Replace(UCase(FileText),"C:", u_Drv)
>
> or
>
> FileText = Replace(LCase(FileText),"c:", u_Drv)
>
> or
>
> FileText = Replace(Replace(FileText,"C:", u_Drv),"c:", u_Drv)
>
> I generally prefer the 2nd option. Your mileage may vary.
>
> However, if the drive identifier is being removed and not replace with the
> value of u_Drv, then you should verify u_Drv is not null/empty. Is it
> possible some are C: and some are c: and some are getting replaced?
>
> --
> Roland Hall
> /* This information 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. */
> Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
> WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
> MSDN Library - http://msdn.microsoft.com/library/default.asp
>
>
>
>

Re: VBscript Search and Replace conundrum by OwenGilmore

OwenGilmore
Mon Mar 14 12:38:40 CST 2005

Roland, thanks for your reply. Unfortunately it's not as simple as a case
mix-up. What's interesting (frustrating) is that if I do a wscript.echo
u_Drv within the find/replace loop I see that F: (for example) is being
displayed but in the INI files c: is replaced by a blank, not with f:

I think it's either a timing issue (value is being overwritten by something)
or I'm trying to mix data types and it's not replacing the text in the file
as it should be.

Oh I wish I were a coder, doobeedoobeedoobeedoobee doobee do
(sung to I wish I were a rich man)

OK I'm feelin a little flaky today.

but really I'm not much of a coder.

Owen Gilmore

"Roland Hall" wrote:

> "Owen Gilmore" <OwenGilmore@discussions.microsoft.com> wrote in message
> news:B0EBBC40-054B-4CEE-A930-0589B7716D21@microsoft.com...
> :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.
> <!-- SNiP -->
>
> : WScript.Echo " Performing Replacement" & FileText & " with " & u_Drv
> : FileText = REPLACE(FileText, "C:", u_Drv)
>
> According to the above, you're trying to replace C:, not c:. If you want to
> replace C:, then I suggest changing the above line to:
>
> FileText = Replace(UCase(FileText),"C:", u_Drv)
>
> or
>
> FileText = Replace(LCase(FileText),"c:", u_Drv)
>
> or
>
> FileText = Replace(Replace(FileText,"C:", u_Drv),"c:", u_Drv)
>
> I generally prefer the 2nd option. Your mileage may vary.
>
> However, if the drive identifier is being removed and not replace with the
> value of u_Drv, then you should verify u_Drv is not null/empty. Is it
> possible some are C: and some are c: and some are getting replaced?
>
> --
> Roland Hall
> /* This information 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. */
> Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
> WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
> MSDN Library - http://msdn.microsoft.com/library/default.asp
>
>
>
>

Re: VBscript Search and Replace conundrum by Roland

Roland
Mon Mar 14 13:57:54 CST 2005

"Owen Gilmore" wrote in message
news:38622AED-9202-4B44-83C0-A523DBFF0EB2@microsoft.com...
: Roland, thanks for your reply. Unfortunately it's not as simple as a case
: mix-up. What's interesting (frustrating) is that if I do a wscript.echo
: u_Drv within the find/replace loop I see that F: (for example) is being
: displayed but in the INI files c: is replaced by a blank, not with f:
:
: I think it's either a timing issue (value is being overwritten by
something)
: or I'm trying to mix data types and it's not replacing the text in the
file
: as it should be.

Any chance I could see a line?

: Oh I wish I were a coder, doobeedoobeedoobeedoobee doobee do
: (sung to I wish I were a rich man)

*tappin' foot... go brother...*

: OK I'm feelin a little flaky today.

Aren't we all?!

: but really I'm not much of a coder.

It's my first day. (O:=

BTW...

Why do you have this in your for...loop?

If UCase(Right(WScript.FullName, Len(WScript.FullName) -
InStrRev(WScript.FullName,"\"))) <> "CSCRIPT.EXE" Then
Set wshShell = WScript.CreateObject("WScript.Shell")
wshShell.Run "%comspec% /k CSCRIPT.EXE " & WScript.ScriptFullName, 1,
False
Set wshShell = Nothing
WScript.Quit
End If

Shouldn't you only need this in the beginning?

--
Roland Hall
/* This information 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. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: VBscript Search and Replace conundrum by O

O
Tue Mar 15 13:25:49 CST 2005

Sure, here is what the INI files look like:

[Digital_Save]
; mode=3DMCI (default)
; mode=3DActiveMovie
mode=3DMCI
drive=3DC:\United\VWCMI\DATA

>> Why do you have this in your for...loop?
>> If UCase(Right(WScript.FullName, Len(WScript.FullName) -
>> InStrRev(WScript.FullName,"\")=AD)) <> "CSCRIPT.EXE" Then
>> Set wshShell =3D WScript.CreateObject("WScript.=ADShell")
>> wshShell.Run

To tell the truth, I cribbed this from somebody else's search/replace
program, and then cobbled together some WMI stuff to determine logical
drive. There probably is a better way of doing it, as you indicate...


Re: VBscript Search and Replace conundrum by Roland

Roland
Tue Mar 15 15:50:00 CST 2005

"O.G." <aogilmore@sbcglobal.net> wrote in message
news:1110914749.781720.134250@l41g2000cwc.googlegroups.com...
Sure, here is what the INI files look like:

[Digital_Save]
; mode=MCI (default)
; mode=ActiveMovie
mode=MCI
drive=C:\United\VWCMI\DATA

Is this the C: being replaced?

>> Why do you have this in your for...loop?
>> If UCase(Right(WScript.FullName, Len(WScript.FullName) -
>> InStrRev(WScript.FullName,"\")­)) <> "CSCRIPT.EXE" Then
>> Set wshShell = WScript.CreateObject("WScript.­Shell")
>> wshShell.Run

To tell the truth, I cribbed this from somebody else's search/replace
program, and then cobbled together some WMI stuff to determine logical
drive. There probably is a better way of doing it, as you indicate...

Ok, if this is a timing issue, verifying the script is being called with
cscript instead of wscript, and making a call to run it in a shell, would
definitely be a timing issue. Unless you check the shell for a return, the
script will continue regardless of what is happening in the shell. If you
want to keep this, I would put it near the top outside of the For...Next
loop.

--
Roland Hall
/* This information 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. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: VBscript Search and Replace conundrum by O

O
Tue Mar 15 16:01:25 CST 2005

No, the C: is not being replaced, or rather it is being replaced but
with a blank value, i.e. I end up with \United\VWCMI\DATA

I'll try your suggestion of putting the check outside the loop.

Funny thing is, I can actually see the drive letter echo (F:) when I
try to debug, but when it comes to the find/replace it just puts a
blank.


Re: VBscript Search and Replace conundrum by Roland

Roland
Tue Mar 15 21:45:22 CST 2005

"O.G." wrote in message
news:1110924085.007735.200410@l41g2000cwc.googlegroups.com...
: No, the C: is not being replaced, or rather it is being replaced but
: with a blank value, i.e. I end up with \United\VWCMI\DATA

Ok, that's what I was referring to that this is the target.

: I'll try your suggestion of putting the check outside the loop.

Ok, let me know.

: Funny thing is, I can actually see the drive letter echo (F:) when I
: try to debug, but when it comes to the find/replace it just puts a
: blank.

Here's what I do so I don't have to wonder. I write the value of the
variable to the screen.
Wscript.echo somevariable

Then I put this on the next line:
wscript.quit

If that one tests ok, I move onto the next. You can even get more creative,
by testing with a counter.

dim i
i = i + 1
if i = 5 then
wscript.echo somevariable
wscript.quit
end if

One last option...

Write it to a string and display the string at the end.
dim str, i
i = 1 + 1
str = str & i & ". " & "somevariable = " & somevariable & vbCrLf

' at the very end
wscript.echo str

--
Roland Hall
/* This information 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. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation - http://msdn.microsoft.com/downloads/list/webdev.asp
MSDN Library - http://msdn.microsoft.com/library/default.asp



Re: VBscript Search and Replace conundrum by Torgeir

Torgeir
Wed Mar 16 06:17:28 CST 2005

O.G. wrote:

> No, the C: is not being replaced, or rather it is being replaced but
> with a blank value, i.e. I end up with \United\VWCMI\DATA
>
> I'll try your suggestion of putting the check outside the loop.
>
> Funny thing is, I can actually see the drive letter echo (F:) when I
> try to debug, but when it comes to the find/replace it just puts a
> blank.
>
Hi

There is logical error in the script, it modifies the files even for
drives that does not contain the folder \United\VWCMI\DATA.

I also have to say that the script is overly complicated for the task
at hand (don't feel to sorry, this is a usual side-effect when a
non-coder copies pieces from different scripts).

Try the version below instead. Note that the script stops enumerating
the drives at the first folder hit, remove the "Exit For" line if you
want to continue (so the ini file always will contain the last drive
letter where the folder exists, if it exists on several drives).


'--------------------8<----------------------

Option Explicit

'Define Constants

Const OpenAsASCII = 0
Const FailIfNotExist = 0
Const ForReading = 1
Const ForWriting = 2

Dim objFSO, objShell, strWinDir, objDrive, strDrvLetter
Dim strMedPath, strFile1, strFile2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

strMedPath = "\United\VWCMI\DATA"
strFile1 = "vwcmim.ini"
strFile2 = "vwgsmm.ini"

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

strWinDir = objFSO.GetSpecialFolder(0)

For Each objDrive in objFSO.Drives
strDrvLetter = objDrive
WScript.Echo strDrvLetter & strMedPath
If objFSO.FolderExists(strDrvLetter & strMedPath) Then
UpdateFile strWinDir & "\" & strFile1, strDrvLetter
UpdateFile strWinDir & "\" & strFile2, strDrvLetter
' stop looping after first hit
Exit For
End If

Next
WScript.Quit


Sub UpdateFile(ByVal strFile, ByVal strDrv)
Dim fFile, strNewContent

Set fFile = objFSO.OpenTextFile(strFile, ForReading, _
FailIfNotExist, OpenAsASCII)

strNewContent = Replace(fFile.Readall, "C:", strDrv, 1, -1, vbTextCompare)
fFile.Close

Set fFile = objFSO.OpenTextFile(strFile, ForWriting, True)
fFile.Write strNewContent
fFile.Close
End Sub

'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: VBscript Search and Replace conundrum by O

O
Wed Mar 16 13:50:22 CST 2005

a great big Thanks to both of you, Torgeir and Roland - I did actually
get this to work.

Now I've discovered yet another issue - I want to only replace the full
path c:\united\vwcmi\data with <drive letter>:\united\vwcmi\data, not
every instance of c:\ with <drive letter>:\, the reason being that most
of the program components will be on c: but we are thinking of letting
users keep the data on an external hard drive.

If you have any thoughts on that I'd appreciate it but I'll try working
that out for myself, by setting up CONST fullpath or something.

I really appreciate your help (indeed, the script did seem to be
getting longer LOL)

Thanks!
Owen


Re: VBscript Search and Replace conundrum by O

O
Wed Mar 16 13:56:39 CST 2005

Disregard my last message, I found a way to replace only the full path,
i.e. this line

strNewContent = Replace(fFile.Readall, "C:" & strMedPath, strDrv &
strMedPath, 1, -1, vbTextCompare)

thanks again, you guys are awesome!

Owen