# VB Script to remove spaces from text string



## Ilneval (Feb 18, 2011)

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


----------



## AlbertMC2 (Jul 15, 2010)

Hi

I think you just have to remove the space after the " ; " in

```
strNewText = Replace(strText, " MANAGER ", " ;")
```
Also remember (if i am not mistaken) that "Manager" is case sensitive

So try this:

```
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
```


----------



## Ilneval (Feb 18, 2011)

unfortunately removing the space before the ; did not work. It is a minor thing at this point. Need to create a "pretty" menu for the script now. Question posted on this board


----------



## AlbertMC2 (Jul 15, 2010)

Hi

When I run the script and have a file *manager.txt* with the following in:

```
Joe Manager Bob
Eric Manager Freddy
```
After running the script it will look like this:

```
Joe ;Bob
Eric ;Freddy
```
What is your doing at the moment?


----------

