I have python code that does this:
Write type A BAT files ( say abat1,abat2..)
Run type A BAT files using dos command "start abat1".. and so on
Some other code.
Write type B BAT files ( say bbat1,bbat2..)
Run type B BAT files using dos command "start bbat1"
The problem here is that I am unable to implement a logic where I want to make sure that bbat1 should start running only after abat1 has finished since there are some dependencies.Similarly for other type A and B bat files.
Is any logic like this possible in bat file or in the python code itself?
Note: I cannot use "start/wait" due to requirements of running all the type A files in parallel. The "wait" I want is between type A and type B files.
You can use a file indicator:
abat
type nul>running.ind
rem here is the code of your abat
del running.ind
bbat
:loop
if not exist running.ind goto :main
rem wait fife seconds
ping -n 6 localhost >nul
goto :loop
:main
rem more code here
Instead of a file, you can do this also with a registry key.
Related
I have a file, script.py, I would like to schedule a Task Scheduler on my Windows 10 machine to run the script.py every 5 minutes. However, I have some parameters that I want to pass into the script.py file to run it:
python script.py --parameter --parameter < database.txt
I'm not sure how to implement these parameters to the Windows Task Scheduler.
Go to properties of the task.
Under the action tab, add/edit an action.
And you will see options to include arguments/parameters.
That being said, windows task scheduler has always been a hit-or-miss for me. I personally find that using custom schedulers by code is more reliable.
Here is my suggestion:
Step 1) Make a bat file with the following contents in your code's working directory:
"C:\Users\user2\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\user1\Desktop\working-directory\runner.py"
and let's name it something like "start-runner.bat". Also, note that the "runner.py" here is your custom scheduler code and not your actual code.
In the "runner.py", you can use the subprocess module combined with the threading module to asynchronously call your main file "python script.py --parameter --parameter".
And instead of using a simple pipe to redirect output, you can use subprocess module features and redirect the stdout to a file.
For scheduling, import time and use "time.sleep(300)"
Step 2) Make the bat file read-only
Step 3) Copy the bat file object to your windows clipboard
Step 4) Type "shell:startup" in your run command
Step 5) Right-click and "paste shortcut" into the startup folder
Step 6) Make the shortcut read-only
I think you open the Edit Action window for your action and under setting, there is a add arguments(optional) just fill in your paremeters.
Simply create one ".bat" file using python and write code to trigger that script.
suppose demo.bat triggers helo.py with arguments like arg1, arg2 and arg3
content of demo.bat should be
python /path/to/helo.py %1 %2 %3
and in your main python file, schedule task using python like
import os
os.system("schtasks /create /sc minute /mo 5 /tn task_name /tr 'path_to_demo.bat arg1 arg2 arg3' /st 12:00")
This will trigger demo.bat after every 5 minutes and demo.bat will trigger helo.py with arguments.
I've made a bunch of tweaks to NPP following this guide, and so far it's gone over well.
But I have a somewhat annoying problem. I want to use input commands in my code.
If the console is closed, this isn't a problem—it pops up and I can type right away in the input field just fine, no clicking.
But when I edit the code and re-run the script (without closing the console), the console clears and runs the program, but I have to click over to be able to interact.
I don't want to close the NppExec console every time I finish the script.
I don't want to have to click over on the console every time I run the script.
As a bonus, I don't want to have to kill my script every time I run it again, either.
I just want to run, type required inputs, go back to editing, re-run, type, etc. without interruptions, if possible..
Can anyone help me with this?
Thank you....
Please check the NppExec Manual for NppExec ver. 0.6 RC2. It includes a lovely script in the section "4.6.4. Running Python & wxPython":
npp_console - // disable any output to the Console
npp_save // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)" // use the current file's dir
set local #exit_cmd_silent = exit() // allows to exit Python automatically
set local PATH_0 = $(SYS.PATH) // current value of %PATH%
env_set PATH = $(SYS.PATH);C:\Python27 // use Python 2.7
npp_setfocus con // set the focus to the Console
npp_console + // enable output to the Console
python -i -u "$(FILE_NAME)" // run Python's program interactively
npp_console - // disable any output to the Console
env_set PATH = $(PATH_0) // restore the value of %PATH%
npp_console + // enable output to the Console
The command
npp_setfocus con
looks like the one you are looking for.
I am trying to run the windows helpfile compiler (hhc.exe) from a script, but it shows very unexpected behaviour.
When I run it from cmd.exe with hhc pathtohelpproject.hpp, the helpfile is compiled as expected. However, invoking the exact same command from python with the same working directory result in the program returning 0 and no output.
But it gets even more bizarre: I've created a batch file runhhc.bat:
hhc "pathtohelpproject.hpp"
which I ran from the python script by invoking call runhhc.bat, start runhhc.bat and just runhhc.bat.
All of them resulted in the same behaviour. However, with start runhhc.bat the cmd instance was still open after hhc returned, so I tried entering the command manually again, without success. But when I entered the command in a freshly, manually opened cmd, it also didn't work! In fact, it only started working once I closed the cmd opened by my script.
What could be the explanation for this bizarre behaviour? And how can I run the compiler from a script regardless?
It's totally down to hhc.exe, nothing else. The trick is to look at the %ERRORLEVEL% after it runs. It returns "1" despite success. This could be used in a custom command to warn the user it's spurious, if the hhc.exe run is isolated from other stuff.
HHC.exe is using HHA.dll. About HHA.dll info has not been published. Microsoft grant the HHA interface information under non-disclosure agreement (NDA) to approved help ISV's.
D:\_working>"C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp
Microsoft HTML Help Compiler 4.74.8702
Compiling d:\_working\foobar.chm
...
Compile time: 0 minutes, 1 second
22 Topics
87 Local links
2 Internet links
0 Graphics
Created d:\_working\foobar.chm, 305,338 bytes
Compression decreased file by 53,639 bytes.
D:\_working>echo %errorlevel%
1
To go around this and continue you need to add if not %errorlevel% 1 exit /B 1 in a batch file.
#echo off
REM -----------------------------------------
REM batch file is located in D:\_batch
REM HH project file is located in D:\_working
REM -----------------------------------------
cd ..\_working
echo '//--- HH Compiler start --------------------------------------
"C:\Program Files (x86)\HTML Help Workshop\hhc" foobar.hhp
echo '//--- HH Compiler end --------------------------------------
echo '//--- errorlevel -------------------------------------------
echo %errorlevel%
echo '//------------------------------------------------------------
if not %errorlevel% 1 exit /B 1
And a python script calling this batch:
print ("*******************************")
print ("We compile a CHM help file ...")
print ("*******************************")
# First import the 'ctypes' module. ctypes module provides C compatible data types and allows calling functions in DLLs or shared libraries.
import ctypes # An included library with Python install.
# ctypes.windll.user32.MessageBoxW(0, "Open CHM", "Your title", 1) # OK only
messageBox = ctypes.windll.user32.MessageBoxA
# documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
returnValue = messageBox(None,"Compile Help Module (CHM) now?","CHM and Python",1) # 1=OK Cancel, 2=Cancel, Retry, Ignore
if returnValue == 1:
print("Pressed OK")
# How to compile a chm file in Python?
# ---------------------------------
import os
os.system("D:/_batch/run-hhc.bat")
elif returnValue == 2:
print("user pressed cancel button!")
You may be interested in calling a CHM from a python script:
# How to open a chm file in Python?
# ---------------------------------
# os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm")
import os
os.system("hh.exe D:/UserData-QGIS-Python/Projekte/ConnectChm/CHM-example.chm::/garden/garden.htm")
This question already has an answer here:
Monitoring directory with VB Script
(1 answer)
Closed 7 years ago.
I've written in .vbs .bat and .au3 before, so I know a little about scripting. What I'm after is a file (in any of the above languages) that will monitor a directory (Let's just say "C:\Users\User\Desktop\Folder") for files (only .html), and then, when a new file is added it will run a new batch file. My only necessity for this is that it runs in the background, with no command window, from what I know and what I've read, .bat won't allow this, and I can't seem to figure out how to do this with .au3 (AutoIt). I'm totally open to new languages, the "inotifywait" from python was suggested, but I've never written in python. All other questions I've read have been for servers, but I just want this all locally done. If someone could please either provide a script for this, or point me towards a link, question, help file or anything else so I could learn this, that would be amazing.
#echo off
setlocal
:start
set inputFolder=%1"C:\Users\Zac\Dropbox\SoundCloud"
set extension=%2html
IF EXIST %inputFolder%\*.%extension% GOTO exists
goto end
:end
goto start
:exists
timeout 70 > NUL
call Random.bat
goto start
Is my script so far, this works, kind of, this requires me to press a key before continuing, and requires me to do so every time we go to :start, and has a command window.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd /k dir c:\windows\*.*", 0, false
This starts a hidden command prompt (the 0).
If you want it all in vbs see wscript.sleep command and here's how to delete, test, and copy files. Also how to start a program.
Set Sh = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("C:\Users\David Candy\Desktop\nethack-360-win-x86-2\bonD0.O") then fso.deletefile "C:\Users\David Candy\Desktop\nethack-360-win-x86-2\bonD0.O"
If fso.FileExists("C:\Users\David Candy\Desktop\nethack-360-win-x86-2\David%20Candy-David.NetHack-saved-game") then
fso.copyfile "C:\Users\David Candy\Desktop\nethack-360-win-x86-2\David%20Candy-David.NetHack-saved-game", "C:\Users\David Candy\Desktop\nethack-360-win-x86-2\David%20Candy-David - Copy.NetHack-saved-game"
else
fso.copyfile "C:\Users\David Candy\Desktop\nethack-360-win-x86-2\David%20Candy-David - Copy.NetHack-saved-game", "C:\Users\David Candy\Desktop\nethack-360-win-x86-2\David%20Candy-David.NetHack-saved-game"
end If
sh.run """C:\Users\David Candy\Desktop\nethack-360-win-x86-2\NetHackW.exe""", 1
I have this script below where I start a python program.
The python program outputs to stdout/terminal. But I want the program to be started via rc script silently.
I can start the and stop the program perfectly. And it also creates the log file, but dosent fill anything to it. I tried a lot of different ways. Even with using daemon as starter.
Where is my problem?
#!/bin/sh
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr
location="/rpiVent"
name="rpiVentService"
rcvar=`set_rcvar`
command="$location/$name"
#command_args="> $location/$name.log" // Removed
command_interpreter="/usr/bin/python"
load_rc_config $name
run_rc_command "$1"
Piping with > is a feature of the shell and not an actual part of the command line. When commands are programmatically involved, the arguments given them cannot contain shell directives (unless the parent process has special support for shell, like with Python subprocess.Popen(shell=True) (doc).
What in this case you can do is that you can wrap your command (/rpiVent/rpiVentService) to a shell script then invoke this shell script in FreeBSD rc script::
Creat /rpiVent/run.sh:
#!/bin/sh
/rpiVent/rpiVentservice > /rpiVent/rpiVentService.log
and then use this is a command (no args needed).
The correct way to do this is probably by "overriding" the start command using start_cmd variable, like this:
#!/bin/sh
# REQUIRE: DAEMON
# KEYWORD: shutdown
. /etc/rc.subr
location="/rpiVent"
name="rpiVentService"
rcvar=`set_rcvar`
load_rc_config $name
command="$location/$name"
command_interpreter="/usr/bin/python"
start_cmd=rpivent_cmd
rpivent_cmd()
{
$command_interpreter $command >$location/$name.log
}
run_rc_command "$1"