VBScript behaves weirdly using SendKeys to send a new line - shell

I'm working on a very simple VBS script and I've ran into a really weird problem:
I was originally using this code:
Set wshShell=wscript.CreateObject("WScript.Shell")
WScript.Sleep(2000)
Dim i
i = 0
Dim val
val = 25
Do While i < 5
wshshell.SendKeys("What the")
WScript.Sleep(val)
wshshell.SendKeys("{ENTER}")
WScript.Sleep(val)
i = i + 1
Loop
But for some reason, the output was this:
What theWhat theWhat theWhat theWhat the
But when I change this:
wshshell.SendKeys("What the")
To this:
wshshell.SendKeys("What the ")
It works fine and outputs:
What the
What the
What the
What the
What the
What's causing that? I'm extremely baffled as to why that happens. Thanks.

Related

vbscript won't read file after 8Mb

I have a file written in vbs that wont read a file after about 8MB. I am currently using "Scripting.FileSystemObject". When I test the code, I notice that it runs fine until line ~79500, thats when the "AtEndOfStream" just results in True. I was looking for documentation, but it seems not to exist.
The code is supposed to show duplicate file information and put it in a separate file, which works well enough till around that line.
This is the section of code giving me the problem (it is the second reading function I have in the code):
Set first = fso.OpenTextFile(filePath + firstFileName)
Set secondFile = fso.OpenTextFile(filePath + secondFileName)
count = 0
countInLine = 0
Do Until secondFile.AtEndOfStream
lineMatches = false
lineOfSecond=secondFile.ReadLine
If count > 79440 Then
MsgBox("first line" & first.AtEndOfStream)
End If
Do Until first.AtEndOfStream
lineOfFirst =first.ReadLine
if lineOfSecond = lineOfFirst Then
lineMatches = True
Exit Do
End If
Loop
If Not lineMatches Then
writeFl.Write(count & "second" & lineOfSecond & vbCrLf)
End If
count = count + 1
Loop

Stopping Vbscript After Completion

Im making a game called cube, when I noticed a problem. I looked at Task Manager Fr Any Extra Programs, when I noticed tons of vbsscript running. I tried searching around, but no scripts worked for me. Heres my code (It plays A Sound)
Set Sound = CreateObject("WMPlayer.OCX.7")
Sound.URL = "Sounds\Hurt.mp3"
Sound.Controls.play
do while Sound.currentmedia.duration = 0
wscript.sleep 100
loop
wscript.sleep (int(Sound.currentmedia.duration)+1)*1000
WScript.Quit 1
It still doesn't want to stop. Please Help.
Try the below example
With CreateObject("WMPlayer.OCX.7")
.URL = "C:\Users\DELL\Desktop\tmp\hit.wav"
.controls.play
Do
WScript.Sleep 100
Loop Until .playState = 1 Or .playState = 10
' .PlayState https://msdn.microsoft.com/en-us/library/windows/desktop/dd564085(v=vs.85).aspx
' "Stopped" (completed) or "Ready" (file not found)
End With

Reading and writing an INI file

