Monitor a directory for added files [duplicate] - python

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

Related

Calling Python (anaconda) with Batch File from ASP.NET

I am trying to call a python script from my asp.net web app. I am using a batch file to call the python script. The python script runs from the batch file when I run it on the server. When I try to call the batch file from asp.net, the code in the batch file works until it gets to the call for the python, and the call to the python script doesn't work.
This is my batch file:
(
echo Hello
)
C:\inetpub\Sites\**\**\**\test.txt
rem the code above works from ASP.NET
rem this code is never called
set CONDAPATH="C:\\Users\\***\\Anaconda3"
set ENVNAME="base"
if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)
call %CONDAPATH%\Scripts\activate.bat %ENVPATH%
C:\\Users\\***\\Anaconda3\\python.exe C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py
Could anyone help me figure out what I am doing wrong in calling this python code?
The first thing you need to understand is that your line:
C:\inetpub\Sites\**\**\**\test.txt
will open a text file in the default application for doing so, and your script will then wait until you have closed that text file, before it continues to the next line. If you want the code to continue without waiting for the text file to be closed, you can use the start command:
Start "" "C:\inetpub\Sites\**\**\**\test.txt"
Here's a quick example to demonstrate my earlier comment, regarding the recommended syntax for defining variables, and using doublequotes around file paths.
Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
Putting it all together:
#Echo Off
Echo Hello
Start "" "C:\inetpub\Sites\**\**\**\test.txt"
Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
If you don't want the script to wait until the python command has completed, (for instance, if this is the last line of your script), you could change that last line to:
Start "" "%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
I figured it out in case anyone is still using asp.net like me. It was all a permissions issue on my folder that had I was calling the python script from CONDAPATH="C:\Users\***\Anaconda3".
This link gave me the hint: IIS7 Permissions Overview - ApplicationPoolIdentity

Interactive Python Execution in NPP

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.

logic to run set of bat files

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.

User-defined command line arguments in python

Encountered a couple of problems with my python program. Basically, I want to be able to send the path files of multiple images to the command prompt (the user defines how many images, so just putting the direct file paths as args, as below, doesn't do what I want). My code currently looks like this:
os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_00000001.jpg image_00000002.jpg image_00000003.jpg")
There are many more images, so of course, writing out image_00000004, 5, 6, etc, is hardly efficient and depends entirely on there being the same number of images each time, which there isn't. (jpivc.jar is the program that opens upon execution of this code, and imports the images and does stuff - that bit works fine). Ideally, the code would be something like:
for i in range(1, NumberOfImages):
os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_0000000" + i + ".jpg")
Except, you know, without opening jpivc.jar each time i is incremented, I'm just using the above to show the kind of thing I want. Unfortunately this doesn't work, as it only sends the first part in " " to the command line, if it doesn't give an error message - my first question is, is there any way of making this do what I want it to do? I'm relatively inexperienced at python, so please be as gentle as possible with the technical details.
My second question is - is there a way of then closing either the command prompt or jpivc.jar? I've tried all the predefined functions I can think of - os.system("tskill cmd.exe") and variatons thereupon, os.kill() (although I'm using 2.5, so I'm not surprised this doesn't work), Popen.kill() and Popen.terminate(), and even tried writing my own kill function using ctypes. Nothing works - until either cmd.exe or jpivc.jar are closed manually, then everything in the rest of my code works perfectly. I think Python halts until the command line is closed - which isn't helpful when I want to then close the command line from Python! To sum up; why does it halt like that, and how can I fix it?
More extracts from my code can be provided if needed - I'm using Python 2.5.4 on Windows XP Professional. Don't ask me to provide anything from jpivc.jar - I won't even pretend to understand the slightest bit of Java.
Thanks in advance,
Dave
I believe you are looking for something like this
fileNames=""
for i in range(1, NumberOfImages):
fileNames += "image_0000000%d.jpg "%i
os.system( "java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar %s"%fileNames )
Rename your file from script.py to script.pyw to avoid the opening of command prompt.
jpivc.jar will remain open until you close it manually or programmer changes its code to quit after it completes processing all files.
[EDIT]
I found this for starting and killing a process
pidId = os.spawnl(os.P_NOWAIT, "\\windows\\notepad.exe")
import win32api
win32api.TerminateProcess(pidId ,0)

How can I stop python.exe from closing immediately after I get an output? [duplicate]

This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 8 years ago.
Is there any way I can stop python.exe from closing immediately after it completes? It closes faster than I can read the output.
Here is the program:
width = float(input("Enter the width: "))
height = float(input("Enter the height: "))
area = width * height
print("The area is", area, "square units.")
You can't - globally, i.e. for every python program. And this is a good thing - Python is great for scripting (automating stuff), and scripts should be able to run without any user interaction at all.
However, you can always ask for input at the end of your program, effectively keeping the program alive until you press return. Use input("prompt: ") in Python 3 (or raw_input("promt: ") in Python 2). Or get used to running your programs from the command line (i.e. python mine.py), the program will exit but its output remains visible.
Just declare a variable like k or m or any other you want, now just add this piece of code at the end of your program
k=input("press close to exit")
Here I just assumed k as variable to pause the program, you can use any variable you like.
For Windows Environments:
If you don't want to go to the command prompt (or work in an environment where command prompt is restricted), I think the following solution is better than inserting code into python that asks you to press any key - because if the program crashes before it reaches that point, the window closes and you lose the crash info. The solution I use is to create a bat file.
Use notepad to create a text file. In the file the contents will look something like:
my_python_program.py
pause
Then save the file as "my_python_program.bat"
When you run the bat file it will run the python program and pause at the end to allow you to read the output. Then if you press any key it will close the window.
Auxiliary answer
Manoj Govindan's answer is correct but I saw that comment:
Run it from the terminal.
And got to thinking about why this is so not obvious to windows users and realized it's because CMD.EXE is such a poor excuse for a shell that it should start with:
Windows command interpreter copyright 1999 Microsoft
Mein Gott!! Whatever you do, don't use this!!
C:>
Which leads me to point at https://stackoverflow.com/questions/913912/bash-shell-for-windows
It looks like you are running something in Windows by double clicking on it. This will execute the program in a new window and close the window when it terminates. No wonder you cannot read the output.
A better way to do this would be to switch to the command prompt. Navigate (cd) to the directory where the program is located and then call it using python. Something like this:
C:\> cd C:\my_programs\
C:\my_programs\> python area.py
Replace my_programs with the actual location of your program and area.py with the name of your python file.
Python files are executables, which means that you can run them directly from command prompt(assuming you have windows). You should be able to just enter in the directory, and then run the program.
Also, (assuming you have python 3), you can write:
input("Press enter to close program")
and you can just press enter when you've read your results.
In windows, if Python is installed into the default directory (For me it is):
cd C:\Python27
You then proceed to type
"python.exe "[FULLPATH]\[name].py"
to run your Python script in Command Prompt

Categories