How to delete leading spaces from text file

February 18, 2011 at 08:06:52
Specs: Windows XP
I have a script that reads a text file and replaces a character string with a new string. The problem is that the data following the new string has spaces that are causing a problem. For example, here is how my data looks prior to running the script

Joe Manager Bob
Eric Manager Freddy


What I am doing is replacing the word Manager with a ; in order to import into Excel easier, making it delimited text. Problem I run into is the data following the ; which is column 2 in the spreadsheet, has leading spaces, which messes up the sorting capabilities. I need to delete teh leading spaces so that the output text file looks like this:

Joe ;Bob
Eric ;Freddy

Here is my script so far:

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\admin\Desktop\test_data\manager.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, " MANAGER ", " ; ")

Set objFile = objFSO.OpenTextFile("C:\Documents and Settings\admin\Desktop\test_data\manager.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close


See More: How to delete leading spaces from text file

Report •

#1
February 18, 2011 at 12:16:41
replace...

strNewText = Replace(strText, " MANAGER ", " ; ")

with...

strNewText = Replace(strText, " MANAGER ", ";")

I removed the spaces around the semicolon.


Report •
Related Solutions


Ask Question