I have been toying with the below script to be able to read settings for use with my HTA (creating a game launcher).
Here is my current HTA:
http://pastebin.com/skTgqs5X
It doesn't quite work, it complains of the WScript object being required. While I understand Echo will not work like that in a HTA I am having trouble modifying the code so it will work. Even just removing all Echo references it still has an issue with objOrgIni on line 200 of the below code (with the WScript references removed):
http://pastebin.com/pGjv4Gh1
I don't even need that level of error checking as the INI will exist etc, I just need a simple way to read from and write to an INI in my scripting. Any help you guys can give me in achieving that would be great, it's a little advanced for me just yet, but I'd love an explanation as to why it fails.
There is no easy way to use INI files with VBScript. You'd have to write the functionality yourself or find some existing code that does it.
But do you really need an INI specifically or just a way to save settings? You could just keep all of your settings in a Dictionary object and serialize it as needed.
For example, here are two functions -- LoadSettings and SaveSettings -- that do just that.
Public Function LoadSettings(strFile)
Set LoadSettings = CreateObject("Scripting.Dictionary")
Dim strLine, a
With CreateObject("Scripting.FileSystemObject")
If Not .FileExists(strFile) Then Exit Function
With .OpenTextFile(strFile)
Do Until .AtEndOfStream
strLine = Trim(.ReadLine())
If InStr(strLine, "=") > 0 Then
a = Split(strLine, "=")
LoadSettings.Add a(0), a(1)
End If
Loop
End With
End With
End Function
Sub SaveSettings(d, strFile)
With CreateObject("Scripting.FileSystemObject").CreateTextFile(strFile, True)
Dim k
For Each k In d
.WriteLine k & "=" & d(k)
Next
End With
End Sub
Imagine you had the following settings file saved at c:\settings.txt:
Count=2
Name=Obama
You'd use the functions above like this:
Const SETTINGS_FILE = "c:\settings.txt"
Dim Settings
Set Settings = LoadSettings(SETTINGS_FILE)
' Show all settings...
WScript.Echo Join(Settings.Keys, ", ") ' => Count, Name
' Query a setting...
WScript.Echo Settings("Count") ' => 2
' Update a setting...
Settings("Count") = Settings("Count") + 1
' Add a setting...
Settings("New") = 1
' Save settings...
SaveSettings Settings, SETTINGS_FILE

Call out to script to stop with attribute in wWWHomePage

I'm gettinga n error message in line 8 when I try to call out the script to stop when it finds teh attribute in the Web page: field in AD.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUserDN)
strwWWHomePage = objItem.Get("wWWHomePage")
If wWWHomePage 6 Then
wscript.quit
Else
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
You have:
If wWWHomePage 6 Then
I'm assuming you want it to say:
If wWWHomePage = 6 Then
Since the missing "=" will cause an error, but since that code really doesn't do anything anyway, other than just abort the script, you could simplify your code by only taking action if that value is not set, for example:
If objItem.Get("wWWHomePage") <> 6 Then
Set ppt = CreateObject("PowerPoint.Application")
ppt.Visible = True
ppt.Presentations.Open "\\abngan01\tracking\ppt.pptx"
End If
I'm also assuming "6" is some sort of flag you've set yourself, you might want to use something a little more descriptive like "PPTSTATUS006", or something along those lines.

How do I increment a value in a textfile using the regular Windows command-line?

I'd like to keep a "compile-counter" for one of my projects. I figured a quick and dirty way to do this would be to keep a text file with a plain number in it, and then simply call upon a small script to increment this each time I compile.
How would I go about doing this using the regular Windows command line?
I don't really feel like installing some extra shell to do this but if you have any other super simple suggestions that would accomplish just this, they're naturally appreciated as well.
You can try a plain old batchfile.
#echo off
for /f " delims==" %%i in (counter.txt) do set /A temp_counter= %%i+1
echo %temp_counter% > counter.txt
assuming the count.bat and counter.txt are located in the same directory.
It would be an new shell (but I think it is worth it), but from PowerShell it would be
[int](get-content counter.txt) + 1 | out-file counter.txt
I'd suggest just appending the current datetime of the build to a log file.
date >> builddates.txt
That way you get a build count via the # of lines, and you may also get some interesting statistics if you can be bothered analysing the dates and times later on.
The extra size & time to count the number of lines in the file will be insignificant unless you are doing seriously fast project iterations!
If you don't mind running a Microscoft Windows Based Script then this jscript will work OK. just save it as a .js file and run it from dos with "wscript c:/script.js".
var fso, f, fileCount;
var ForReading = 1, ForWriting = 2;
var filename = "c:\\testfile.txt";
fso = new ActiveXObject("Scripting.FileSystemObject");
//create file if its not found
if (! fso.FileExists(filename))
{
f = fso.OpenTextFile(filename, ForWriting, true);
f.Write("0");
f.Close();
}
f = fso.OpenTextFile(filename, ForReading);
fileCount = parseInt(f.ReadAll());
//make sure the input is a whole number
if (isNaN(fileCount))
{
fileCount = 0;
}
fileCount = fileCount + 1;
f = fso.OpenTextFile(filename, ForWriting, true);
f.Write(fileCount);
f.Close();

Resources