0

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

user692942
  • 16,398
  • 7
  • 76
  • 175
  • If you are using VBA you would have another tag to denote what program is hosting it, [tag:excel], [tag:access] etc. For now re-tagged as [tag:wsh] until you [edit] and specify. – user692942 Apr 07 '19 at 19:16

2 Answers2

0

Windows will inherit environment from the process that invokes the function, this is why the file is created at C:\Users\myuser\Documents. To change the directory, you can specify the full path for CreateTextFile(), for example (CreateTextFile("C:\d\a\a.txt")), this will create a file in the location you specify. For your 2nd question, your ask is how to print ", and ', right? There are two ways, one is by using ' over ", when you are using ' your content ', you content is not evaluated, such that special instruction \n will be output as it is, but this is not fully addressed your needs since your content may contain '. You can use "\" instead, this means leave it alone for special characters, so do " asdf \' asdadf", will output "asdf ' asdadf". This is also why I put "\" in CreateTextFile() so that it will be outputted "C:\d\a". Hope this helps

Guang Mo
  • 26
  • 3
0

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.

Louis
  • 3,592
  • 2
  • 10
  • 18
  • it's work, but encoding is utf-8 not ANSI, how to do encode in ANSI? – poszkodowany Apr 07 '19 at 17:38
  • @poszkodowany the third argument of [`CreateTextFile()`](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/createtextfile-method) defines a boolean that holds whether the file is created using ASCII `False` *(the default)* or Unicode `True`. You are already creating the file as ASCII so don't understand what you are asking?? Also remember that [ASCII and ANSI are two different things](https://stackoverflow.com/a/700221/692942). – user692942 Apr 07 '19 at 19:20
  • 1
    @poszkodowany I don't quite undestand what you're asking. If you need to convert the textfile in ANSI, I think this is what you're looking for: https://stackoverflow.com/questions/5182102/vb6-vbscript-change-file-encoding-to-ansi – Louis Apr 07 '19 at 19:25