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

Re: Removing non-ASCII characters from a text file. by Brendan

Brendan
Thu May 10 11:02:26 CDT 2007

On May 10, 12:28 pm, nadimpalli.pa...@gmail.com wrote:
> 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

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.


Re: Removing non-ASCII characters from a text file. by mayayana

mayayana
Thu May 10 11:13:46 CDT 2007

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






Re: Removing non-ASCII characters from a text file. by nadimpalli

nadimpalli
Thu May 10 11:37:25 CDT 2007

Thanks for the answer. Sorry about the cross-posting.

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

Thanks,
-PAvan