Search code examples
fileencodingvbscriptwshfilesystemobject

ANSI text extracting to other file


I copied the code and I do not know how it works, but I tested it as a macro.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ObjFileTxt = objFSO.CreateTextFile("filename.txt", True, False)
ObjFileTxt.Write "ok"
Set objFSO = Nothing

1. How to change directory in example to %tmp% ? (default is C:\Users\myuser\Documents)
2. How to add ANSI code? i.e. (MZ ˙˙)

ObjFileTxt.Write "ĂäD»N§űGç„ 06߲ŕ7‰Vű"0ť­Â‹1'Ç U¶¤CÜf­×qjćĽ"

ansi has a code that contains " and ' how to write in it to ObjFileTxt.Write

I want the ansi code to be saved in the filename.txt


Solution

  • To answer your questions:

    1. How to change directory in example to %tmp% ?

    You can do it by using .ExpandEnvironmentStrings on a Shell object, like this:

    Set wShell = CreateObject("WScript.Shell")
    sTempPath = wShell.ExpandEnvironmentStrings("%TMP%")
    
    1. How to add ANSI code?

    You have to correct the way you add a quote " inside a string, by duplicating it, like this "":

    ObjFileTxt.Write "AäD»N§uGç„ 06ß?r7‰Vu""0t­Â‹1'Ç    U¶¤CÜf­×qjcL"
    

    Your code will look like this:

    Set wShell = CreateObject("WScript.Shell")
    sTempPath = wShell.ExpandEnvironmentStrings("%TMP%")
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set ObjFileTxt = objFSO.CreateTextFile(sTempPath & "\filename.txt", True, False)
    ObjFileTxt.Write "AäD»N§uGç„ 06ß?r7‰Vu""0t­Â‹1'Ç    U¶¤CÜf­×qjcL"
    Set objFSO = Nothing
    

    If you want then to convert the file in ANSI, check out this answer.

    Hope this helps.