#66362 - 2002-06-08 03:47 PM
Re: Unicode Converter UDF
|
Anonymous
Anonymous
Unregistered
|
The Ascii zero is in the manual under 'General Syntax Rules', I quote: Strings can contain any characters, except the \0 (NULL) and \x1a (end of file) characters.
With FSO you can open a file as unicode for read or write.
I knocked up the following to read and write to/from an array
code:
$files = @ScriptDir+"\output.uni",@ScriptDir+"\output.txt" $txt = "hello there","how are you","today?","I am " WriteFile($files[0],$txt,"u") AppendFile($files[0],"Unicode"+@CRLF+"so there") WriteFile($files[1],$txt) AppendFile($files[1],"Ascii"+@CRLF+"ending with EOL"+@CRLF) ; FOR EACH $fn IN $files $arr = ReadFile($fn) ? $fn +', Lines = '+Ubound($arr) FOR $ajh = 0 to Ubound($arr) ? ""+$ajh+" : "+$arr[$ajh] NEXT ? NEXT GETS $rc EXIT ; ;======================================== ; Creates a text file from $text (can be an array) ; If $type = "u" then a Unicode text file is created. ; New-line is NOT forced after text ; (to force New-Line $text[n] should be an empty element ; or if $text is a string it should end with @CRLF) ; FUNCTION WriteFile($fn,$text,OPTIONAL $type) IF Ubound($text) < 1 $text =Split($text,@CRLF) ENDIF IF $type = "U" $type = -1 ELSE $type = 0 ENDIF DIM $oFSO,$oFile,$i $oFSO = CreateObject("Scripting.FileSystemObject") IF @Error EXIT @Error ENDIF $oFile = $oFSO.OpenTextFile($fn,2,-1,$type) IF @Error EXIT @Error ENDIF FOR $i = 0 TO UBound($text) -1 $oFile.WriteLine($text[$i]) NEXT $oFile.Write($text[UBound($text)]) $oFile.Close ENDFUNCTION ; ;======================================== ; Appends $text (can be an array) onto an existing text file ; New-line is NOT forced before or after text ; (to force New-Line $text[n] should be an empty element ; or if $text is a string it should begin/end with @CRLF) ; FUNCTION AppendFile($fn,$text) IF Ubound($text) < 1 $text =Split($text,@CRLF) ENDIF DIM $oFSO,$oFile,$type $oFSO = CreateObject("Scripting.FileSystemObject") IF @Error EXIT @Error ENDIF $oFile = $oFSO.OpenTextFile($fn,1,0,0) ;Ascii IF @Error EXIT @Error ENDIF $type = $oFile.Read(2) $oFile.Close IF $type = CHR(255)+CHR(254) $type = -1 ELSE $type = 0 ENDIF $oFile = $oFSO.OpenTextFile($fn,8,0,$type) FOR $i = 0 TO UBound($text) -1 $oFile.WriteLine($text[$i]) NEXT $oFile.Write($text[UBound($text)]) $oFile.Close ENDFUNCTION ; ;======================================== ; Reads contents of file into an array. ; Ascii / Unicode is detected automatically. ; FUNCTION ReadFile($fn) DIM $oFSO,$oFile,$type $oFSO = CreateObject("Scripting.FileSystemObject") IF @Error EXIT @Error ENDIF $oFile = $oFSO.OpenTextFile($fn,1,0,0) ;Ascii IF @Error EXIT @Error ENDIF $type = $oFile.Read(2) $oFile.Close IF $type = CHR(255)+CHR(254) $type = -1 ELSE $type = 0 ENDIF $oFile = $oFSO.OpenTextFile($fn,1,0,$type) $ReadFile = Split($oFile.ReadAll,@CRLF) $oFile.Close ? "Type "+$type ENDFUNCTION
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 920 anonymous users online.
|
|
|