Re: Batch File Manipulation With WSH
From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 04/26/04
- Next message: name: "Re: ConTEXT"
- Previous message: name: "Re: Printer Hard disk size"
- In reply to: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Next in thread: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Reply: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 25 Apr 2004 21:37:20 -0700
I have no idea how VB works, either. This is VBScript, which has nothing to
do with VB.
* and ? are not valid filenames. They are wildcard characters implemented
by various tools -- meaning if you want * or ? to work, you'll either have
to implement it yourself or hope that some component implements it. I do
not know of any scripting component that implements it, so you'll likely
have to do it yourself.
Frankly, if you cannot implement it in script code (either VBScript [VB-like
syntax] or JScript [C-like syntax] ), I suggest you look at batch file
programming -- microsoft.public.win2000.cmdprompt.admin -- the crucial batch
file constructs support the * and ? syntax for you, but the downside is that
the batch file syntax itself is quite arcane.
For example, this command will list all files on D: drive with the .txt
extension
for /R D:\ %I IN ( *.txt ) DO @ECHO %I
If you link this command with the script below, you could conceivably
enumerate all the necessary files and do the replacement using the script...
--
//David
IIS
This posting is provided "AS IS" with no warranties, and confers no rights.
//
"Adelson Anton" <adelson@mail.ru> wrote in message
news:408b76ca@duster.adelaide.on.net...
David Wang [Msft] wrote:
> Google this newsgroup for past source code that does what you ask for.
I found one script which works but it's written in VBS (I have NO idea
how VB works, I have experience with only C based languages). Another
problem is that it won't recognize wild characters (*, ?) as right
paramaters.
Here's the code:
Dim FileName, Find, ReplaceWith, FileContents, dFileContents
Find = WScript.Arguments(0)
ReplaceWith = WScript.Arguments(1)
FileName = WScript.Arguments(2)
'Read source text file
FileContents = GetFile(FileName)
'replace all string In the source file
dFileContents = replace(FileContents, Find, ReplaceWith, 1, -1, 1)
'Compare source And result
if dFileContents <> FileContents Then
'write result If different
WriteFile FileName, dFileContents
Wscript.Echo "Replace done."
If Len(ReplaceWith) <> Len(Find) Then 'Can we count n of replacements?
Wscript.Echo _
( (Len(dFileContents) - Len(FileContents)) /
(Len(ReplaceWith)-Len(Find)) ) & _
" replacements."
End If
Else
Wscript.Echo "Searched string Not In the source file"
End If
'Read text file
function GetFile(FileName)
If FileName<>"" Then
Dim FS, FileStream
Set FS = CreateObject("Scripting.FileSystemObject")
on error resume Next
Set FileStream = FS.OpenTextFile(FileName)
GetFile = FileStream.ReadAll
End If
End Function
'Write string As a text file.
function WriteFile(FileName, Contents)
Dim OutStream, FS
on error resume Next
Set FS = CreateObject("Scripting.FileSystemObject")
Set OutStream = FS.OpenTextFile(FileName, 2, True)
OutStream.Write Contents
End Function
- Next message: name: "Re: ConTEXT"
- Previous message: name: "Re: Printer Hard disk size"
- In reply to: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Next in thread: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Reply: Adelson Anton: "Re: Batch File Manipulation With WSH"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|