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

Removing non-ASCII characters from a text file.

1,077 views
Skip to first unread message

nadimpal...@gmail.com

unread,
May 10, 2007, 11:28:17 AM5/10/07
to
Hello Everyone,

I am new to VB Scripting. So, please forgive me, if this is basics.

I would like to write a script, which will take a text file as input,
read line by line, replace any non-ASCII characters with '' and send
the result set into a different text file.

I want to replace any character that is not in [A-Za-z 0-9 \.,\?'""!@#\
$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]

How do I do that?

Any pointers are appreciated.

Thanks,
-Pavan

Brendan

unread,
May 10, 2007, 12:02:26 PM5/10/07
to

VBscript has a regular expression object(http://msdn2.microsoft.com/en-
us/library/yab2dx62.aspx) that you can use to search and replace. Best
to open your file and a new file, read in your file one line at a
time, search-replace, and then write to the new file one line at a
time.

mayayana

unread,
May 10, 2007, 12:13:46 PM5/10/07
to
Please don't multi-post. You've posted this same
question separately in a VB group, which has nothing
to do with VBScript.

It sounds like you just want to filter funky
characters. They're all ascii. If it's an ANSI text file
then each byte is an ascii character. But anything
above 125 in English, I think, might be considered
funky.
(If you don't have an ascii reference you might want to
download this handy help file:
http://ourworld.compuserve.com/homepages/r_harvey/asciicat.htm
)


You can probably use RegExp for this, but personally
I find that very tedious to figure out. :) Here's one method:


----------------------------------------------------
'-- save this text as a VBS file, then
'-- drop a text file onto the VBS to clean
'-- out all funky characters.

Dim FSO, TS, sFil, s1, A1(), i2, iChar, s2

sFil = WScript.arguments(0)
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile(sFil, 1)
s1 = TS.ReadAll
TS.Close
Set TS = Nothing

ReDim A1(len(s1) - 1)

For i2 = 0 to UBound(A1)
s2 = Mid(s1, (i2 + 1), 1)
iChar = Asc(s2)
If (iChar < 9) Or ((iChar > 13) And (iChar < 32)) Or (iChar > 125) Then
A1(i2) = Chr(1)
Else
A1(i2) = s2
End If
Next

s1 = Join(A1, "")
s1 = Replace(s1, chr(1), "")

Set TS = FSO.CreateTextFile(sFil, True)
TS.Write s1
TS.Close
Set TS = Nothing

Set FSO = Nothing
--------------------------------

nadimpal...@gmail.com

unread,
May 10, 2007, 12:37:25 PM5/10/07
to
Thanks for the answer. Sorry about the cross-posting.

I will try this and let you know how it worked.

Thanks,
-PAvan

0 new messages