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
Related
I have a batch program that opens up programs and files in specific order so I can have everything open up without even a single click when I sign in to the computer. This script will run automatically for both cold boots and by adapting the answer it will be possible to run the script to simply open windows not already opened without creating duplicates.
This bit of code works perfectly for almost everything I need by determining if a program is already running or not:
SETLOCAL EnableExtensions
set EXE=notepad++.exe
FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto FOUND
start C:\MEDIA\OFFICE\Notepad++\notepad++.exe
goto FIN
:FOUND
echo Notepad++ is already running.
:FIN
However I am not sure how to use this method (or what alternative methods exist) that will tell me if the following specific shell folder is open or not.
The desired/working path (My Documents) is:
start %windir%\explorer.exe shell:UsersFilesFolder\JAB Creations
The absolute path (My Computer) is:
D:\My Documents\JAB Creations
Important Clarifications
The directory is a symbolic link, that command if you are curious is Mklink /J "C:\Users\John\JAB Creations" "D:\My Documents\JAB Creations".
I do not want to use the absolute path, I want the directory opened under My Documents, not My Computer.
I have removed the garbage entries in Windows Explorer such as Libraries and Quick Access and I use My Documents (which is now called User Files). This command opens the directory via My Documents instead of having it listed under My Computer in example.
If you are testing you will need to enable Expand to open folder and Show all folders options in the Folder Options window under the View tab otherwise useful directories are hidden by default.
Important Prerequisites
First fix the folder options otherwise an attempt is pointless:
Secondly this is what should visually occur:
In Windows 10 explorer.exe feeds a window title of N/A by default, but there is a work-around if you are launching these explorer instances.
Opening explorer regularely (by running explorer.exe or opening the icon) will result in the following title query:
C:\Users\***>tasklist /fi "imagename eq explorer.exe" /fo list /v
Image Name: explorer.exe
PID: 5320
Session Name: Console
Session#: 1
Mem Usage: 141,516 K
Status: Running
User Name: ***\***
CPU Time: 0:31:41
Window Title: N/A
While launching explorer.exe with the title flag (don't even need to specify a string) will force explorer to keep its title value accurate. So in your code above, call explorer.exe like so:
C:\Users\***>explorer.exe /Title
This will result in our title query returning the actual title (and therefore current open folder):
Image Name: explorer.exe
PID: 13888
Session Name: Console
Session#: 1
Mem Usage: 45,964 K
Status: Running
User Name: ***\***
CPU Time: 0:00:00
Window Title: Documents
So now all you need to do is query all instances of explorer with tasklist /fi "imagename eq explorer.exe" /fo list /v and parse the returned data for the folder name you wish to identify, like below (I check for "Documents" in the title):
C:\Users\***>tasklist /fi "imagename eq explorer.exe" /fo list /v | find "Documents"
Window Title: Documents
C:\Users\***>
The above query will return 0 code or 1 code for found/not found. You can work with this to have your code check if a specific folder is open in windows explorer(.exe). I hope this has given you a good grasp on Windows 10's quirky explorer usage!
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).
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
)
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"
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.