Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
My code doesn't work
i am trying show my friends
this is the code it is a message box
Dim objShell, strComputer, strInput
Dim strRestart
pass=inputbox( "test time! what is 2+2?" )
if pass="4" then
msgbox=( "you is smart" )
else
DO
msgbox( "you cant do math now i will self destruct" )
Set WSHShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
The first error you have: Move your Dim strRestart to a newline.
I.E: Instead of
Dim objShell, strComputer, strInput Dim strRestart
Use:
Dim objShell, strComputer, strInput
Dim strRestart
Second error is on line 4:
msgbox=("you is smart")
it should be
msgbox("you is smart")
Third error is on line 6:
Remove the DO. The DO is part of a DO .. WHILE statement for looping,
and it seems that you only want to have an if/else clause.
All in all:
Dim objShell, strComputer, strInput
Dim strRestart
pass=inputbox( "test time! what is 2+2?" )
if pass="4" then
msgbox( "you is smart" )
else
msgbox( "you cant do math now i will self destruct" )
Set WSHShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
end if
Cheers.
Related
This question already has answers here:
How can I show a message box with two buttons?
(8 answers)
Closed 1 year ago.
I was creating a vbs but after i click Yes in msgbox, it instead does what the "No" is supposed to do.
Here is my code:
X=MsgBox("Please Restart. Our system will break down.",4+4096, ErR0r)
if vbNo then
X=MsgBox("e")
dim count
set object = wscript.CreateObject("wscript.shell")
do
object.run "error.vbs"
count = count + 1
loop until count = 100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
end if
if vbYes then
Dim IntCounter
Dim objWshShl : Set objWshShl = WScript.CreateObject("wscript.shell")
Dim objVoice : Set objVoice = WScript.CreateObject("sapi.spvoice")
ShutdownWarning()
TimedMessageBox()
ShutdownComputer()
Function ShutdownWarning
X=MsgBox("happy late april fools, hehe", 0+4096)
WScript.Sleep 5000
End Function
Function TimedMessageBox
For IntCounter = 5 To 1 Step -1
objWshShl.Popup "Your time will be wasted in " _
& IntCounter & " seconds",1,"Computer Shutdown", 0+4096
Next
End Function
Function ShutdownComputer
objWshShl.Run "Shutdown /s /f /t 0",0
End Function
end if
does anyone have solutions to this?
(im not trying to create a legitimate virus, this is just a prank i made for fun)
vbNo is a constant value equal to 7.
if vbNo will always be true, since 7 is not a falsy value.
You need to use a variable to get the return value of the user input, such as :
userInput = msgBox("click yes or no", vbYesNo)
if userInput = vbNo Then
' do something
end if
This question already has answers here:
File checking on Windows Startup is failing [duplicate]
(2 answers)
Closed 2 years ago.
I have this script (below), not the best script but it works fine, however every time I restart my computer that new registry triplet is gone and I have no guess why.
I got no errors from this script, but if I let it run for a reasonably period of time a vbs Msgboxpops up with,
This script contains malicious content and has been blocked by your antivirus software.
I honestly don't think it is related but apparently I cannot post a question being concise due to text requirements limitations. Or is it related and the antivirus is wiping out that triplet? After this message the new register is still there (in the registry) but not after a restart.
Dim sKey
sKey = "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\so_Robocopy"
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim wshShell
Set wshShell = CreateObject("WScript.Shell")
If fso.FileExists("so_Robocopy.bat") Then
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & "so_Robocopy.bat" & Chr(34), 0
WScript.Sleep 300000
Loop
Else
RemoveFromRegistry()
End If
Function RemoveFromRegistry()
On Error Resume Next
wshShell.RegDelete sKey 'Error handling routine
End Function
Function RegisterOnWindowsStartUp()
If DoesRegistryExist = False Then
wshShell.RegWrite sKey, Chr(34) & WScript.ScriptFullName & Chr(34), "REG_SZ"
End If
End Function
Function DoesRegistryExist()
with CreateObject("WScript.Shell")
on error resume next
sValue = .regread(sKey)
DoesRegistryExist = (err.number = 0)
on error goto 0
End with
End Function
When you run the script, it will work fine, and no problem because you are running the script on the current directory and the so_Robocopy.bat existed on the same directory.
However, on Windows Startup, the script will execute on the Directory of Windows Startup and not on the original directory where your script is located.
Here's what happened to your code,
' Script execute from the Directory of Windows Starup
If fso.FileExists("so_Robocopy.bat") Then ' (1) There will be no so_Robocopy.bat on the Directory of Windows Startup, then this will return false.
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & "so_Robocopy.bat" & Chr(34), 0
WScript.Sleep 300000
Loop
Else (2) The condition is false, then remove the key from registry.
RemoveFromRegistry()
End If
Make sure you are looking from the original directory where your script is. You can use Scripting.FileSystemObject and WScript.ScriptFullName for that.
(1)
so_robocopy_file = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\so_Robocopy.bat"
If fso.FileExists(so_robocopy_file) Then ' (2)
RegisterOnWindowsStartUp()
MsgBox "Backup message text"
Do While True
wshShell.Run Chr(34) & so_robocopy_file & Chr(34), 0 ' (3)
WScript.Sleep 300000
Loop
Else
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I would like to know how to write text in a middle of a file in vbscript.
The text file has 2 lines, one line for the name of the output and the second of the value. The outputs separated by ";"
For example :
Before insert text, the text file contain -
mem1;mem2;mem3;
0.15;15.5;12.3;
After insert new text -
mem1;mem2;mem3;mem4
0.15;15.5;12.3;13.2
Thanks of helping me!
P.S. - note that it should be txt file and not csv.
I have this snippet:
'Usage:
'If ReplaceInFile(filename, search, replace, addToEnd) = 0 Then
' WScript.Echo "Succeeded"
'End If
Function ReplaceInFile(strFilename, strSearch, strReplace, addToEnd)
Dim fso, objFile, oldContent, newContent
'Does file exist?
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(strFilename) = False Then
ReplaceInFile = 0
Exit Function
End If
'Read file
Set objFile = fso.OpenTextFile(strFilename, 1)
oldContent = objFile.ReadAll
'Write file
newContent = replace(oldContent, strSearch, strReplace, 1, 1, 0)
newContent = newContent & addToEnd
Set objFile = fso.OpenTextFile(strFilename, 2)
objFile.Write newContent
objFile.Close
ReplaceInFile = 0
End Function
So you can use like this:
If ReplaceInFile("your file path", ";\r\n", "mem4", "13.2") = 0 Then
WScript.Echo "Succeeded"
End If
WARNING!!
This assumes that the last line does not have a new line at the end!
I am trying to launch "GetPID.exe" and store the error code.
This is the .vbs code :
Set WshShell = CreateObject("WScript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
strFolder = strFolder & "\GetPID.exe"
Msgbox strFolder
Dim temppid
temppid = WshShell.Run(strFolder , 0 , True)
However, at the last line, I get the following message:
G:\test001.vbs(11, 1) (null): No application is associated with
the specified file for this operation.
The MsgBox echoes the correct path to the exe, so I am confused why is there the error.
Avoid answering questions in comments? I agree myself but this thesis seems to be considered opinion-based here.
However, I agree yet another (outweighing) topical sentence: a guess (a hunch) is not an answer. In other words, I like generally applicable, well-explained solutions in well-sourced answers.
Unfortunately, due to lacking information on GetPID.exe, I can neither explain nor source this forced answer: as a matter of general principle, enclose the first parameter of Run method in a pair of double quotes.
You could use next syntax:
temppid = WshShell.Run( """" & strFolder & """", 0 , True)
I am playing with VBScript and I want to make a MsgBox which asks the user if they want to shut down their computer or not.
If the user clicks Yes they should see a MsgBox first then their computer starts to shutdown.
I am using this code but it doesn't work.
What is the problem?
result = MsgBox ("Shutdown?", vbYesNo, "Yes/No Exm")
Select Case result
Case vbYes
MsgBox("shuting down ...")
Option Explicit
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
Case vbNo
MsgBox("Ok")
End Select
I have amended your code as per below:
Option Explicit
Dim result
result = MsgBox ("Shutdown?", vbYesNo, "Yes/No Exm")
Select Case result
Case vbYes
MsgBox("shuting down ...")
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
Case vbNo
MsgBox("Ok")
End Select
The main issues were that "option explicit" has to be at the top, and as a result the "result" variable then must be declared using the "dim" keyword. The above code works fine when I executed it via the command line.
I also added a timeout of 20, but you can easily change this back to the original value of 0.
As documented Option Explicit must appear before any other statement in a script. Using it anywhere else in a script should raise a "Expected Statement" error pointing to the line with the Option Explicit statement. If you don't get that error, you have an On Error Resume Next in your code that you didn't show.
If you move the Option Explicit statement to the beginning of the script, but the shutdown still doesn't occur, you need to check the return value of the shutdown command:
rc = objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0", 0, True
If rc <> 0 Then MsgBox "shutdown failed with exit code " & rc & "."
The parentheses in your MsgBox statements shouldn't cause an issue as long as you pass just a single argument to the function, but I'd still remove them.
Try This:
Set Shell = CreateObject("WScript.Shell")
Answer = MsgBox("Do You Want To" & vbNewLine & "Shut Down Your Computer?",vbYesNo,"Shutdown:")
If Answer = vbYes Then
Shell.run "shutdown.exe -s -t 60"
Ending = 1
ElseIf Answer = vbNo Then
Stopping = MsgBox("Do You Wish To Quit?",vbYesNo,"Quit:")
If Stopping = vbYes Then
WScript.Quit 0
End If
End If