VB Scripting help required
Author
Discussion

slinky

Original Poster:

15,704 posts

279 months

Tuesday 26th April 2005
quotequote all
Folks,

I need to tidy up a file..

I need to remove all blank spaces!

The file is a list of computer names, there are three leading blank lines, and many blank characters after each entry, there is only one entry per line..

So, I would like to be able to remove any blank "characters" (spaces) from the file..

Is there an easy way of doing this?

TIA,

slinky
587racing.com

Plotloss

67,280 posts

300 months

Tuesday 26th April 2005
quotequote all
You cant update a file with vbScript.

You can delete it and recreate it but there is no edit as such to maintain file system security.

So it would be a case of iteratively reading each text line, trimming the spaces out and writing that line to a new file.

Vee

3,109 posts

264 months

Tuesday 26th April 2005
quotequote all
slinky said:
Folks,

I need to tidy up a file..

I need to remove all blank spaces!

The file is a list of computer names, there are three leading blank lines, and many blank characters after each entry, there is only one entry per line..

So, I would like to be able to remove any blank "characters" (spaces) from the file..

Is there an easy way of doing this?

TIA,

slinky
587racing.com


MS Excel, use the =TRIM() function.
This will remove spaces at either end but NOT in between.
If you have spaces in between it's a bit more complicated . .

Data text to columns the =CONCANTENATE()

PetrolTed

34,468 posts

333 months

Tuesday 26th April 2005
quotequote all
Plotloss said:
You cant update a file with vbScript.

You can delete it and recreate it but there is no edit as such to maintain file system security.

So it would be a case of iteratively reading each text line, trimming the spaces out and writing that line to a new file.


Not strictly true.

Pseudo code would be

- Read file into memory
- process line by line using trim or ltrim, rtrim
- then write file back out when you're doing using option to overwrite original.

slinky

Original Poster:

15,704 posts

279 months

Tuesday 26th April 2005
quotequote all
To give you a little more information, the file is a flat text file with no formatting.. (the output from a "DOS" command)... in order to pipe this data into another script I need to remove the leading spaces and non required data..

I only require the first word on each line (computer name)...


here's an example of some of the text..

ABETTERTON Anne Betterton
ACCSRV1
ACOUPLAND
AJMONAGHAN

So I need to remove anything after the first block of characters, on each line...

TIA,

slinky

Plotloss

67,280 posts

300 months

Tuesday 26th April 2005
quotequote all
True, wasnt clear you can overwrite the entire object just not individual characters inside the object.

If you see what I mean.

PetrolTed

34,468 posts

333 months

Tuesday 26th April 2005
quotequote all
slinky said:
stuff


rtrim

slinky

Original Poster:

15,704 posts

279 months

Tuesday 26th April 2005
quotequote all
picks up rtrim and run's off to google..

Cheers Ted!

slinky

ATG

23,801 posts

302 months

Tuesday 26th April 2005
quotequote all
If you were feeling sufficiently masochistic, I think you could do all of that with a DOS script. Given that you say the output is from a DOS command, you might end up with a more self-contained solution if you just put together a DOS batch file to do the whole thing.

fazz81

324 posts

265 months

Tuesday 26th April 2005
quotequote all
Think this would do what you need...

code said:

Dim strData() As String
Dim strTemp As String
Dim FF As Integer
Dim intPos As Integer
Dim intLoop As Integer
Dim intCount As Integer

FF = FreeFile
Open "C:Data.txt" For Input As #FF
intLoop = 0
While Not EOF(FF)
Line Input #FF, strTemp
If strTemp <> "" Then

intPos = InStr(1, strTemp, " ", vbTextCompare)
strTemp = Mid$(strTemp, 1, (intPos - 1))

ReDim Preserve strData(0 To intLoop)
strData(intLoop) = strTemp
intLoop = intLoop + 1

End If

Wend
Close #FF

Open "C:Data.txt" For Output As #FF

intLoop = UBound(strData())
For intCount = 0 To intLoop
Write #FF, strData(intCount)
Next intCount

Close #FF

pdV6

16,442 posts

291 months

Tuesday 26th April 2005
quotequote all
You could reduce this:
fazz81 said:

intPos = InStr(1, strTemp, " ", vbTextCompare)
strTemp = Mid$(strTemp, 1, (intPos - 1))

ReDim Preserve strData(0 To intLoop)
strData(intLoop) = strTemp
intLoop = intLoop + 1


to:
simpler verison said:

strData = Replace(strTemp, " ", "")

tycho

12,277 posts

303 months

Tuesday 26th April 2005
quotequote all
Can you import it into Excel? The computer names should be in the first column. Delete the rest and save it as plain text.

Fer

7,773 posts

310 months

Tuesday 26th April 2005
quotequote all
awk '{print $1}'

(oops, sorry, wrong OS!)

fazz81

324 posts

265 months

Tuesday 26th April 2005
quotequote all
pdV6 said:
strData = Replace(strTemp, " ", "")


this would only remove spaces, but slinky only wants the first word on each line.

ATG

23,801 posts

302 months

Tuesday 26th April 2005
quotequote all
Fer said:
awk '{print $1}'

(oops, sorry, wrong OS!)

Whose got the wrong OS? Him or you?

dilbert

7,741 posts

261 months

Tuesday 26th April 2005
quotequote all
If you're piping things about, can't you use a Windows port of GREP?

slinky

Original Poster:

15,704 posts

279 months

Tuesday 26th April 2005
quotequote all
Tis done..

coding bloke in the office said:

Const ForReading = 1, ForWriting = 2
Dim fso,txtInputFile, txtOutPutFile

strInputFileName = "infile.txt"
strOutPutFileName = "PCRerun.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set txtInputFile = fso.OpenTextFile(strInputFileName)
Set txtOutPutFile = fso.createTextFile(strOutPutFileName)

Do While txtInputFile.AtEndOfStream <> True
txtReadLine = trim(txtInputFile.Readline)
if left(txtReadline, 2) = "\" then
myPos = Instr(1, txtReadLine, chr(32))
if myPos <> 0 then
txtReadLine = Left(txtReadLine, myPos-1)
end if
txtOutPutFile.WriteLine(txtReadLine)
end if
Loop

txtInputFile.Close
txtOutPutFile.Close



Cheers for all of your input guys!

slinky