Name: Mitran Date: November 14, 2005 at 17:16:12 Pacific Subject: basic batch script OS: Win XP home CPU/Ram: 2ghz/250ram
Comment:
Hi, I've looked for over 2 hours on Google to find the answer to this: Does anyone know how to gat a batch script to open a program and then close the command window without turning off the program the script just started?
Here is what I have: move "E:\SSSS\mf\*.*" "E:\" (works fine) "C:\Program Files\Program\program.exe" exit
It starts "program.exe," but of course it won't get to "exit" till the "program.exe" is exited.
Thanks, but I tried that, and it does not start the program when I use START. It does close the current command window, and opens another. But it doesn't start the program.
Used with the /b switch, it gives: C:\START /B "C:\Program Files\BearShare\BearShare.exe"
E:\>exit Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.
and it does not open the program or shut the command window, even though if I type EXIT at this point, it does close. (the path to the program is correct, and works without START).
You're right, start doesn't seem to work correctly when sending it the full path of the program. I did get it to work though. Try a script like this:
c: cd "Program Files\BearShare" start bearshare.exe
By first going into the folder where the EXE is it worked for me. The script should immediately move to the next command, not waiting for BearShare to start (you don't need to specify the exit command, the script exits when it gets to the end of the file). I have no idea what the problem was before.
That at least gets to the directory and starts the program. (but just using "C:\Program Files\BearShare\bearshare.exe" will also start it- it's the other stuff that messes it up) I have:
C: CD "C:\Program Files\BearShare" "BearShare.exe" exit
Two problems: One, when I use
C: CD "C:\Program Files\BearShare" START "BearShare.exe" exit
the program does not start at all. It does go to the folder, though.
Two, in either script, the command window stays open- it doesn't exit.
Well, I've never tried anything, but are there alternative shells for windows that would actually do what you tell them- and work in batch scripts? Any suggestions? A free one would be nice (:
I'm not sure why you are having continued problems, but if you're willing, why not use WSH instead of a batch script?
What else are you trying to do with this script? I'll help if you want to move it over to WSH. For the part of starting the program, you would just put this:
Dim WSHShell Set WSHShell = WScript.CreateObject("WScript.Shell")
Into a file called "whatever.vbs". Run it by double-clicking it.
That will start the program without waiting for it to finish. There will be no console or any other indication that the script is running besides a system tray icon that shows for a second or two.
Does that work for you? What else do you want the script to do?
Yeah, that sounds really great. I don't want this particular script to do anything in the end except move "illegal" files from the shared folder (don't worry, I share plenty), and start the program. I mostly use scripts to gather all my personal files, zip them and use CDRecord to back them up. That script works fine as it is. I keep finding uses for scripts, like the following for unzipping all my fonts:
7za x -y "*.zip"
And this next is kind of cool, because using the two different avi preview programs together gets you more of the file (only use on a copy of your file in seperate folder):
cd "E:\SSSS\Temp\AVIPREVIEW" rename "*.avi" "Renamed_Movie_File.avi" avipreviewc "Renamed_Movie_File.avi" "AviPreviewc_Output_Movie.avi" del "Renamed_Movie_File.avi" avipreview "AviPreviewc_Output_Movie.avi" del "AviPreviewc_Output_Movie.avi"
So basically I'm no techie, I just use scripts for a few usefull things. Thanks for the offer of advice- I'll look into WSH as soon as I have time and ask you if I have difficulties (:
Ok, I've gotten this vbs script set up, and it works- as you see I have two scripts, because it looks like the loop command in the second script would try to redo the whole script. Can you tell me if I'm doing snything really clumsy, and maybe how to do that loop so I only have to use one script? So, first sctipt is:
Set objFSO = CreateObject("Scripting.FileSystemObject") objFSO.MoveFile "E:\Copy of MSC\*.*" , "E:\" Dim shell Set shell = CreateObject("WScript.Shell") shell.Run "E:\killIE.vbs" Dim WSHShell Set WSHShell = WScript.CreateObject("WScript.Shell") WSHShell.Exec("C:\Program Files\BearShare\BearShare.exe") WScript.Quit
And the killIE script is:
strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colMonitoredProcesses = objWMIService. _ ExecNotificationQuery("select * from __instancecreationevent " _ & " within 1 where TargetInstance isa 'Win32_Process'") i = 0 Do While i = 0 Set objLatestProcess = colMonitoredProcesses.NextEvent If objLatestProcess.TargetInstance.Name = "iexplore.exe" Then objLatestProcess.TargetInstance.Terminate End If Loop
I didn't write this... I copied from web pages and modified. But it kills all popups!
Reading over the first script, the only thing that jumps out at me is that you're creating two Shell object when you only need one. I'd change it to be like this:
Dim WSHShell, objFSO
Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
I'm not sure what you're trying to do with the second script. Are you just trying to terminate all instances of the iexplorer.exe process?
If that's your goal, you can do it easier with one line (assuming you are running this on Windows 2000 or XP) like so:
WSHShell.Run "taskkill.exe /im iexplorer.exe", 0
Also, in hindsight I think it's better to run BearShare with the Run method instead of Exec. Doing this there is no reason you can't put them together in one script like this:
Dim WSHShell, objFSO
Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
Doh. Two things: First, I tested that script with Notepad so you'd need to change where it says "notepad.exe" to "iexplore.exe". Second, if you want the script to keep running and keep closing IE windows then use this script instead:
Dim WSHShell, objFSO
Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
I'm going to try that ASAP- it looks great. The "do while true" - does that mean it keeps going while the bearshare.exe is also going?
The reason for killing all IE windows, is that BearShare is ad supported, and while I have the adware blocked with the firewall, it still pops up a lot of windows (blank, as they are blocked from downloading). We use Mozilla or Firefox, so blocking IE won't ever be noticed (:
What I really need- and haven't been able to find- is a large dictionary of examples- like the ones you give, and the few I found online. And explanations- like, What does FSO and Dim mean, (I mean, what is a file system object?), and what is the difference between, say "WScript.Shell" and "Scripting.FileSystemObject" for instance? Any resources? I looked as you suggested at the Microsoft site, and it isn't basic, or I didn't find where it is. The best I've found was http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/
Second post: I downloaded taskkill.exe, but when I run:
Dim WSHShell, objFSO Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject") WSHShell.Exec("C:\Program Files\BearShare\BearShare.exe") 'Begin infinite loop to kill IE windows Do While True WSHShell.Run "taskkill.exe /im iexplore.exe", 0 Sleep(1000) Loop WScript.Quit
It says line 8 char: 4 error: type mismatch: 'sleep'
Is a runtime error. Is this sleep command so it will run only every 1000 seconds? good idea...
The line with Sleep(1000) should actually be WScript.Sleep(1000).
What Sleep() does is wait the specified number of milliseconds. In this case it pauses the script for 1 second each time through the loop. You can increase this number if you want, but it's a good idea to use at least 1000 milliseconds just to preserve resources.
Regarding your other question about the loop, it doesn't just run while BearShare is running, it actually runs until you kill the script with Task Manager. If you want it to stop when BearShare stops, we'll need to change the script like this (also fixing my mistake with Sleep():
Dim WSHShell, objFSO, objExec
Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "E:\Copy of MSC\*.*" , "E:\"
Set objExec = WSHShell.Exec("C:\Program Files\BearShare\BearShare.exe")
'Loop until BearShare closes (Status will change to a 1) Do While objExec.Status = 0 'Kill all IE windows WSHShell.Run "taskkill.exe /im notepad.exe", 0 'Wait for 1 second (2 seconds would be 2000, etc) WScript.Sleep(1000) Loop
WScript.Quit
Also, you shouldn't have needed to download taskkill. Taskkill.exe is a program that comes with Windows 2000 and XP systems.
In response to your specific questions:
Dim is a VisualBasic keyword that tells it to initialize (or dimensionalize) a variable and prepare it for use. It's not required, but is a good practice to get into.
FSO is an abbreviation of FileSystemObject which allows the script to interact with the filesystem (read, write, create, copy, move, etc files). It's usually what I call my variables of this type.
WScript is a namespace for WSH that contains a number of child objects and methods. Shell is an object which lets you interact with the Windows shell. Sleep() is one of many WScript methods which add functionality to your scripts.
CreateObject() is used to instantiate an object to a variable. Inside the method call you put what you want to create like this: "Namespace.Object", for example "WScript.Shell". You can prefix calls to CreateObject with "WScript" if you want, but it's not required.
I know this only answers your direct questions, but I can only suggest that if you want to learn more you can either find yourself a book on WSH or do like I have and learn by doing. When I find something I want WSH to do I use Google a lot and/or might ask a question on a board like this. Do this enough and you'll pick it up pretty quick.
Finally, have you considered using a different p2p client? Personally I use (but not very often) Shareaza. It's free and open source and connects to the Gnutella, Gnutella2, and eDonkey2k networks while also supporting BitTorrent files. Not sure what you use BearShare for exactly, but you might try Shareaza to get away from nasty ads.
That latest script worked perfectly on the first try!
Just knowing the different status- what it means, 0 or 1 is going to help a lot.
Yeah, I know about different file share programs. I have Shareaza. I've tried about every major program that doesn't have spy/adware. Just recently tried FrostWire. But BearShare is the one and only program I've every had work on my slow connection of 2.4 kbps. Nothing else ever worked at all. Otherwise I'd use Shareaza or LimeWire/FrostWire.
Agent Ransack couldn't find taskkill on my drive... nor sleep. The shell didn't see it, either. I have Home XP, and I think it is rather crippled for stuff like this. I've missed a lot of common stuff like that in the past.
I'm going to be gone for a couple of days. So thanks much again for all the help (:
Here is the script, for anyone who wants it (modified very slightly)
Dim WSHShell, objFSO, objExec
Set WSHShell = CreateObject("WScript.Shell") Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objExec = WSHShell.Exec("C:\Program Files\BearShare\BearShare.exe")
'Loop until BearShare closes (Status will change to a 1) Do While objExec.Status = 0 'Kill all IE windows WSHShell.Run "taskkill.exe /im iexplore.exe", 0 'Wait for 1 second (2 seconds would be 2000, etc) WScript.Sleep(6000) Loop
The information on Computing.Net is the opinions of its users. Such
opinions may not be accurate and they are to be used at your own risk.
Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE