How can I save non-Latin characters in a text file via VBScript? [duplicate] - vbscript

This question already has answers here:
Write Chinese chars to a text file using vbscript
(3 answers)
Closed 4 years ago.
Chinese characters cannot be saved in a text file via VBScript.
The VBScript is in a folder whose name is in Chinese: 视窗. The script will create a text file, in which the current working directory will be shown. The Chinese characters cannot be saved in the file. Windows Script Host says "Error: Invalid procedure call or argument". The error will not arise if the folder name is in English.
Path = CreateObject("WScript.Shell").CurrentDirectory
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(Path & "\Testing.txt", 8, True)
objFile.WriteLine Path
objFile.close
Is it possible for VBScript to save a file path that contains Chinese characters?

The method openTextFile has another optional parameter - format. Its value is 0 by default which opens the file in ASCII format. To save the chinese characters in the file, you can open the file in UNICODE format by specifying the value of the parameter format = -1. Here is the Reference.
objFso.openTextFile(path,8, true, -1) '-1 = TriStateTrue = Opens the file as Unicode
path = split(wscript.scriptFullName, wscript.scriptname)(0) & "Testing.txt"
set objFso = createObject("scripting.filesystemobject")
set objFile = objFso.openTextFile(path,8, true, -1)
objFile.write path
objFile.Close
set objFile = Nothing
set objFso = Nothing

Related

Multi line vbs auto typer from .txt file [duplicate]

This question already has answers here:
Single Line Input VBS Autotyper
(2 answers)
Closed 2 years ago.
So i have been working on a troll terminal and now i want to type out the entire bee movie script or whatever i put in a text file.
What i need help with is taking the text(with enters and spaces) and fully typing it out.
Another way to say it is i want to take a text file and copy it threw typing it out.
I couldnt find any decent code to do this wich is why i'm asking here.
Also if possible make it type anything that you put in the text file.
(I have no clue why it doesnt let me post this)
Idea: I might know another way around but i dont know how to get it done.
Maby there is a way to infinetly generate variables for each line and type them out like that.
Other idea: I might be able to make a tool to let me quickly hard code it.
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\yourfirstfile", 1)
content = file.ReadAll
Const fsoForAppend = 8
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open the text file
Dim objTextStream
Set objTextStream = objFSO.OpenTextFile("C:\yoursecondfile", fsoForAppend)
'Display the contents of the text file
objTextStream.WriteLine content
'Close the file and clean up
objTextStream.Close
Set objTextStream = Nothing
Set objFSO = Nothing
Set wshShell = CreateObject("Wscript.Shell")
wshShell.run "C:\yoursecondfile"
Tell me if it works
Yes i m self anwsering.
This script works but it double taps enter i will update it if i figure out how to stop that from happening.
Set wsh=WScript.CreateObject("WScript.Shell")
sub custom_text_typer_confirm
Set objFileToRead =WScript.CreateObject("Scripting.FileSystemObject").OpenTextFile("Custom_Text_Typer.txt",1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
custom_typer_wait = inputbox("How long should the program wait untill it will start typing?", "Eclips-Terminal Custom Text Typer")
custom_typer_wait1=custom_typer_wait * 1000
Wscript.sleep custom_typer_wait1
wsh.sendkeys (strFileText & "{enter}")
Wscript.sleep 1000
call home
end sub

Trying to find a username variable to creat files on the desktop said "user" is using [duplicate]

This question already has an answer here:
How to get a path with the variable user in VBscript
(1 answer)
Closed 3 years ago.
Basically, the code im looking for is how to find username with a variable
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\Users\%username%\Desktop\test.txt"
I tried with other lines of code to find out the username. But nothing worked
when i typed out the code i wrote with other lines of code to figure out the %username% variable, it doesn't work.
Please respond with new lines of code attached to my previous code here. (I'm new to this so it would make it confusing if you responded with unattached lines of code.)
You should write it like that :
strUser = CreateObject("WScript.Network").UserName
wscript.echo strUser
And to continue with your code like that :
Dim fso,strUser
Set fso = CreateObject("Scripting.FileSystemObject")
strUser = CreateObject("WScript.Network").UserName
'wscript.echo strUser
fso.CreateTextFile "C:\Users\"& strUser &"\Desktop\test.txt"
Also,in VBScript you can get the path to the current user's desktop folder via the SpecialFolders collection:
WScript.Echo CreateObject("WScript.Shell").SpecialFolders("Desktop")
And your code can be written like that too :
Option Explicit
Dim fso,DesktopFolder
Set fso = CreateObject("Scripting.FileSystemObject")
DesktopFolder = CreateObject("WScript.Shell").SpecialFolders("Desktop")
fso.CreateTextFile DesktopFolder &"\test.txt"

Why does VBS not read this text file correctly?

I have the following code to read a text file:
Option Explicit
Dim InputFile
Dim FSO, oFile
Dim strData
InputFile = "C:\Program Files (x86)\AVG\CloudCare\ClientVersion.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFile = FSO.OpenTextFile(InputFile)
strData = oFile.ReadAll
oFile.Close
msgbox strData
The contents of ClientVersion.txt is:
CLIENT_VERSION_STRING _T("3.5.2") //
When I run the VBS code, I get back this:
If I create a new text file with the same content in the same location, it works fine. Is there a reason why VBS is unable to read this simple text file? I couldn't see any issues with permissions on the file.
ÿþ is the byte order mark of a UTF-16 Little Endian encoded file. UTF-16 (unlike ASCII/ANSI) uses two bytes for a character instead of just one. However, the OpenTextFile method reads files as ASCII files by default, so each 2-byte character gets interpreted as two separate characters.
From the documentation:
Syntax
object.OpenTextFile(filename[, iomode[, create[, format]]])
Arguments
[…]
format
Optional. One of three Tristate values used to indicate the format of the opened file (TristateTrue = -1 to open the file as Unicode, TristateFalse = 0 to open the file as ASCII, TristateUseDefault = -2 to open the file as the system default). If omitted, the file is opened as ASCII.
Specify the proper encoding when reading the file and the problem will disappear:
Set oFile = FSO.OpenTextFile(InputFile, 1, False, -1)

Physically opening a text file

I have a text file C:\user\test.txt with the text "This is a test", and I want to physically open the file using VBScript (as if i were to click Accesories → Notepad). I want to see the file open on my screen so I can visually read the text.
Now, for some reason I can't. This is what I'm trying (I tried with and without the "textfile.close" line, and yes, the file exists in that path):
dim FS1, textfile
Const ForReading = 1
set FS1 = CreateObject("Scripting.FileSystemObject")
set textfile = FS1.OpenTextFile("C:\user\test.txt", ForReading, True)
textfile.close
What am I missing? I have no issue creating, writing, or appending... but I just can't open it!
If you want to open the file in Notepad:
With CreateObject("WScript.Shell")
.Run "notepad.exe c:\user\test.txt"
End With
You can also just run the file and it will open in your default txt editor (whatever Windows file association you have established for extension txt).
With CreateObject("WScript.Shell")
.Run "c:\user\test.txt"
End With
Are you reading the file?
You should be doing something like
' Open the file for input.
Set MyFile = fso.OpenTextFile(FileName, ForReading)
' Read from the file and display the results.
Do While MyFile.AtEndOfStream <> True
TextLine = MyFile.ReadLine
Document.Write TextLine & "<br />"
Loop
MyFile.Close
https://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

Output content of text file to computer voice via batch file

I have this batch file:
#echo off
echo StrText="Application created Successfully" > spk.vbs
echo set ObjVoice=CreateObject("SAPI.SpVoice") >> spk.vbs
echo ObjVoice.Speak StrText >> spk.vbs
start spk.vbs
This batch file creates spk.vbs in the same directory and outputs the text "Application created Successfully" with the computer voice.
Now I want the batch file to speak out the content of a text file given to it on the command line instead (%1). And the spk.vbs file should be created in the default Windows temporary directory instead. How can I do this?
***Edit 06.11.2012 20:24
Meanwhile I've discarded the idea of using a batch file script to generate a vbs script file and want to use the vbs script directly. Although I am an absolute beginner with VBS I created this one:
Set objFSO = CreateObject("Scripting.FileSystemObject")
strAFile = Wscript.Arguments(0)
Set objFile = objFSO.GetFile(strAFile)
If objFile.Size > 0 Then
Set objReadFile = objFSO.OpenTextFile(Wscript.Arguments(0), 1)
strContents = objReadFile.ReadAll
objReadFile.Close
set ObjVoice=CreateObject("SAPI.SpVoice")
ObjVoice.Speak strContents
Else
Wscript.Echo "The file is empty."
End If
It works, but probaly I've made a lot of mistakes. Can someone tell me how the vbs script can be optimized? Thank you!
***Edit 06.11.2012 22:19
After this worked a few times, now it does not work anymore: Now the computer speaker outputs only "Y" and the first character of the text file! Has this something to do with an error in my script?
***Edit 10.11.2012 19:32
Found the bug: The above script work only with ANSI-coded text-files. It does not work with UNICODE text-files! Why? How can I make it work with UNICODE text-files too?
Use the 4th parameter of the .OpenTextFile (or the 2nd parameter of the .OpenAsTextStream) method to specify whether to open the file as ASCII or Unicode (16).
I don't find any serious mistakes in your code snippet, but perhaps you want to consider:
using "Option Explicit" (explicitly)
checking whether the user passed at least one argument to the script
avoiding to refer to the same 'object' via different names/variables (strAFile, WScript.Arguments(0))
using .OpenAsTextStream as you have a File object already
avoiding 'magic numbers' (e.g. 1) by defining the appropriate constants (e.g. ForReading)
avoiding unnecessary variables (code you don't write can't be wrong)
E.g:
Set objReadFile = objFSO.OpenTextFile(WScript.Arguments(0), 1)
strContents = objReadFile.ReadAll
objReadFile.Close
==>
Const cnUnicode = -1
...
strContents = objFile.OpenAsTextStream(ForReading, cnUnicode).ReadAll()

Resources