Echo Code into a File using VBScript - vbscript

I am writing a VBScript which I want to use to echo the code of another VBScript into an output file.
However, I am unable to write some of the characters to the output file using this method.
If I use the command line method:
cmd.exe /c "#echo "hello"">output.vbs
This works and the string: "hello" is written to the output file.
However, when I do the same using a VBScript, it does not work.
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
So, is there a way I can echo it into another file retaining the double quotes?
Thanks.

Your quoting is wrong.
Change this:
objShell.Run "%comspec% /c ""#echo ""hello"">output.vbs"
into this:
objShell.Run "%comspec% /c #echo ""hello"">output.vbs"

Related

vbs text concatenation not working with wscript.arguments

I made a js script to convert text to speech and running a command line with vbs to keep the console window hidden. I am calling via a command so Im trying to use the parameters passed in to it.
I tried using the '+' operator like in most other languages but it didn't work, then I tried the '&' operator but to no success :(.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c node audio.js" + WScript.Arguments.Item(0)
oShell.Run strArgs, 0, false
This
C:\Users\eh
type 56861329.vbs
WScript.Echo "cmd /c node audio.js" + WScript.Arguments.Item(0)
C:\Users\eh
cscript 56861329.vbs MissingSpace
cmd /c node audio.jsMissingSpace
should show the real problem. As #Geert said, & is the VBScript operator for concatenation and should be used.
I forgot a space I feel so dumb

VBScript Command window - passing a command syntax

I'm trying to get a command to run in a cmd vindow via VBS. Just like this answer:
How to keep the VBScript command window open during execution
The command I'm trying to issue is this, as written it works in a .cmd file.
"\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec" /x86 /f "\\path\folder\folder with space\Import.dtsx"
I've been unable to get it to work in the syntax from the above answer:
objShell.run "%comspec% /c ""SomeProgram.exe -R & pause""", 1, True
Figure it's a double quote issue, but I can't find it.
(I have to use the whole path to dtexec to force usage of the 16bit version.)
Followup: =======================================================
This works:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f c:\temp\Import.dtsx & Pause", 1, True
This does not:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec"" /x86 /f ""c:\temp\temp two\Import.dtsx"" & Pause", 1, True
nor this:
oShell.Run "%comspec% /C ""\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\dtexec /x86 /f c:\temp\temp two\Import.dtsx"" & Pause", 1, True
It's that space in the filename argument that is hosing it.
You don't need pause, just tell CMD to keep the window open after the command finishes (/k) instead of closing it (/c):
objShell.Run "%comspec% /k program.exe -R", 1, True
Nested double quotes are only required when you have a path with spaces in it, e.g.:
objShell.Run "%comspec% /k ""C:\some folder\program.exe"" -R", 1, True
Edit: If arguments in your commandline are paths with spaces as well you need quotes around each path and another set of quotes around the entire statement:
objShell.Run "%comspec% /c """"C:\some folder\program.exe"" /p ""foo bar"" & pause""", 1, True

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

VBS How to call cmd.exe using a string variable with spaces

I need to call the following:
set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /c copy /y C:\input\" & WScript.Arguments(0) & " C:\output", 0
where the input argument may be "File Name.txt". I have seen countless examples of people doing the same thing using double quotes for a hard coded file location, but nothing using an input argument or variable. What syntax is required so that the command line receives:
copy /y "C:\input\File Name.txt" C:\output
and not
copy /y C:\input\File Name.txt C:\output
for an arbitrary file name?
Embed the needed quotes (escaped via doubling) in the surrounding literals:
WshShell.Run "cmd /c copy /y ""C:\input\" & WScript.Arguments(0) & """ C:\output", 0
background, further reading

How to write to the same command prompt with vbs in windows?

I have vbs script and that creates folder, make archive and copy to that folder, upload to ftp and so on. I want it to write status to cmd after each step of execution( after creating folder, zip...)
The following opens cmd.exe and writes there "creates folder". That's exactly what I want.
Dim objShell, strCmd
strCmd = "%comspec% /k echo creates folder"
Set objShell = CreateObject("Wscript.Shell")
objShell.Run strCmd, 1, True
But, how I can write to the same cmd window that just opened? If I use this
strCmd = "%comspec% /k echo starting zip"
objShell.Run strCmd, 1, True
it opens new cmd window, but I want to write "starting zip" to previously opened cmd.
How I achieve this?
To print to the command prompt use wscript.echo.
I want to point out that the behavior of .echo is effected by how the script is loaded. For instance, if I run it from command prompt, like this: test.vbs, then the echo lines show up as pop-ups due to running wscript by default. However, if instead I load the file like this: cscript text.vbs all output goes to console as expected.

Resources