Multiple tasks that do the same - batch-file

I'm working on a project that create a x number of tasks that do the same thing, open the same Excel file, but in different intervals. The problem comes when two of the tasks run at the same time, a read only file errors with macros. I tried to put a limit in the bat file that the tasks run, first stopping open other excel files and before do the same with other tasks, but It doesn't work. There is the last code that I wrote.
#ECHO OFF
TASKLIST /FI "IMAGENAME eq TASKENG.EXE" 2>NUL | FIND /I "TASKENG">NUL && (
START %~dp0myExcelFile.xlsm
EXIT
)

Related

Checking for running .bat files

I need to have a .bat file running on a PC all the time, which exports data every 8 hours.
The .bat file I would like to build would make hourly checks to see if the other .bat file is running, and if not start it.
I know tasklist can't find .bat files which is why I'm using WINDOWTITLE instead.
I am assuming that ERRORLEVEL = 1 means the test failed.
I have looked around a lot and best I have come up with is this but it doesn't work.
tasklist /FI "WINDOWTITLE eq Export"
if errorlevel=0 START C:\ExportApps\Export.bat
TIMEOUT /T 3600
GOTO LOOP
When it checks for the second time it starts another instance of the .bat file.
If I change the ERRORLEVEL to 1 I get the following:
INFO: No tasks are running which match the specified criteria.
tasklist doesn't set errorlevel to 1 when there is no finding. (Ab)use find to get a correct errorlevel:
tasklist /fi "windowtitle eq Export" | find /c "cmd.exe" 1>nul && exit /b
REM code for restarting
Schedule it to be run every hour (or consider Compo's advice to schedule your primary batch file, when it actually doesn't have to run all the time for some reason).

tasklist.exe, Win Server 2008 R2, file name has spaces

I have a client running Apple's FileMaker Pro Advanced on Windows Server 2008 R2. The associated executable is "FileMaker Pro Advanced.exe" (note the spaces). My client has had problem with "something" shutting down FileMaker; part of one solution is to write a batch file to run every 15 minutes that'll start FileMaker if it's not running. I'm helping with the batch file.
I proposed code based on this:
set target=FileMaker Pro Advance.exe
set startWith=(batch file that starts FileMaker)
tasklist /FI "IMAGENAME eq '%target%’" /NH 2>NUL | find /I "%target%">NUL
if "%ERRORLEVEL%"=="1" %startWith%
if "%ERRORLEVEL%"=="0" goto :EOF
Please note: The design of the tasklist command line required use of a set of quotes; I used double quotes for those. The presence of spaces in the file name would seem to require another set of quotes; I used single quotes above.
My technique doesn't work; I get an error message about a poorly formed command. Sigh.
Does anyone have a workaround?
Absent better suggestion, I'll ask the sys admin to let me rename that file to something like "FileMakerProAdvanced.exe"; that should work. I'll only get that permission a percentage of the time I ask. I'll need something else in case they decline my request.
(The hawk-eyed among you will notice that the filename is 26 characters long. It seems tasklist has a 25 character limit. I can get around that (in the find command, for example) by searching for "FileMaker Pro Advanced.ex"; that's all tasklist provides find. And I can use double quotes in the find command.)
Thanks in advance for solutions and commiserating.
You will not have any problem with spaces in the imagename filter so there is no need for a workaround, just remove the inner single quotes
set "target=FileMaker Pro Advance.exe"
tasklist /fi "imagename eq %target%"
And the problem with the output length is just for the column mode. You can change to csv output format
tasklist /fi "imagenane eq %target%" /nh /fo:csv | find /i "%target%" >nul
Or, as you are not retrieving any data from the output, instead of hidding the headers, as them are only present if a matching process has been found, check for the presence of the headers
tasklist /fi "imagenane eq %target%" | find "========" >nul

check if any other copy is in process before my xcopy command

I have xcopy batch utility which copies files to network drive.
Is there any way to find if any other user is copying files to the same above location before i start??
The whole idea is to prevent overwriting the files when other users are running the same.
Thanks in Advance
9>"w:\target\file.lock" (
xcopy "r:\source\*" "w:\target"
) && ( del "w:\target\file.lock" )
Use a lock file in your target folder.
What it does is redirect to a file (and lock it) inside the target folder the data sent to the stream 9 (there are 10 streams, 0=stdin, 1=stdout, 2=stderr, 3-9 user defined). Nothing will be written to the stream but the output file will be locked.
As the redirection is wrapping the xcopy command, the lock on the file is maintained until the command ends.
Not absolutely sure but you can use TASKLIST dos command like below; which will get you the information whether an XCOPY exe is running already.
TASKLIST /FO TABLE /NH /FI "USERNAME ne NT AUTHORITY\SYSTEM" /FI "STATUS eq running" /FI "IMAGENAME eq XCOPY.exe"

Giving control to app started from bat file

I have a batch script that tests for the existence of a running program (JoyToKey.exe), if it's not running, start it, if it is running, move on.
Once the app is running, I then launch another app (mgalaxy.exe) but although it is maximized and I can see it, it does not have control. That is I need to do an to get control of the running mgalaxy.exe.
How can I do this so that I don't need to do the . It used to work perfectly on Windows 7, but under Windows 8.0 I have this problem. Code in batch file is:
#echo off
tasklist /FI "IMAGENAME eq JoyToKey.exe" 2>NUL | find /I /N "JoyToKey.exe">NUL
if NOT "%ERRORLEVEL%"=="0" (
echo Launching JoyToKey
cd C:\Mame\jtk374en
START /MIN JoyToKey.exe
)
echo Launching mGalaxy
cd c:\Mame
start mgalaxy.exe
exit
Try adding timeout. I'd be interested to hear how it goes, as I also found focus issues when launching programs from a batch file. This worked for me when starting a text editor.
start mgalaxy.exe
timeout 3 /nobreak
exit

Create batch file to block specific programs

How to create an autorun batch file to block/close portable programs?
For example:
if (xxx.exe is running)
then (close it immediately OR block access to internet)
Thank you.
I have tested this batch command:
#echo off
:TOP
tasklist /FI "IMAGENAME eq taskmgr.exe" /FO | grep taskmgr.exe
if ERRORLEVEL == 0 taskkill /f /im taskmgr.exe
GOTO TOP
but it's failed to run, what is the problem?
I'm pretty sure this isn't possible, in batch anyways.
You could edit registry keys for certain files to stop them from being run but this would be system wide, not just for portable programs.
There are too many variables to consider, and this really isn't something you can do in batch.

Resources