CMD doesn't wait to finish process - cmd

When i use command
<path exc.xlsx>
it starts excel file working and wait until i close the file than i can type next command. My issue is that when other excel file is running cmd doesn't wait to close the file and goes to next line. It is necessary to type the command that will force cmd to wait until i close excel file even when onother excel file is running. I tried a lot of commands from the internet but no one seems to work properly. These were for instance:
start /wait exc.xlsx && exit
exc.xlsx cmd /k
exc.xlsx|rem
I'm using Windows7.

I try this method but it sometimes doesn't work properly. The line <os.rename> try to change name of the file. If it can it means that the file is closed and break the loop but using that the script sometimes can't open the excel file. I'm not as advanced as i would be and cant find another way to check if the file is closed.
import os
def is_open(file_name):
if os.path.exists(file_name):
while True:
try:
os.rename(file_name, file_name)
break
except:
continue
time.sleep(0.001)
is_open('exc.xlsx')
That's not very dignified and uses a lot of processor. If there is a way using programing language (preferably python) I can implement that in my script.

Related

Python.exe stays open after running batch file in task scheduler

I'm trying to schedule in task scheduler:
Run a batch file
Activate a conda environment
Run a python program
Exit command prompt
Everything works fine, except the python.exe window will remain open while the command prompt closes.
My batch file: (the sleep is for the python code to run. It takes a few seconds)
call activate python2
start C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
sleep 30
exit
My python script:
driver = webdriver.Chrome(executable_path="C:\path\to\chromedriver")
driver.get('http://website.com')
# Find email and pw fields and then fill them in
email = driver.find_element_by_id("user_email")
email.send_keys('myemail#email.com')
pw = driver.find_element_by_id("user_password")
pw.send_keys('password')
# Click on sign-in button
driver.find_element_by_class_name("button").click()
time.sleep(5)
# Click on save button to update
driver.find_element_by_class_name("button").click()
# Close driver
driver.close()
Last thing, the program/script is the batch file, no arguments, and the start in is in the directory that the batch file is in.
Any help would be appreciated!
put you python codes in a main() function.
and give:
if __name__ == '__main__':
main()
at the end.
Just tested works for me.
#pk2019 's answer really helped me.
One improvement is to use
drv = webdriver.Chrome()
# Do your things.
...
drv.close()
drv.quit()
No need to do the dirty work of killing task.
I'm not a python expert, but I think you just need to call sys.exit() or quit().
Instead of using sleep and waiting too long or possibly not long enough, call start with the wait option:
call activate python2
start /WAIT C:\Users\Chris\Anaconda3\envs\python2\python.exe testtest.py
exit
If you don't need the batch file to do anything else, you can just start the python script and exit.
I had a similar problem with a Python selenium web scraper running geckodriver.exe Firefox web driver on Windows (executed via Task Scheduler using a .bat file). The problem is that my geckodriver.exe process is still running after the Python script is done ... and I think that running process is preventing the Windows command prompt from closing.
In order to test this hunch, I inserted the tasklist command into the .bat file, both before and after the Python script. It prints out a list of running Windows tasks to the console ... I noticed the geckodriver.exe file was still running the Python script was finished.
The way to kill a Windows process (using taskkill) is described in these two different Stackoverflow responses:
Safely killing a python script on Windows
Batch script to close all open Command Prompt windows
How to close Command Prompt window after Batch file execution in python?
Here is the Windows documentation for taskkill:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill
In my case, I added this as the 2nd to last line: taskkill /im geckodriver.exe /f, and the last line was exit. That worked.

How to open xlsx file withough hanging a perl process

I am trying to open a xlsx file in a Perl script with the command :
system("path_to_file\\file.xlsx");
The file is opened but it keeps the perl process hanged until I close the .xlsx file. How can I avoid that?
The file file.xlsx is created in the perl script. At the end of the execution I want it to be displayed to the user (like you would manually open it) and it works but it keeps the perl process hanging until i close it.
On Windows, system(1, $cmd) will run the command and immediately return without waiting for the child to finish. See perlport.
On Unix, fork and exec will work to this end.

start /wait fails to wait if file already open

system( "start /wait file.docx")
This starts the file but fails to wait if another docx file already open. Works perfectly if there is no file open.
What I am trying to do : I would like to open a file in windows with its default editor and wait for the user to input and do some changes to the file post save, hence I am using
/wait.
Thanks for any tip?
The default behaviour in winword is to reuse existing instances of the executable to open multiple documents. So, the second open file operation delegates its work into the existing one and exits, so the start command returns.
One usual option is to use COM to open the file and test for closing of the instance. But i know nothing of ruby or if it supports COM.
The best approach will be locate the winword executable and call it directly using as paramenters /w filename.docx to force opening the file into a new instance.

Logging the exit of a batch file

So I wrote a tool with some batch commands, nothing specific. In the beginning the user can choose which task to perform thanks to a loop.
In that loop I included the "Q" option, as to quit the batch file. When this happens, it gets written to a logfile to check when the user started the script(s), and when it ended.
The issue is this only happens if the user actually quits/exits with Q. If (s)he quits by just closing the batch file, this won't be logged.
In short: how can I record when the user has quit the batch file without using the build-in function?
a batch file can't receive the "exiting"-event. What you can do is:
Make a launcher.bat file, that starts the original (yourfilename).bat file with:
start /wait (yourfilename).bat
the launcher.bat file will now wait until you close the second (yourfilename).bat file. place your log-information on the next line of launcher.bat
convert launcher.bat to launcher.exe using bat to exe converter (and make it invisible).

How to return an error code from a process that runs in another cmd window (windows)

I'm running a script from the cmd prompt. This script opens another cmd prompt and runs another batch file there. I want to wait for the error code and then send it back to the original cmd window. Is there a nice way to do this without writing the error code to a file?
Thanks,
Li
If I inderstand you correctly, you want this solution. It solves the problem of returning error level to the calling script from the script that was run in a separate cmd session.

Resources