How to write from VBS to Batch!?

February 6, 2011 at 06:22:58
Specs: Windows 7, 12GHZ,12GB DDR5
Option Explicit ' Comment Me Out When Debugging
On Error Resume Next ' Comment Me Out When Debugging

Do
If Check() = vbTrue Then ' Cmd is Started
Sleep()
Else ' Cmd is Not Started
StartV()
End If
Loop

Function Check()
Dim objService , colProcess ' Declair Var!
Set objService = GetObject( "winmgmts:" ) ' GetObject SWbemServicesEx
For Each colProcess In objService.InstancesOf( "Win32_Process" ) ' Enum The Collection
If Not InStr( colProcess.Name , "cmd.exe" ) = vbFalse Then ' Check For "cmd"
Check = vbTrue
End If
Next ' Check Next Process
Set(objService)=Nothing ' Clean Up Object!
Set(colProcess)=Nothing ' Clean Up Var!
End Function

Function StartV()
Dim objShell , strProg ' Declair Var!
strProg = "Crap.bat" ' Set Var!
Set objShell = CreateObject( "WScript.Shell" ) ' Create Object
objShell.Run strProg ' Run Your Program
Set(objShell)=Nothing ' Clean Up Object!
Set(strProg)=Nothing ' Clean Up Var!
End Function

Function Sleep()
WScript.Sleep(1000*5) ' (1000 = 1 second)
End Function


and if Crap.bat not found .. Write all the code wich means it got to be bunch of lines it cant be like @echo off set a=b etc... it need to be alined like

@Echo off
set a=b

in the orginal code :) this is hard :D


See More: How to write from VBS to Batch!?

Report •


#1
February 7, 2011 at 04:31:31
Option Explicit

'Set Dimension
DIM fso

'Set Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Create Condition
If (fso.FileExists("Crap.bat")) Then
'Quit?
WScript.Quit()
Else
'Create the .bat
Set fso = CreateObject("Scripting.FileSystemObject")
'OpenTextFile Parameters:
'-Filename
'-The 2 is for writing... 1 is reading and 8 is appending
'-The "True" is to create if not already there.
Set fl = fso.OpenTextFile("Crap.bat", 2, True)
fl.WriteLine("REM NOOB :)")
fl.writeLine("@ECHO OFF")
fl.writeLine("Moar code ;)")
fl.Close : Set fl = Nothing
Set fso = Nothing
End If

'Exit Script
WScript.Quit()

works... but how to i add it to the above vbs without messin it up :s


Report •

#2
February 7, 2011 at 08:51:15
HERE IT IS!!! :)

Option Explicit ' Comment Me Out When Debugging
On Error Resume Next ' Comment Me Out When Debugging

Do
If Check() = vbTrue Then ' Cmd is Started
Sleep()
Else ' Cmd is Not Started
StartV()
End If
Loop

Function Check()
Dim objService , colProcess ' Declair Var!
Set objService = GetObject( "winmgmts:" ) ' GetObject SWbemServicesEx
For Each colProcess In objService.InstancesOf( "Win32_Process" ) ' Enum The Collection
If Not InStr( colProcess.Name , "cmd.exe" ) = vbFalse Then ' Check For "cmd"
Check = vbTrue
End If
Next ' Check Next Process
Set(objService)=Nothing ' Clean Up Object!
Set(colProcess)=Nothing ' Clean Up Var!
End Function

Function StartV()
Dim objShell , strProg ' Declair Var!
strProg = "Crap.bat" ' Set Var!
MakeSureBatExists strProg
Set objShell = CreateObject( "WScript.Shell" ) ' Create Object
objShell.Run strProg ' Run Your Program
Set(objShell)=Nothing ' Clean Up Object!
Set(strProg)=Nothing ' Clean Up Var!
End Function

Function Sleep()
WScript.Sleep(1000) ' (1000 = 1 second)
End Function

Private Sub MakeSureBatExists(Crapbat)
'Set Dimension
Dim fso, fl

'Set Object
Set fso = CreateObject("Scripting.FileSystemObject")

'Create Condition
If Not fso.FileExists(Crapbat) Then
'OpenTextFile Parameters:
'-Filename
'-The 2 is for writing... 1 is reading and 8 is appending
'-The "True" is to create if not already there.
Set fl = fso.OpenTextFile(Crapbat, 2, True)
fl.WriteLine("REM NOOB :)")
fl.writeLine("@ECHO OFF")
fl.writeLine("PAUSE")
fl.Close
Set fl = Nothing

End If

Set fso = Nothing
End Sub


Report •

Related Solutions


Ask Question