Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

VBScript to Remove First Character from each line and dump into new text file

1,936 views
Skip to first unread message

Chris

unread,
Apr 18, 2012, 10:39:13 AM4/18/12
to
Hello,

I have a csv file that have a list of confirmation codes. I need to
remove the first letter of each line in that list. I have Googled
endlessly for a solution but I either only find how to trim a
character or create a text file with output, but not both. Below is a
sample of the file called confirm.csv. Could anyone help me construct
a script to remove the first character on each line and dump into a
new file called confirm_extract.txt?

Thank you in advance.


Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.



z374DG
z4669B
z5147N
z5597N
z7322C
z775B4

Mayayana

unread,
Apr 18, 2012, 11:18:08 AM4/18/12
to
After reading in the file you have a string?
Something like s = Textstream.ReadAll?

Then....

A1 = Split(s, vbCrLf) '--create an array of lines.
For i = 0 to UBound(A1)
s2 = A1(i)
If Len(s2) > 0 then '-- avoid errors on blank lines.
s2 = Right(s2, (Len(s2) - 1))
A1(i) = s2
end if
Next

s = Join(A1, vbCrLf) '-- put the file string back together.

'-- Now s can be written to disk.

------------------------------------------
--
--
"Chris" <chris...@gmail.com> wrote in message
news:e8eb3b83-762f-4a12...@s7g2000yqm.googlegroups.com...

Dave "Crash" Dummy

unread,
Apr 18, 2012, 11:30:38 AM4/18/12
to
Mayayana wrote:
> After reading in the file you have a string?
> Something like s = Textstream.ReadAll?
>
> Then....
>
> A1 = Split(s, vbCrLf) '--create an array of lines.
> For i = 0 to UBound(A1)
> s2 = A1(i)
> If Len(s2) > 0 then '-- avoid errors on blank lines.
> s2 = Right(s2, (Len(s2) - 1))
> A1(i) = s2
> end if
> Next
>
> s = Join(A1, vbCrLf) '-- put the file string back together.
>
> '-- Now s can be written to disk.

As always, there's more than one way to skin a script.
Here's how I'd do it:
'================ stripper.vbs ==================
set fso=CreateObject("Scripting.FileSystemObject")
set infile=fso.OpenTextFile("confirm.csv")
set outFile=fso.CreateTextFile("confirm_extract.txt")
do until infile.atEndOfStream
line=infile.readLine
outfile.writeLine mid(line,2)
loop
'===========================================

Note: "mid(line,2)" will work without error even for a zero length
or single character line. It just returns a zero length line.
--
Crash

"The future ain't what it used to be."
~ Yogi Berra ~

Mayayana

unread,
Apr 18, 2012, 2:15:36 PM4/18/12
to
Your way makes sense, but I was also trying to provide
help without posting a complete script. So far this week
you've likely done the day's work for two IT people.
Hopefully they'll send you a check. :)


Dave "Crash" Dummy

unread,
Apr 18, 2012, 2:33:50 PM4/18/12
to
Mayayana wrote:
> Your way makes sense, but I was also trying to provide help without
> posting a complete script. So far this week you've likely done the
> day's work for two IT people.

I know I shouldn't encourage the sluggards, but problem solving is how I
entertain myself. It is also how I learn more about scripting. As I
said, I've never used RegExp before. Now I know how.

> Hopefully they'll send you a check. :)

Phat chance!!
--
Crash

"The real question is not whether machines think but whether men do."
~ B. F. Skinner ~

Chris

unread,
Apr 18, 2012, 4:43:49 PM4/18/12
to
I appreciate you honesty. I use the boards as a last resort when I
can't figure something out...cause you are right...people shouldn't
have to do work for me, and how else are you gonna learn if you don't
try right? Thank you for your reply..I really do appreciate it!

Chris

unread,
Apr 18, 2012, 4:45:43 PM4/18/12
to
On Apr 18, 10:30 am, "Dave \"Crash\" Dummy" <inva...@invalid.invalid>
wrote:
Thank you very much for your reply...your script was helpful....Just
to let you guys know, I only use boards as a last resort...I apologize
for not posting my work earlier...just frustrated and wasn't
thinking.....Just so you guys don't think I'm a total sluggard and
don't try it on my own, here is my effort before your post:


Option Explicit

Dim objFSO, strTextFile, strData, strLine, aLines, a
CONST ForReading = 1


strTextFile = "confirm.csv"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set a = objFSO.CreateTextFile("confirm_extract.txt")

strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll


aLines = strData

For Each strLine in aLines
Next
a.WriteLine mid(strData,2)

'Cleanup
Set objFSO = Nothing

Tom Lavedas

unread,
Apr 18, 2012, 5:30:04 PM4/18/12
to
> Set objFSO = Nothing- Hide quoted text -
>
> - Show quoted text -

If you'd posted that someone could have pointed out that you weren't
parsing the data file into the array and that there was nothing inside
your FOR loop. Corrected, it would look something like this ...

Option Explicit

Dim objFSO, strTextFile, strData, strLine, aLines, a
CONST ForReading = 1

strTextFile = "confirm.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set a = objFSO.CreateTextFile("confirm_extract.txt")

strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

aLines = Split(strData, vbNewline)

For Each strLine in aLines
a.WriteLine mid(strLine,2)
Next

So, you were actually pretty close to a working script, though the
errors were pretty fundamental.

BTW, I left out the 'clean up' because a script does that
automatically when it exits. The only time it 'might' be needed is if
you have big data structures and find that you are running out of
memory to run a later part of the script.

I said 'might' because such a memory hog of a script is very
unlikely. If the job is that big, scripting is probably the wrong
tool.
__________________________
Tom Lavedas

Evertjan.

unread,
Apr 18, 2012, 5:59:33 PM4/18/12
to
Chris wrote on 18 apr 2012 in microsoft.public.scripting.vbscript:

> I have a csv file that have a list of confirmation codes. I need to
> remove the first letter of each line in that list.


load file into a variable v

Set regEx = New RegExp
regEx.Pattern = "\n."
regEx.Global = True
r = regEx.Replace(v, "\n")

store r into file



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
0 new messages