?
Solved

close all open cmd windows at end of batch

Posted on 2011-10-25
19
Medium Priority
?
651 Views
Last Modified: 2013-11-09
I have a batch application that runs through several batch files. It executes from temporary directory.  The problem is that once the batch is completly done it leaves anywhere from one to five CMD windows open that says something like...
"c:\documents and settings\user\local settings\temp\????.tmp"

Main question: I have a batch at the end responsible for cleaning up files, how do I edit it so that closes any and all remaining CMD windows that are open?  

Less important question: And if I wanted to exclude just one script from closing, how would I do that?  
0
Comment
Question by:icecom4
  • 6
  • 5
  • 4
  • +2
18 Comments
 
LVL 78

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37026412
Not where I can test this but you can loop through ALL processes looking for cmd.exe then kill it.

Not sure how to fine what the specific window is running.

Example of loop and kill but not killing cmd.exe.

http://www.tek-tips.com/viewthread.cfm?qid=1492889
0
 
LVL 78

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 80 total points
ID: 37026439
I knew I saw a cleaner example.  I used this to loop through and kill a specific process for my own setup:

http://www.computerperformance.co.uk/vbscript/wmi_process_stop.htm
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 1304 total points
ID: 37026444
OK what is in the batch.  It sounds like maybe you are starting other batch files with a START command, or possibly cmd /k otherbatch.cmd etc?

If you have one batch that does this:

@Echo off
call batch1.cmd
call batch2.cmd

Then it would call batch1.cmd until it hits the end of the file or the command exit /b or goto :eof
Then it would run batch2.cmd and the same...

If you have a batch that does

@echo off
START "" batch1.cmd
START "" batch2.cmd

It should start batch1 and batch2 at the same time but they will both go back to a cmd.exe prompt
If you add the line

exit

to the end of them, or wherever the batch finishes then it will close that window.

A brute force method would be to taskkill them with

taskkill /FI "IMAGENAME eq cmd.exe"  

or similar but better to fix the batch if possible.

Steve
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1304 total points
ID: 37026463
Another way without having to add exit to the end of your batches is change a start command like this:

START batch1.cmd
or
START "title" batch1.cmd

to START "" cmd /c batch1.cmd

Steve
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 616 total points
ID: 37027173
Feels like rather than killing them if they are still running, try to find out why they don't finish gracefully and address that problem. What do the BAT scripts that don't close cleanly do, and what code is in them?

Is this line that you mentioned in the title bar of the remaining window, or in the inner part of the window?

"c:\documents and settings\user\local settings\temp\????.tmp"

~bp
0
 

Author Comment

by:icecom4
ID: 37027192
I have one batch that controls all the others.  

well I do have START batch.com on most.  Once a batch is finished it closes and moves on to the next.  This works great if I do not use the "temp directory" feature when compiling to an exe.  But when the files move to the temp directory is when these cmd windows stay open.  

At the end of each batch is EXIT.  should I use EXIT /b instead?

I will try adding to START "" cmd /c batch1.cmd and see what happens. But will batch.cmd work if most of my files are named batch.com?    

thanks
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1304 total points
ID: 37027223
Well if you could advise what an example that is failing is doing specifically please.  The examples above are the ways that will work at least.

Are you sure it is batch.com you are using?!

If you want it to go from one to the next then get rid of START and use:

call batch1.cmd
call batch2.cmd

in the "master" cmd file.
etc.

What exactly do you mean by ""temp directory" feature when compiling to an exe." ?

Steve
0
 

Author Comment

by:icecom4
ID: 37028243
Yes, I converted my main batch to an exe and made all the other batches includes.  I am using bat to exe program.  There is an option to extract and run from "current directory" or "temp directory".  I like the temp directory because it does not extract 30 files on the desktop.  However when I do use that method a few CMD windows remain when batch scripts are finished...weird.  It may be the exe compiler, I wont know until I try some of these changes you guys are suggesting.  

I will try some of these switches you guys posted in the batches and see if that helps, if not I will try the taskkill /FI "IMAGENAME eq cmd.exe"  

.  
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 616 total points
ID: 37028455
Just curious, why did you decide to do the BAT to EXE conversion?  Was it for performance reasons, and if so have you seen any difference?  If not, was it because of sensitive text in the BAT files, and if so are you sure that those strings are encrypted in the resulting EXE files, and not still extractable?

~bp
0
 

Author Comment

by:icecom4
ID: 37029049
Yes, it is sensitive, I have competition in the gamerserver admin world that would love to take all my batches and make something better.  Not sure about performance...although in windows 7 it runs real slow and I don't know why.  

Hmmmm...how do I know if they are extractable? Is there something I can run on it to test that?

Thanks for the help.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1304 total points
ID: 37029254
Interesting.  doesnt that defeat the object of secrecy if they if all it does is extract them to temp and run them?!

ok another thing ... You say it does this from temp but not elsewhere.... Could it be temp is in a path with spaces in and somewhere in there you are using start command with the current path say so it ends up doing

START "c:\full path to\temp\whatever.cmd"

that will start a cmd window with the title of path as above and do nothing.... First parameter in quotes is title of the window...

As to passwords and the like, try FIND against one of the files with your password and see if it finds it perhaps:

find "password" file.exe


steve
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 616 total points
ID: 37030351
Try this utility against the compiled EXE and then look through the extracted strings for sensitive values.

http://technet.microsoft.com/en-us/sysinternals/bb897439.aspx

~bp
0
 

Author Comment

by:icecom4
ID: 37032146
It extracts them to temp but they are converted to .com before that, so no they can't read the batch lines

yes it is a broken path to temp directory...ok that explains why they open

ok, i will use a combination of all this information and work these open windows down to zero

Billprew I used that strings application and it opened my files but most of it was garbled, only showing copyright info..hah woot!

Anyway guys thanks for your help, I will divy up the loot

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37032176
In the off chance that you want to pass any points to billprew or dragon-it, could ass MS-DOS as a zone to the question before awarding.  Appreciate it...

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37032199
+1 !
0
 

Author Closing Comment

by:icecom4
ID: 37032219
All this helped one way or another
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37032277
didnt show in the "accepted" email but thanks for trying!

Icecom4 - thanks for the points.  Bill and I are both trying to hit a 1 million point milestone in the "MSDOS" area if you have any other questions if you could choose that zone please

steve
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 37037515
Add this at the end:
TASKKILL /F /IM cmd.exe /T
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question