VBS - Weird Japanese characters conversion - vbscript

I've been searching Google and StackOverflow and did find some partial answers to my problem. Here is my question:
I've bee n trying to make a small script that will promt the user for keywords and once you hit enter it will parse the array into Chrome or Firefox and open up tabs with all the various searches. It works! But, then I wanted to expand on that so I could make a .txt file and make it read from that, that works too! BUT since it's a Japanese site we're searching, Japanese will yield better results!
So I got the ForReading and TriState/Unicode = -1 to work, and it does issue something in Japanese, the problem is it doesn't issue the characters I inserted into the document. :/ And I'm at a loss at the moment. Here's the part of the code that I need some help with:
'Declaring
Option Explicit
Const ForReading = 1
Const Unicode = -1
Dim FullPath
Dim objShell
Dim SearchArray
Dim Figure
Dim Index
Dim listWord
Dim Mandarake
Dim invalidName
Dim rawImport
Dim objFSO
Dim objTextFile
FullPath = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(FullPath&"\mandalist.txt", ForReading,True,Unicode)
Set objShell = CreateObject("Shell.Application")
SearchArray = Array()
mandarake = "http://ekizo.mandarake.co.jp/shop/en"
'Starting do loop until we trigger the search with "go"
Do
ReDim Preserve SearchArray(UBound(SearchArray)+1)
Figure = InputBox("Enter a name or type 'go' when you're done!", "Figure search", "List")
SearchArray(UBound(SearchArray)) = "http://ekizo.mandarake.co.jp/shop/en/search.do?action=keyword&doujin=all&keyword="&Replace(Figure," ","+")
'Trying to handle empty or "Name" entry into array error
If Figure = Empty Then
invalidName = MsgBox("Are you sure you want to quit?",1,"Are you sure?")
If invalidName = vbOK Then
WScript.Quit (0)
End If
ElseIf Figure = "List" Then
Do Until objTextFile.AtEndOfStream
rawImport = objTextFile.Readline
Figure = Replace(rawImport," ","+")
SearchArray = Split(Figure , ",")
Call objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", mandarake, "", "", 1)
For Index = 0 To UBound(SearchArray)
Call objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "http://ekizo.mandarake.co.jp/shop/en/search.do?action=keyword&doujin=all&keyword="&SearchArray(index), "", "", 1)
WScript.Sleep(1500)
Next
WScript.Quit (0)
Loop
I have more code hence all the dims and set's, but that is if the If Figure = "List" is not true and it will then continue to the insertion of manual keywords, and those works even with Japanese signs. The thing is, no matter what I paste into the text file it will parse other characters (also some Japanese stuff) into the URL instead of what I put in. How can I get it to treat the charset as it's supposed to?
Example would be:
Text in Mandalist.txt = グリフォン アスナ
Text in URL in Chrome = 믯낂菣閃苣뎃ꊂ苣誃 - HMF :s
Any help would be much appreciated.
PS - This is my first project where I don't rely on a piece of paper that my teacher wrote me in school, so it may be simple but I just can't make it work.
Thanks
/CuraeL

Related

Calling windows command prompt and use find to check if a string is in a text file

I want to use:
Set cmdLine = CreateObject("WScript.Shell")
Then I want to do something like:
check = cmdLine.Exec("%comspec% /C find /N "End of Report" D:\test3.txt)
But I am not very familiar with coding in the command prompt... so I don't really know what I am doing. But I want to search for a string of text in a .txt file and see if it exist or not... I don't really need a to know what line number it is found on or anything... just if it was found or not.
PS- I am going about it this way to avoid having to open the text file if the string isn't found.
I read that find has one of three %errorlevel% responses.
0 – The string you were searching for was found.
1 – The string you were searching for was not found.
2 – This means you had a bad switch or your parameters were incorrect.
I don't really know what they mean by %errorlevel% responses... but I am hoping to do something like"
if check = 0 then
'do something
End if
meaning... If the string I am searching for is found... then do something...
Try the following. It uses VBScript to retrieve all the text in a file as suggested in the comments above, and does a case-insentivie search for a specific search string. Modify it as required:
Option Explicit
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextStream, allText
Dim searchTerm : searchTerm = "End of Report"
const strFileName = "D:\test3.txt"
const fsoForReading = 1
If objFSO.FileExists(strFileName) then
Set objTextStream = objFSO.OpenTextFile(strFileName, fsoForReading)
allText = objTextStream.ReadAll
if instr(1,allText,searchTerm,1) > 0 Then
MsgBox "Found"
end if
objTextStream.Close
Set objTextStream = Nothing
End If
Set objFSO = Nothing
Here was the solution i found that worked for my purposes.
Dim cmdLine As Object
Dim result As String
Dim SearchStr As String
Dim FilePath As String
Set cmdLine = CreateObject("WScript.Shell")
result = cmdLine.Exec("%comspec% /C Find " & SearchStr & " " & Chr(34) & FilePath & Chr(34)).STDOut.ReadAll

vbscript does not execute as desired

i've been googling the last couple of days for a solution to my problem.
There seems to be a ton of questions asked on this topic but somehow they are not working for me or (most likely) i'll be doing something wrong.
Const OverwriteExisting = True
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim FSO, MyFile, FileName, strFileName, strCopyTo
Set FSO = CreateObject("Scripting.FileSystemObject")
strFileName = "C:\......SourceFolder\SourceFile.html"
strCopyTo = "T:\....DestinationFolder\"
FileName = "T:\........\autorun.inf"
If FSO.FileExists(strFileName) Then
FileSystemObject.CopyFile strFileName, strCopyTo, True
'FSO.CopyFile strFileName, strCopyTo, OverwriteExisting
Else
For i = 1 to 3
If FSO.FileExists(strFileName) Then
FileSystemObject.CopyFile strFileName, strCopyTo, True
else
WScript.sleep 1000 '1000 milliseconds
End if
Set MyFile = FSO.OpenTextFile(FileName, ForAppending, True, TristateTrue)
' Write to the file.
MyFile.WriteLine "Attempt " & i & " - " & Now
MyFile.Close
Next
End If
What i'm trying to do is copying a file from the source folder located on C:\ to a shared network drive called T:.
So i started the script with checking if the file exist, if it exists than it will have to copy it to the destination folder.
If not i would like to try 3 more times with each at least 1 second a part from each other. for that i'm using the FOR method and WScript.sleep function. And if it didn't exist i'm writing it to a file.
Everything is working fine, except for the most important thing, the file even though it does exist it does not get copied.
I've checked the attributes on the origin and destination file, as according to google, the read-only attribute would prevent the file from being copied even when in the copy function "overwrite" is set to TRUE.
So i do not know where to look right now and would greatly appreciate any help.
Thanks
Philippe
I'm going to go ahead and post this as an answer instead of a comment, because I'm pretty sure it's what's causing your problem.
In the lines where you're trying to do the copying, you have
FileSystemObject.CopyFile [...]
However, at no point do you define a variable, let alone an object, named FileSystemObject. Your file system object is actually defined as
Set FSO = CreateObject("Scripting.FileSystemObject")
That's FSO, not FileSystemObject. Try changing your CopyFile lines to:
FSO.CopyFile strFileName, strCopyTo, True
If you started your code with <%Option Explicit%>, the interpreter ("compiler") would catch these sorts of errors for you.

VBS - How to check if a word is existing in a registry Data, and then delete it?

I must have a vbs script which can delete the string ",vmhgfs" (the coma is important) if it exists in the a registry data.
The registry key is:
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order"
The registry value is "ProviderOrder" (it's a string value)
The data is something like "PICAClientNetwork,SnacNp,vmhgfs,RDPNP,LanmanWorkstation,webclient,PnSson" but can be very different from a computer to another.
The script must check if the string ",vmhgfs" exists in this data, and if it exists, delete it. If it don't exist, just end.
I'm newbie in VBS, and I've begun to write this script:
Dim objShell, RegValue, RegData
RegValue = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder"
set objShell = CreateObject("Wscript.Shell")
RegData = objShell.RegRead(RegKey)
wscript.echo RegData
The returned echo show me i'm in the right way... but not enough skill to go further...
Can you please help me by finishing it ! Many thanks in advance !
EDIT (before applying your advices):
Hi, thanks guys so I've written the script:
Dim objShell, RegValue, RegData, NewRegData
RegValue = "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder"
set objShell = CreateObject("Wscript.Shell")
RegData = objShell.RegRead(RegValue)
if Instr(RegData,",vmhgfs") > 0 then
NewRegData = Replace(RegData,",vmhgfs", "")
objshell.Regwrite RegValue ,NewRegData
Else IF Instr(RegData,"vmhgfs") > 0 then
NewRegData2 = Replace(RegData,"vmhgfs,", "")
objshell.Regwrite RegValue ,NewRegData2
End If
set objshell = nothing
The first "IF" is to manage the case where ",vmhgfs" is in the middle of the string. OK The second "IF" is to manage the case where "vmhgfs" is at the beginning of the string
BUT THIS DOESN'T WORK IF ",vmhgfs" IS AT THE VERY END OF THE STRING !!!
I don't undestand that, please help !
How about this?
if InStr(1,RegData,",vmhgfs") > 0 then
NewRegData = left(RegData,InStr(1,RegData,",vmhgfs")) & right(RegData,7+InStr(1,RegData,",vmhgfs"))
or (as per Ansgar :) )
if InStr(1,RegData,",vmhgfs") > 0 then
newRegData = Replace(RegData, ",vmhgfs", "")
Then you just have to write it back to the registry
I'd use a regular expression instead of InStr here, because the latter would also (mis)detect things like ,vmhgfsFOOBAR.
Set sh = CreateObject("Wscript.Shell")
Set re = New RegExp
re.Global = True
val = "HKLM\SYSTEM\CurrentControlSet\Control\NetworkProvider\Order\ProviderOrder"
re.Pattern = "(^|,)vmhgfs(,|$)"
data = re.Replace(sh.RegRead(val), ",")
re.Pattern = "^,|,$"
sh.RegWrite val, re.Replace(data, ""), "REG_SZ"

Opening Word from VBScript hangs, can't figure out why

I'm not really a programmer by trade, so forgive me if I'm not aware of any standard debugging tools.
I have what I thought was a very simple VBScript (just a txt file saved with a .vbs extension):
Const wdDoNotSaveChanges = 0
Const wdRevisionsViewFinal = 0
Const wdFormatPDF = 17
Dim arguments
Set arguments = WScript.Arguments
Function DOC2PDF(sDocFile)
Dim fso ' As FileSystemObject
Dim wdo ' As Word.Application
Dim wdoc ' As Word.Document
Dim wdocs ' As Word.Documents
Set fso = CreateObject("Scripting.FileSystemObject")
sDocFile = fso.GetAbsolutePathName(sDocFile)
sPdfFile = fso.GetParentFolderName(sDocFile) + "\" + fso.GetBaseName(sDocFile) + ".pdf"
Set wdo = CreateObject("Word.Application")
Set wdocs = wdo.Documents
Set wdoc = wdocs.Open(sDocFile)
if fso.FileExists(sPdfFile) Then
fso.DeleteFile sPdfFile, True
End If
Set wview = wdoc.ActiveWindow.View
wview.ShowRevisionsAndComments = False
wview.RevisionsView = wdRevisionsViewFinal
wdoc.SaveAs sPdfFile, wdFormatPDF
wdo.Quit wdDoNotSaveChanges
Set fso = Nothing
Set wdo = Nothing
End Function
however, the following line is causing huge grief:
Set wdoc = wdocs.Open(sDocFile)
Sometimes the Word ActiveX object just freezes at this step. I've verified this by some super-simple debugging by putting a WriteLine after each line and seeing where it stops.
Word just sits there consuming 100% CPU, and the script never gets past that step.
How can I go about debugging to find out what the hell is going on with the Word ActiveX object and why it's just hanging and never returning?
Word might be waiting for a prompt from you. I would make Word visible and see if you can visually see what the problem is:
Set wdo = CreateObject("Word.Application")
'if memory serves, this should make Word visible
wdo.Visible = true
Set wdocs = wdo.Documents

checking format to a text box

I need a method to check the contents of the text entered to make sure they are correctly entering a folder path. So it needs to be in the format of:
Drive Letter :\ Folder
e.g. C:\My Documents
If they haven't typed in that format I need to stop and show a message telling them to double check.
I have tried the Filter function but I haven't quite got it to work. Any help would be awesome. I don't have any code to show because I am nto sure where to start.
I also tried the common dialog, but the user jsut needs the type the path, not select the file. All I want to check is if the text type is within that format DRIVE:\FOLDER, that is it. So if the type "BLAH" in the text bax a message says Hey you type a correct path.
In VB6, to test whether your text contains a valid folder:
If Len(Dir("c:\My Documents", vbDirectory))>0 Then
'it's a folder
End If
Have you thought of implemeting the common dialog control to allow the selection of a correct folder instead - it'll be much more likely to be accurate.
Some example code of folder browsing from here:
Private Sub Command1_Click()
On Error Resume Next
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "select folder:", NO_OPTIONS, "C:Scripts")
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
objPath = Replace(objPath, "", "\")
Print objPath
End Sub
Alternatively you could validate the folder first you could check for ":\" using eith instr or mid
then you could validate the folder and even include an option to create it if not present with the filesystemobject (needs a reference set) here it is in function form, you can pass the contents of the textbox for validation.
Function DirExists(pFile As String, Optional pCreate As Boolean = False)
'
Dim fso As New FileSystemObject
Dim vPath As Variant
Dim sPath As String
Dim y As Variant
DirExists = False
If fso.FolderExists(pFile) Then
DirExists = True
Else
If pCreate Then
vPath = Split(pFile, "\")
For Each y In vPath
sPath = sPath & y & "\"
If Not fso.FolderExists(sPath) Then
fso.CreateFolder (sPath)
If fso.FolderExists(pFile) Then
DirExists = True
Exit Function
End If
End If
Next
End If
End If
End Function

Resources