Link to home
Start Free TrialLog in
Avatar of Ben Weber
Ben Weber

asked on

Run a .bat file wich is inside a .vbs file

I just started learning to program so please don't judge me for possibly dumb faults. Thank you :D

I have got this .vbs file:

name=inputBox("Wer spricht da mit mir?")
MsgBox("Hallo "+name)
name=inputBox("Ich bin Loga. Wie geht's dir?")
name=inputBox("Ok und was hast du heute so gemacht?")
name=inputBox("Ist ja interessant! Du ich bemrke grade da fasudk istjkyshxb igrjn was mµb µbh was nihct stimmtbvj...")
X=MsgBox("Virus detected. Delete Virus?",16,"Loga_Backup")

and now I want to run a .bat file after that. This is it:

@echo off
echo If the proper Username and Password is not entered all files from C:/ will be deleted by this virus.
echo Good Luck
set/p\/name=Username:
set/p\/password=Password:
echo haha you got the username and password wrong
pause
echo Files are being deleted
pause
dir/s
del "c:up.bat"

now i just tried to copy and paste the .bat file into the original .vbs file wich looked like this:

name=inputBox("Wer spricht da mit mir?")
MsgBox("Hallo "+name)
name=inputBox("Ich bin Loga. Wie geht's dir?")
name=inputBox("Ok und was hast du heute so gemacht?")
name=inputBox("Ist ja interessant! Du ich bemrke grade da fasudk istjkyshxb igrjn was mµb µbh was nihct stimmtbvj...")
X=MsgBox("Virus detected. Delete Virus?",16,"Loga_Backup")

@echo off
echo If the proper username and password is not entered all files will be deleted by this virus.
echo Good Luck
set/p\/name=Username:
set/p\/password=Password:
echo haha you got the username and password wrong
pause
echo Files are being deleted
pause
dir/s
del "c:up.bat"


Do you hav an idea how i can combine them without giving me an error?

Thank you so much and sry for the long text
Avatar of JesterToo
JesterToo
Flag of United States of America image

This "wrapper" for running a shell command should do it... unless I have a typo... just keyed it in off top of my head.

Option Explicit

Dim oFSO, oShell, sBatFileName

   sBatFileName = "YourBatchFileName.bat"

   Set oFSO = CreateObject("Scripting.FileSystemObject")
   Set oShell = CreateObject("Wscript.Shell")

''''-- put your existing code here...

''''-- execute your bat file like this...

   oShell.Run("cmd /c " & YourBatchFileName & Chr(34)),0
   Wscript.Echo "Finished!"

   Set oFSO = Nothing
   Set oShell = Nothing

Open in new window

Oh, and don't put you bat file contents in the vbs file... put it in a separate text file with an extension of either .bat or .cmd
ASKER CERTIFIED SOLUTION
Avatar of JesterToo
JesterToo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ben Weber
Ben Weber

ASKER

So now i have this code:

Dim oFSO, oShell, sBatFileName
sBatFileName = up.bat

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("Wscript.Shell")

@echo off
echo if the proper username and password is not entered all files will be deleted!
echo Good Luck
set/p\/name=username:
set/p\/password=password:
echo haha you got the username and password wrong
pause
echo files are beeing deleted
pause
dir/s
del "C:up.bat"

Dim name, x

name=inputBox("Wer spricht da mit mir?")
MsgBox("Hallo "+name)
name=inputBox("Ich bin Loga. Wie geht's dir?")
name=inputBox("Ok und was hast du heute so gemacht?")
name=inputBox("Ist ja interessant! Du ich bemrke grade da fasudk istjkyshxb igrjn was mµb µbh was nihct stimmtbvj...")
X=MsgBox("Virus detected. Delete Virus?",16,"Loga_Backup")

oShell.Run("cmd /c " & Chr(34) & up.bat & Chr(34)),1,true

Set oFSO = Nothing
Set oFSO = Nothing

Open in new window


but it still errors me off...has anyone got an idea what now my fault is?
Error: @ in line 7 not accepted.

I'm using windows 7 by the way
You still have all the BAT file statements in the VBS file... remove them.  They only belong in the BAT file which you have named "up.bat".  And, you have a syntax error on line 2 of the VBS file... it should read as   sBatFileName = "up.bat"

Notice the quotes around the filename... that is how you designate a string in vbscript.
Thank you Jester! It finally works!
Creds to him: JesterToo https://www.experts-exchange.com/members/JesterToo.html
You're welcome Ben

One final note... I noticed that you removed the first line of the example I sent you... every VBS file should begin with the statement
"option Explicit".  The purpose of that is to force you to declare every variable you use.  It prevents many mistakes caused by mis-typing and will save you lots of time trying to debug a script.
Thank you for that information. I just added that and I hope that I'll never have problems with that again.