# Solved: Batch file to check if a process is finish?



## g_01001 (Sep 22, 2008)

Hi to all.

I need to make a cmd that checks the status of another process to see if that other process is already over. And execute the commands only when that procces is over.

This is the full description of the problem:

In my server I got 2 jobs ("Unzip.cmd" and "Rename.cmd"). 
This 2 jobs must be executed in order: first the Unzip.cmd and then the Rename.cmd.

The Unzip.cmd, unzips a file in the same directory
And the Rename.cmd, rename that unziped file.

The problem is that some times the unzip procees takes too long and the Rename.cmd can't rename the file because it's still used by the Unzip.cmd.

So I want to add some command to check for the end of the Unzip.cmd.

Thanks.


----------



## -Fabez- (Jul 28, 2008)

If you start the Unzip.cmd process, the command prompt will wait untill it is finished untill it resumes and executes the next command, which in your case would be Rename.cmd. If you do not start the Unzip.cmd using the command prompt then using a higher level language would be advisable. Try looking into the Tasklist command for the command prompt as well.


----------



## g_01001 (Sep 22, 2008)

Hi, thanks for your help.

The thing is that the 2 jobs are inclued in a box of jobs.
The server execute the box , and all the jobs in this box are executed. The first job ( in this case the Unzip.cmd) is executed and the second is the job Rename.cmd.

That is when some times the Unzip takes too much time and lock the file until the job finish and then unlock the file so that the Rename.cmd can use it.

This is when the rename.cmd fails in rename the file because when the Rename.cmd is executed, the required file is locked by the other job.

And if there any way to ask for something like this, that if the previus job it's not done do not execute the next job?


Thanks again.


----------



## TheOutcaste (Aug 8, 2007)

Easiest way is to combine the two jobs. Create a new job named unzip-rename.cmd
this job will have just the following:_*
<path>*_unzip.cmd
_*<path>*_rename.cmd

(The path only needs to be specified if the unzip and rename files are not in the same folder as unzip-rename.cmd)

Rename will not run until unzip finishes.

The tasklist utility won't work as the process name for all batch files will be cmd.exe. There will be no way to determine which cmd.exe process is the unzip.cmd process unless it's the only batch script running.

HTH

Jerry


----------



## Codiah (Sep 27, 2008)

-Fabez- is right with this one, so i would suggest using TheOutcaste idea if your able to do so, combining them into one job would have the desired effect... are there instances where you have to use one command without the other ever?


----------



## g_01001 (Sep 22, 2008)

Actually, I can't change the jobs to include both(rename and unzip) in a new job.

Thanks Again!


----------



## TheOutcaste (Aug 8, 2007)

Only other option I can think of is to check for the process name of the unzip utility and have the rename job wait until it is not present before it tries to rename.

Problems with this:

If you have more than one unzip process task running, it won't run until they all close.
If the rename job starts before the unzip job, it will try to rename immediately.
You could have it wait to make sure the process exists first, then wait for it to exit, but if the unzip process finishes before the rename job starts, it will hang.

Another possibility if you are using WinZip or 7Zip:
When WinZip extracts a file, it is first extracted into a temp folder, usually %temp%. When it's finished extracting, it copies the file(s) to the destination folder. The file(s) are not deleted from the temp folder until WinZip exits.

So, you monitor the destination folder for the file. If it's there, you check the temp folder and wait until it's gone, meaning WinZip has exited.

7Zip is similar, except it creates a randomly named folder in %temp%, which is deleted once the file(s) are copied.
The folder name looks like this: *7zE14E3.tmp*
The portion in red appears to be an incrementing 5 digit Hexadecimal number. If you have multiple unzip operations occurring at the same time, you would have to check the destination folder for the file(s), once it exists there, check the %temp% folder, search all *7z?????.tmp* folders to see if the file(s) exists and if found, wait for that folder to be deleted. If not found, or no *7z?????.tmp* folders exist, it should be safe to start the rename job.

Obviously all of these would require re-writing the rename job to do these tests.

I tested this with the GUI version of WinZip and 7Zip, not the command line, so it may operate differently, or may need to have the temp folder specified on the command line.

HTH

Jerry


----------



## g_01001 (Sep 22, 2008)

Great! Thanks a lot again.


----------



## avisitor (Jul 13, 2008)

You could also have the unzip script put a temporary file in the directory that the rename script polls for and runs when it's deleted.

That is, if it runs ahead of time, it looks (say every 30 seconds) for the temporary file, if it's there, it sleeps and waits another 30 seconds. When it's gone, it does its job.


----------

