I am launching from the command line a vbscript like this:
cscript.exe myscript.vbs
can I add something inside myscript.vbs to hide the console that launched it?
Is this a batch file?
If so, if that's the only line, you can put
EXIT
after your code and it will close the console once the vbscript opens.
As for hiding the console, you can run the batch from a vbs as shown here: http://www.howtogeek.com/131597/can-i-run-a-windows-batch-file-without-a-visible-command-prompt/
In general you can minimize a Program from Vbscript by using sendkeys and sending the sequence ALT+Space + whatever key is the shortcut for minimize in your local language. (You can check this in the GUI by pressing ALT+Space and looking for the underlined character in the minimize option)
In german and english this would be
wso.SendKeys "% n"
However this method is buggy with cmd and will not work since Windows XP. So the best workaround I found is a horrible hack where you use a call to powershell.exe.
The question for you is whether your program immediately steals the focus of the hosting cmd.exe or not. If not you can just insert
Set wso = CreateObject("WScript.Shell")
wso.SendKeys "powershell -windowstyle minimized -command """"{ENTER}"
If you somehow lose the focus on the cmd, you have to regain it. You could do that by using:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
' WQL uses \ as escpae char so we need to escape it with an additional \
wmiScriptName = Replace(wscript.ScriptFullName, "\", "\\")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where CommandLine like '%" & wmiScriptName & "%'")
For Each objProcess in colProcesses
wso.AppActivate objProcess.ProcessId
wso.SendKeys "powershell -windowstyle minimized -command """"{ENTER}"
Next
This will minimize all cmds that have the script running so it should not be started multiple times.. The WQL query is also not perfect, so if you edit the script in notepad for example it will detect this as well and send keys there... If this is a use case you think will happen you have to be more exact with your WQL query (include processname cscript.exe or something)
It is quite a hack but I could not think of any other way of doing this in vbscript only (sadly one external call is necessary but no code executed and powershell.exe should be present everywhere). If you could do the whole script in powershell for example there would probably be better methods.
Related
we have this vbs script we use to update certain documents with SyncToy.
This is the script as it is currently written:
'--------------------------------------------------
Set oShell = CreateObject("WScript.Shell")
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" &
chr(34) & "-R"
iRC = oShell.Run(sFilePath, 0, True)
' Return with the same errorlevel as the batch file had
Wscript.Quit iRC
'---------------------------------------------------
I didn't write this script, and I have very little experience with scripting.
I have a task set up in task scheduler that runs this script anytime the device connects to a network. The script should run SyncToy and then synchronize the folder pair that is set up. I have tried running the script through command prompt with the cscript command but nothing happens as far as I can tell. At least the folders aren't syncing.
The script is running on a Windows 10 pro tablet
I have verified that the task is indeed running when it is supposed to. I'm just not sure if it is an issue with the way the script is written or if the task settings need to be changed. Is there anything wrong with the script as far as you can tell?
I was unsure whether to post this here or over in serverfault. If this doesn't belong here please move the question over to serverfault
Update: I've verified that this isn't a problem with the script. This problem apparently arose only after the update from SyncToy 2.0 to 2.1.
Thanks Guys.
There is a error with the sFilePath lines.
First, I don't know if this was originaly on a single line but it should (or add "_" before changing line).
Then, this (...)& >"-R" would not work. The ">" symbole is outside the quotes and generate a error.
If you want to execute this command: "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R,
this is the way to do this:
sFilePath = chr(34) & "C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" & chr(34) & " -R"
You can also add msgbox sFilePath to show a popup with the value of sFilePath.
To test/run the script, you just need to double-click on it.
How do I create a shortcut of a batch file and configure if to run in minimized mode? When I create a shortcut to a batch file I have to manually configure it to run in minimized mode manually. Any idea how do I write a script to change it to run as "minimized" mode
#npocmaka's shortcutjs.bat is a complete solution but it has about 200 lines. So, I have created a small VBScript for the purpose. You need to modify it according to your purpose.
'======PART 1: elivate to admin. required to save the batch file from part 2 in C drive
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
'======PART 2: create the test batch file on the fly
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile = "c:\test.cmd"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "pause" & vbCrLf
objFile.Close
'=======PART 3: create the shortcut of the batch file
set WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\test.lnk")
oMyShortcut.WindowStyle = 7
OMyShortcut.TargetPath = "c:\test.cmd"
oMyShortCut.Save
Part 1 and 2 are optional and they are just to give an idea about what to do if you also want to create the batch file on the fly. Part 3 is the required code to create a shortcut using VBS.
You can run VBS script from cmd: cscript shortcut.vbs after you save the code above as shortcut.vbs
If you want to pass some argument about your batch file location, see this question, Can I pass an argument to a VBScript (vbs file launched with cscript)?
Then you can also use your code like cscript shortcut.vbs "C:\test.cmd" and reuse the same VBScript to create different shortcuts.
For other available options like adding an icon to your shortcut, adding hotkey support, setting Working Directory etc. please see this link
If I understand you correctly. You will need to use VB script to create shortcut. I don't believe batch script can create shortcut
https://support.microsoft.com/en-us/help/244677/how-to-create-a-desktop-shortcut-with-the-windows-script-host
see example 2: the WindowsStyle parameter define the windows size.
oMyShortCut.WindowStyle = 7 <-- 7= minimized.
Good luck
Binh
Try with shortcutjs.bat:
shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -windowstyle 7 -adminpermissions yes
-adminpermissions yes is optional if you want to run the bat as administrator. You'll need the full path to your script. possible modes are 1 for normal, 3 for maximized and 7 for minimized.
I have written a PowerShell script that is going to interpret a mail's body for command's and create a .bat file to execute the commando's it found.
This script works, but the one big issues is that whenever is executes the .bat file, a command-prompt window flashes over the screen real quick. I was wondering if it's possible to prevent this from happening?
My code:
$m.Body | Out-File cmd.bat -Encoding ascii -Append
.\cmd.bat | Out-File results.txt
Is there any command of property i have to set?
Thanks.
I realize this question is more than 2 years old at the time I write this however there is still no official answer. Although I am very new to PowerShell, I think I have a more pure Powershell answer than using vbscript or COM.
Use Invoke Command:
Invoke-Command {cmd.exe /c cmd.bat} | Out-File results.txt
That Should do the trick. This will shell to cmd.exe and the /c will self terminate the shell on completion. It will run within the current shell so no new window will open.
Answers and information can be found here.
From there, the selected answer, in case the link goes stale:
Save the following as wscript, for instance, hidecmd.vbs after replacing "testing.bat" with your batch file's name.
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c testing.bat"
oShell.Run strArgs, 0, false
The second parameter of oShell.Run is intWindowStyle value indicating the appearance of the program's window and zero value is for hidden window.
The reference is here http://msdn.microsoft.com/en-us/library/d5fk67ky.aspx
I am not so good in VBScript at all, but thanks to Google I was able to put together script which is able to open file path in explorer.exe
I would like to open the specific path in same window not in the new one. Is VBScript able to do it?
Here is my code:
Dim SH, FolderToOpen
Set SH = WScript.CreateObject("WScript.Shell")
FolderToOpen = "C:\path\to\my\folder"
SH.Run FolderToOpen
Set SH = Nothing
Thank you for your advice.
Try this:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Target
Here is a hackish approach using SendKeys that will work if the open instance of explorer.exe has the focus:
Set WshShell = WScript.CreateObject("WScript.Shell")
target = "C:/programs"
WshShell.SendKeys "%d"
WshShell.SendKeys target
WshShell.SendKeys "{ENTER}"
This will work if you e.g. have the above code (with the intended target) in a script in one folder. Click on the script icon and it will send you to the target folder.
[On Edit] An explanation of how it works: If you are using Windows Explorer and type Alt+d (which is what SendKeys "%d" simulates) then the focus is shifted to the address bar. For years I have been using this trick to open a command prompt in the current folder (Alt - d then type cmd then press Enter and the prompt opens with the open folder as the working directory). When I saw this question I wondered if essentially the same trick (but automated with VBScript) would work for navigation purposes and was pleasantly surprised when it worked as intended the very first time. Alt-d is a useful keyboard shortcut to keep in mind.
I'm trying to run the following bit of code in a vb6 dll:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run strPath & "test.bat", 0, True
The dll process gets hung up. The batch file will not run, no matter what its contents. I even tried an empty batch file and it still hung up. However, if I try this same piece of code, with this change:
Dim objWSShell As Object
Set objWSShell = CreateObject("Wscript.Shell")
objWSShell.Run "calc", 0, True
It works fine. I can't figure out why exe files work and bat files don't. Any ideas?
You don't need to use the shell scripting stuff, you can make things simpler & use the built in Shell() function:
shell environ$("COMSPEC") & " /C c:\xxx\yyy.bat", vbNormalFocus
Ditto for:
shell "calc", vbNormalFocus
You need to run cmd.exe and pass your BAT file to it.
objWSShell.Run "%COMSPEC% /c " & strPath & "test.bat", 0, True
I had a similar issue where batch files couldn't be run directly from WScript.Shell, but I didn't have access to modify the VBScript. It turns out there was a registry override on the .bat extension.
While using COMSPEC worked for me, deleting the registry key actually fixed more than just the WScript problem.