Open ie from command line invisibly - batch-file

I'm trying to open internet explorer invisibly frim command line. The page I will open log user data for our software. Thats why I want to make it invisible, there is nothing necessary for people on the page.
I tried this,
start iexplore http://www.example.com
it works. However, its visible. (I know there are some working scripts in vbs , shell. But I need to make it from command line) How to make it invisible ?

You can't with batch.
This is a vbs script. Just execute it.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run """c:\program files\internet explorer\iexplore"" www.google.com", 0, false

Related

How to generate key strokes from a batch file?

I want to create a batch file to do the following:
Start selenium server(webdriver-manager start)
Run Protractor tests(protractor conf.js)
Stop Selenium server()
This needs 2 different command prompts since webdriver-manager start will keep running and simultaneously the tests need to be executed
I have achieved the following so far. I have created a .bat file with the following contents:
start runTests.cmd
webdriver-manager start
Ctrl-C(**DOES NOT WORK**)
However, I am not able to figure out a way to shutdown the Selenium server(which is achieved by pressing Ctrl+C on the same window on which the webdriver-manager start command is executed)
You can generate keystrokes using VB Script, which can be called from a batch file.
I followed this post: http://www.w7forums.com/threads/f5-key-via-batch-file.18046/, substituting {F5} with ^{C} for Ctrl+C. So my file looks like:
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "^{C}"
You need to save this file with a ".vbs" extension.
I also found from this answer VBScript - switching focus to window using AppActivate how to set the focus to another window (which you know the title of). Doing this first, you can direct your keystroke to the appropriate window, so:
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Untitled - Notepad"
objShell.SendKeys "^{C}"
I experimented with an empty instance of Notepad, so you'll need to change the window title string appropriately. (Ctrl+C doesn't do anything in Notepad, but Alt+F4 closes it, so I used "%{F4}" to test it.)
Then you can simply call the VBS file from your batch file to run it.

Passing an argument into a VBS script which passes into a batch file

I have a legacy application which doesn't support utilizing the default applications defined in windows which requires that I specify a specific an executable for a file format to be opened within the application. Since Microsoft no longer includes MODI with Office by default I have been looking at using launching Windows Picture Viewer for .TIF, .TIFF, & .BMP files since it is built into Windows; however Microsoft does not have a direct executable for Windows Picture Viewer which can be called forcing me to create a script which executes the command which calls for Windows Picture Viewer to execute. After research the only way I have been able to call the application to open a specific file is by creating a batch file such as below:
GIFTS.BAT
rundll32.exe C:\WINDOWS\system32\shimgvw.dll,ImageView_Fullscreen %~1
If I execute the above code such as GIFTS.BAT "C:\Example Directory\Sample File.tif" from a command prompt or from the application launches and the Sample File.tif opens without a problem; however a command prompt opens along with the file when launching from the application.
Upon which I tried to create a vbscript to hide the batch file from executing however I can't seem to pass my argument if the argument has a "space" within the argument.
GIFTS.VBS
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run """C:\GIFTS.BAT"" " & WScript.Arguments.Item(0), 0
Set WshShell = Nothing
If I try to execute the VBScript from a command prompt such as GIFTS.VBS "C:\Example Directory\Sample File.tif" the application never launches and the command prompt returns no message or error. If I simplify the execute command (removing spaces to GIFTS.VBS "C:\Sample_File.tif" the application launches as well as the file Sample_File.tif is displayed and there is no command prompt displayed when the application executes the VBScript.
My question is how can I pass an argument into the VBS script that in return passes the the batch file when the argument contains spaces (which there is always going to be spaces since the argument will be a file name and path)?
There might be an easier approach to what I want to accomplish; however I am looking for a solution that Windows 7 - 8.1 can utilize with no additional software to install or manage on each workstation. The batch files works great I just need to be able to hide the command prompt that opens along with the application as my end users won't know what to do with it.
Thanks!
Sometimes, nested levels of escaping characters requires intimate knowledge of the undocumented behavior of CMD or some voodoo. Another way to attack the problem is to guarantee that you won't have any spaces in the name of the file. Windows has a concept of a short path (no spaces or special chars) for which every existing file has a unique one.
Here's a modified version of your program for which invoked subcommand doesn't need quotes around the file name.
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoFile = fso.GetFile(WScript.Arguments.Item(0))
WshShell.Run """c:\GIFTS.BAT"" " & fsoFile.ShortPath, 0
Set WshShell = Nothing
You may wish to add your own error checking. The specified file must exist in order for the GetFile() command to succeed.

Locking focus on batch file

I have a simple batch file in windows that I run on startup that presents the user with a menu to start certain applications. However by default, whenever I open one of these applications the focus goes to that window and off of the batch file. Is there a way to maintain, or re-divert focus onto the batch window?
Thanks
EDIT: Got it to do what I wanted. Used foxidrives suggestion to start them minimized but they were still taking focus. So I made a short VBScript to make the cmd window the active window after each call and the combination of the two worked. Thanks!
There is no command to steal the focus. As Bill_Stewart posted, that would be a dangerous feature that grants the program too much power over the user. You can however start the applications minimized (they will still be the active window), and then build and call a VBScript to make your batch window the active window:
start "" /MIN "application.exe"
cscript /nologo myVBScript.vbs
myVBScript.vbs
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate "myBatchFile"
WScript.Sleep 2000
WshShell.AppActiavte "myBatchFile"
I've read that several people have had trouble with AppActivate on Windows 7. It was not functioning for me, and instead of bringing my target window to the foreground it just blinked in the task bar. Calling it a second time however, for some reason, brought it to the foreground and made it the active window, which is what I wanted. Hope this helps anybody else with a similar issue.
you can't lock the focus to your command prompt window. But what you could do is to set the TopMost flag of the command prompt window. There is a Win32 function called SetWindowPos which does that. Maybe there are some ready to use command line tools around which are doing this for you. Or, if you have visual studio installed, try to compile this one here: How to set a console application window to be the top most window (C#)?
If you use the start command with the /min switch then the application will be minimised and the batch file should remain visible:
#echo off
pause
echo launching notepad
start "" /min notepad
echo notepad is active
pause

while running batch file command prompt window blink

I am running my batch file inside my code, it's working fine, I just want to know is there anyway I can stop the blinking of command prompt window.
Try using some VBScript like this.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("C:\yourbatchfile.bat"), 0, True

Running a Batch File in the Same Directory from the VBS

I have a VBScript that is supposed to run a .bat or .vbs file, but it doesn't work!
It comes up with an error saying that the file could not be found, whether i put in a file path or not (it shouldn't matter anyway I think because it's in the same directory).
So my question is, how do I start a .bat file (or even better, a .vbs file) from within a VBScript?
The relevant code is bellow:
'*******This is the start of my open command that doesn't work*******
Do
If Hour(Now) >= 9 And Hour(Now) <= 18 And Minute(Now) = 34 And Second(Now) = 59 Then
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "F:\\EAS\Volume Up.vbs"
Set shell = Nothing
MsgBox "My De-bug Message Box which doesn' even get to open"
WScript.Sleep 2000
Set WshShell = CreateObject("WScript.Shell")
music = "C:\...\MYFILE.wav"
WshShell.Run "wmplayer """ & music & """", 0, True
WScript.Quit 1
Else
'*******This is the end*******
So what am I doing wrong? Is it the wrong way to open it? What should I put instead?
It would be good to see the contents of the .BAT File.
Your code seems fine as I am able to run the below script on my machine:
dim shell
set shell=createobject("wscript.shell")
shell.run "tester.bat"
You may not see what the .BAT File is doing as it happens so quickly, as a tester add the following command to the end of your .BAT Script:
pause
Then you will see the command prompt open. As per my VB code above, the .BAT file contents are below:
#echo OFF
#echo %time%
pause
This will show you the current time and then pause, leaving the command prompt open. Give this a go as a tester as it works fine for me.
putting triple quotes (as suggested by ToThePoint) around the path solved my vbs file error, where it was failed to find the file on specified path as file path was having spaces, like
D:\Main\My text Files\abc.txt.
thanks :)
The only thing i can think of is that it must be a typo in the file name.
Can you ensure the file name is spelled correct?
Or else please post the exact error you get.

Resources