0

i need help with this VBScript
What I'm trying to do here is modify the logfile to remove the extra spaces inside. (I just got this script actually somewhere on the net.)
It works if i specify just a single file but I'm trying to modify multiple files. Using the wildcard character as i did below did not work either (sorry I'm not so good with vbs)

Also does anyone know how we can do this without creating a new output file? just modify the original file. Thanks in advance..

Set objFSO = CreateObject("Scripting.FileSystemObject") 
'change this line to wherever you want to read the input from.
Set objTextFile = objFSO.OpenTextFile("D:\access*.log",1) 
Set objNewFile = objFSO.CreateTextFile("D:\access*_new.log")
Do Until objTextFile.AtEndOfStream

myString = objTextFile.Readline
objNewFile.WriteLine(Replace (myString, " ", " "))
Loop
2
  • 1. You are replacing spaces " " by spaces " ", this will result in the same output as the input. You can replace double spaces by single spaces, but you'll still end up with double spaces if there are triple spaces... Ideally you would use a regular expression replace. 2. If you want to modify a file without creating another you have to buffer your input, and overwrite the file with the buffer (this is risky, you could lose data), maybe you could do it with ADO. 3. You cannot use wildcards, you should specify a folder, iterate over all files and pick the ones matching a specific name. Commented Jan 31, 2013 at 8:55
  • Thanks a lot @AutomatedChaos for answering my query. I know they're both spaces. But with the log file I'm trying to parse have some extra spaces that needs to be cleaned up. I got similar issues as with this article below. As I've mentioned, that script works perfect on the logfile I'm using. here's the link where i got the script: cw.sampas.net/blog/2009/10/using-logparser-to-dump-blueco.html Can you show me the code for iterating on all files and the resulting output filename will be the same as the original file name but with a suffix? Commented Jan 31, 2013 at 9:15

2 Answers 2

3

In addition to my comment: The Scripting Guy explains exactly your replacement case: multiple spaces by one, with a regular expression:

Set objRegEx = CreateObject("VBScript.RegExp")

objRegEx.Global = True
objRegEx.Pattern = " {2,}"

strSearchString = _
"Myer Ken, Vice President, Sales and Services"
strNewString = objRegEx.Replace(strSearchString," ")

Wscript.Echo strNewString

The Scripting Guy also explains how you can change a text file:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Jim ", "James ")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close

And on the same technet.microsoft you can find how you can easily iterate over all files. You can use a regular expression again to see if the file is matching your (wildcard) pattern, in your case ^access.*\.log$:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\FSO")
Set colFiles = objFolder.Files
For Each objFile in colFiles
    Wscript.Echo objFile.Name, objFile.Size
Next

This should give you all the ingredients to create your script.

Sign up to request clarification or add additional context in comments.

Comments

1

The freeze-dried version:

  Const ForReading = 1
  Const ForWriting = 2

  Dim goFS    : Set goFS    = CreateObject("Scripting.FileSystemObject")

  Dim reZap : Set reZap = New RegExp
  reZap.Global  = True
  reZap.Pattern = " +"
  Dim oFile
  For Each oFile In goFS.GetFolder("..\testdata\14620676").Files
      WScript.Echo "----", oFile.Name
      Dim sAll : sAll = oFile.OpenAsTextStream(ForReading).ReadAll()
      WScript.Echo sAll
      oFile.OpenAsTextStream(ForWriting).Write reZap.Replace(sAll, " ")
      WScript.Echo oFile.OpenAsTextStream(ForReading).ReadAll()
  Next

that makes no sense at all without @AutomatedChaos' admirable contribution (+1), but avoids growing the file's tail by using .Write instead of .WriteLine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.