WSH - Out of string space: 'Replace'

May 23, 2011 at 14:01:16
Specs: Windows XP
I'm writing a program to compensate for a bug in our vendor's software. I have about 6,000 text files I need to read. The first part of my script will combine all files into a single file. The reason I'm doing this is because I don't want to use WSH to open and close 6,000 text files to do a find/replace. It's easier to combine all files into a single file, then do a find/replace on a single file. I then string certain data together into an SQL file in order to import into our database.

The problem I'm having is that once the 6,000 text files are combined into a single file, the size of the file is 182MB. I'm not ashamed to say that I found the below section of code on the Microsoft site. A few seconds after the below section of code executes, I receive the following error:

"Out of string space: 'Replace'"

What can I add to this code to prevent this error from occuring?

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set objFile = objFSO.OpenTextFile("C:\Folder\File.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "12345 ", "REMOVED ")

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


See More: WSH - Out of string space: Replace

Report •


#1
May 23, 2011 at 15:38:02
Keep them as seperate files.

How To Ask Questions The Smart Way


Report •

#2
May 24, 2011 at 06:44:10
Would I need to write the below code for every single file? Or is there an easier way?

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set wshShell = WScript.CreateObject( "WScript.Shell" )
Set objFile = objFSO.OpenTextFile("C:\Folder1\File.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "12345 ", "REMOVED ")

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


Report •

#3
May 24, 2011 at 07:12:28
First, I should point out ForReading and ForWriting have no value unless you asign it. I'm not seeing that in this script, so even if your replace did manange to work, the writing to the file should fail. Your first post got it right, though, so I'll assume it's lazy copy/paste. You also create a WScript.Shell object, but never do anything with it, so I have no idea what you're going for there.
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each file In objFSO.GetFolder("c:\Folder1").Files
  If LCase(objFSO.GetExtensionName(file)) = "txt" Then
    strNewText = Replace(objFSO.OpenTextFile(file).ReadAll, "12345 ", "REMOVED ")
	objFSO.OpenTextFile(file, 2).Write strNewText
  End If
Next 'file

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question