简体   繁体   中英

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):

  • This code is placed in the startup folder
  • The code is created in C# in order to run a ClickOnce application on startup
  • The reason I want to use vbscript is that the batch file opens a cmd window for a second, which is undesirable. My understanding is that the line Set WshShell = Nothing will make the command run invisibly
  • I have tried including >nul at the end of each line of the batch file, since I read that it will stop the output. This did not work for me.
  • It is theoretically possible for this to work by using both a .bat and a .vbs file, but this would require putting the .bat file in some other directory and feels generally hackish

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 

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM