My script contains the following code.
Set objShell=CreateObject("WScript.Shell")
ObjShell.Run "vb6/make Project1.vbp Project1.exe"
But my script doesn't make my project compile into exe.
I have the visual basic project Project1.vbp in the same folder which contains the script and the path for vb6 is also set in the Environment variable.
However when I tried to compile the project manually into exe it worked fine, by the command given below
vb6/make Project1.vbp Project1.exe
Kindly suggest some way to resolve my script's problem.
Always use full paths in sutch cases, I presume the error is there.
You can catch the error produced in the following way.
Here with a Ruby script I want to run but deliberatly made a mistake in the path.
Set objShell = CreateObject("WScript.Shell")
command = "cmd.exe /S /C ruby ""C:\Users\Gebruiker\ruby\excel\ru.rb"""
Set objWshScriptExec = objShell.Exec(command)
Set objStdOut = objWshScriptExec.StdOut
Set objStdErr = objWshScriptExec.StdErr
While Not objStdOut.AtEndOfStream
WScript.echo objStdOut.ReadLine
Wend
While Not objStdErr.AtEndOfStream
WScript.echo objStdErr.ReadLine
Wend
' ruby: No such file or directory -- C:/Users/Gebruiker/ruby/excel/ru.rb (LoadError)
I made the following changes
Set objWshScriptExec=objShell.Exec("cmd.exe /S /C vb6/make ""Project1.vbp""")
And it worked.
Since I am a beginner in vbscript, I don't know the function of
/S /C
so please let me know.
Related
I am using Quality Center to execute tests in Windows 8.
The VBScript file creates a temporary (timestamped) workspace/folder to clone the git repository and execute the test.
When I try to remove the folder in the end of the test, it doesn't work and gives an error: Invalid field type definition.
This code works fine if I work with a non git repository and a static project. If I delete manually the .git folder, the script is able to remove the remaining of the folders/files.
'''workspace = "C:\tests\20170613224942"
Dim objFSO, objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder(workspace)
'git clone
'Test Execution
Dim objFSO, objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(workspace)
I already tried (unsuccessfully) something like that:
objShell.Run "rmdir /S /Q " & workspace
I don't know if its problem with hidden folder so I tried this:
del /s /a *.git
But this only works for files and in this case only the folder is hidden. Not the sub-folders/sub-files.
Can someone help me to resolve this problem? How can I force the folder removal automatically via script?
Thank you.
Can you try :
git rm -r gitDirectoryToRemove
and then your code to remove the parent if required
Found a temporary solution. Maybe not an elegant one, but solves the problem.
I created a .bat file only for removing the folder passed by argument and I call it in VBS.
Bat File:
#echo off
#echo Try to removing %1
rmdir "%1" /s/q
VBS Call:
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "C:\tests\rm_workspace.bat " & workspace
Removing indirectly, it works like a charm.
The reason: still don't know why.
I have looked around and can't seem to make it work with the research I've done.
I'm going to create a GPO to apply to workstations that will uninstall Malwarebytes 2.0 and 3.0 from a given system. This will allow us to roll out the enterprise version.
What I have in my .bat file is this:
#echo off
cd "C:\Program Files (x86)\Malwarebytes Anti-malware\"
unins000.exe /verysilent /suppressmsgboxes /norestart
cd "C:\Program Files\Malwarebytes\Anti-Malware\"
unins000.exe /verysilent /suppressmsgboxes /norestart
However, I noticed that if one of the paths above doesn't exits (1 will always not exist) than it will pop up a command prompt with an error. I'd like no boxes to pop up at all if possible. I then tried to accomplish this with vbs with the error:
compilation error: Invalid character
This is that script:
Dim objShell
Set objShell = WScript.CreateObject( WScript.Shell )
Sub MalwareBytes()
On Error Resume Next
objShell.Run(%ProgramFiles%Malwarebytes Anti-malwareunins000.exe verysilent
suppressmsgboxes norestart)
objShell.Run(%ProgramFiles(x86)%MalwarebytesAnti-Malwareunins000.exe
verysilent suppressmsgboxes norestart)
End Sub
Set objShell = Nothing
How about, y'know, checking if a path actually exists before trying to go there?
if exist "C:\Program Files (x86)\Malwarebytes Anti-malware" (
cd "C:\Program Files (x86)\Malwarebytes Anti-malware"
unins000.exe /verysilent /suppressmsgboxes /norestart
)
The reason why your VBScript doesn't work is because your syntax is invalid. You need double quotes around the argument to CreateObject() as well as the command strings. With nested double quotes in case of the latter, because you have paths with spaces in them. Not to mention that it would be cleaner to check if a path actually exists in VBScript too.
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
I'm trying to delete a file like winzip command line using this code
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile("C:\Program Files\WinZip\wzuninst.exe wzcline C:\Program Files\WinZip\wzclun.dll")
When I run it on cmd it says Bad name or number. Can someone clarify it for me?
That's not a valid command line.
If you want to run a program you use wshshell.run.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /k dir c:\windows\*.*"
This is what I have and can not get the bat to run, if I move the bat to a folder without spaces in the name it works. My problem is that the actual bat is in a folder with spaces, so I need this to work.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%comspec% /K C:\Program Files\ping.bat"), 1, True
You need to quote the file specification:
Run("%comspec% /K ""C:\Program Files\ping.bat""")
I had a similar problem with a directory path in a VBScript that had empty spaces:
E.g.
The following did not work:
objShell.Run("C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe")
I simply included two extra double quotations on either side of the path and it worked for me:
objShell.Run("""C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe""")
Try this one
Set objShell = WScript.CreateObject("WScript.Shell")
strCommand = chr(34)&"%comspec% /K C:\Program Files\ping.bat"&chr(34)
objShell.Run strCommand,1,True
I know that this is an old question, but I found a fix that works for me.
It's the double quotes you need.
Try below:
objShell.Run("%comspec% /K " & """C:\Program Files\ping.bat""""), 1, True);