progressiveCavemen
progressiveCavemen

Reputation: 529

Run batch CODE in a vbscript file

I am trying to make a vbscript file that can run batch code (Note: Not a batch file, but batch code)

The code, which works in a batch file:

IF EXIST "%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms" ( 
"%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms" 
) ELSE (start /b "" cmd /c del "%~f0"&exit /b)

I can make the vbscript code almost do what I want using:

Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\myscript.bat" & Chr(34), 0
Set WshShell = Nothing

Now I would like to combine these two pieces of code into one file, so something along the lines of:

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Exec "IF EXIST ""%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms"" (""%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms"") ELSE (start /b """" cmd /c del ""%~f0""&exit /b)" 
Set WshShell = Nothing 

However when I run this code I get The system cannot find the file specified. This is expected, since Exec (or Run, or Execute) runs a batch file and not batch code. So, is there a command similar to Exec that will run batch code and not a batch file?

Some extra info that I don't think is necessary to a solution (But included for the sake of completedness):

I am open to other solutions besides vbscript, provided they can check if the .appref file exists, run the file if so, and delete itself if the file doesn't exist. This may be trivial in vbscript but I've never used vbscript before.

EDIT: According to @Jason's comment, I have modified the code as follows. Now it runs with no output and without running my app (AKA it doesn't do $#!+)

Set WshShell = CreateObject("WScript.Shell" ) 
WshShell.Run "cmd.exe /C ""IF EXIST ""%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms"" (""%appdata%\Microsoft\Windows\Start Menu\Programs\MyManufacturer\MyClickOnceApp.appref-ms"") ELSE (start /b """" cmd /c del ""%~f0""&exit /b)", 0
Set WshShell = Nothing 

Upvotes: 1

Views: 2590

Answers (2)

SachaDee
SachaDee

Reputation: 9545

The problem are the string in the path ! like this it work :

Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /c if exist test1.txt (echo ok & del test1.txt & pause) else (echo ko & pause)"

Try to work with 8.3 format. To resolve the composed-name and don't use string.

But if you're programming in VBS why do you want to use batch code in it ?

If you want to use both make a .BAT file. Or generate it from you're VBS and call it.

Upvotes: 1

xs6
xs6

Reputation: 890

Here is a example:

you have a batch called regex.bat :

@echo off &setlocal
for /f "tokens=1* delims=:" %%i in (
  'netsh wlan show interfaces ^| findstr /rxc:"[ ]* SSID [ ]*: ..*"'
) do for /f "tokens=*" %%k in ("%%j") do set "SSID=%%k"
echo %SSID% > regex.txt

the vbs looks like this:

Set WshShell = WScript.CreateObject( "WScript.Shell" )
WshShell.Run "regex.bat",0,True 

This works for me fine. No cmd-Windows comes up. Hope this helpes you

Upvotes: 0

Related Questions