Re: Batch File Manipulation With WSH

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: David Wang [Msft] (someone_at_online.microsoft.com)
Date: 04/26/04


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


Relevant Pages

  • Re: using xml to save/open project data
    ... http://www.TransProCalc.org - Free translation project mgmt software ... set filename tk_getOpenfile ... Exec'ing the script you run it as a separate process, your script won't be able to access its vars. ... generating the xml file is easy enough. ...
    (comp.lang.tcl)
  • Re: Help me find 5 mistakes and than solution to thoes mistakes!
    ... > 8:# This script will create links to files from all of the filename ... > 10:# arguments provided on stdin if no filenames provided on command ...
    (Fedora)
  • Re: MIME - scripting and symbol substitutions
    ... This script is creating a text file with a different filename ... symbol called NEWFILENAME (which works, ... MIME from the command line with all options (so normal DCL ...
    (comp.os.vms)
  • Re: virtual server backups
    ... Here is the script. ... 'Save state the virtual machine ... Filename = MyArray) ... Set objVM = Nothing ...
    (microsoft.public.windows.server.general)
  • Help with os.spawnv
    ... I've had to migrate back to Python 2.1 and am now trying to use ... This script gets each Ascii file in the workspace, ... for filename in filenames ...
    (comp.lang.python)