Used a lot of time to se if i could figure this out but no luck.
My issue currently is via CMD i can do
taskkill /F /FI "WindowTitle eq Administrator: Server2" /T
But i add this to a bat file, it removs the extra space between Administrator: Server2 and for some reason that wont work ofc.
The thing i wanna do is being able to close down a specific cmd with a bat script, since that CMD runs a game server, and i dont want to target them all ad once.
Is there any way to only close the specific CMD?
Since "WMIC process" doesn't include "windowtitle", I reverted back 'tasklist' and 'findstr /l /c:"your string goes here"'.
The "/l" and "/c" together allow you to specify extra spaces. Although I don't have an example on my system with the same number of spaces your example has, I recall using for something similar in the past. Please try this and let me know:
Cmd
for /f "tokens=2" %A in ('tasklist /v /fi "imagename eq chrome.exe" ^|findstr /l /c:"Administrator: Server2"') do #echo taskkill /pid %A
Related
I use a windows .bat script to start all of my game servers
The script looks like this:
cd "C:\Users\Administrator\Desktop"
start Minecraft_HUB
start Minecraft_LOBBY
start Minecraft_SURVIVAL
with many more servers
It works for launching all of them at the same time but I need to manually type stop / exit in every single one to shut them down.
Is there any way I could have a .bat script that would insert and run a save & shut down command in a running cmd console?
I don't yet know how to make that script
From the prompt:
for /f "tokens=1,2delims=," %b in ('tasklist /fo csv') do echo %~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %~c
As a batch-file line:
for /f "tokens=1,2delims=," %%b in ('tasklist /fo csv') do echo %%~b|find /i "minecraft">nul&if not errorlevel 1 echo taskkill /pid %%~c
Note that the taskkill command will merely be echoed to the console. Remove the echo before the taskkill to actually execute the taskkill
Untested. Use at your own risk.
Disclaimer: I have absolutely no coding experience in any language.
I need to create a script that can find the PID for a process, of which there will be multiple running, and then create a process dump for each PID. Procdump doesn't allow the process name to be used if more then one is running, so my goal is to to have the PID found for the process and then be set to variables for later commands.
Here is a .bat I have made:
for /F "tokens=2" %%K in ('
tasklist /FI "ImageName eq T.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
set "X= %%K"
)
c:\Users\Public\Documents\Procdump\procdump.exe -ma %X%
pause
I am able to get the last PID set and used in a command, but I can't figure out how to get the first two.
This may seem like a lot of work for one go, but the later goal is for this to run every 2 and 5 hours for 24 hours. I have been able to get that to work so for now i just need to solve the PID problem. In hindsight, maybe powershell would have been a better choice, but i feel like i got close so I want to to see this route through.
There could be three processes max. I was thinking it would look something like this:
for /F "tokens=2" %%K in ('
tasklist /FI "ImageName eq T.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
set "X= %%K"
set "Y=???"
set "Z=???"
)
c:\Users\Public\Documents\Procdump\procdump.exe -ma %X%
c:\Users\Public\Documents\Procdump\procdump.exe -ma %Y%
c:\Users\Public\Documents\Procdump\procdump.exe -ma %Z%
pause
If you need to execute procdump.exe nice per item, simply move it into the for /F loop with -ma %%K as the parameters
Background: OS: win10. A single root folder: "U:\11Web\gallery-dl".
Within the root I have 1400+ sub-folders (there are no sub-folders beneath those (and never will be)). (There are also no individual/extraneous files in root (and never will be)).
Each sub-folder has its own .bat file, aGallery-dl.bat.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0" || exit /B
move /Y "Folder2.jpg" "Folder.jpg"
for %%I in (.) do set "FOLDER=%%~nxI"
"%ProgramFiles(x86)%\gallery-dl\gallery-dl.exe" -d "U:\11Web\gallery-dl" --download-archive "%~dp0zzzGDB.sqlite3" "https://www.deviantart.com/%FOLDER%/gallery/all"
if not errorlevel 1 if exist "zzzGDB.sqlite3" del "Folder.jpg"
popd
endlocal
I normally run aGallery-dl.bat manually; open the next folder, double-click to run; next folder run, etc., until I'm running between 5 and 10 cmd windows. Later when they've all completed, I might go thru another 5-10 folders, and so it goes. As each aGallery-dl.bat is executed, the requested files are downloaded and when complete, some cleanup is done on Folder.jpg, and each cmd window closes.
Problem: Looking to automate the running of these a little. Would like a single batch file at the root folder that when run would
Create/update a list.txt/database file of all sub-folders. A list would work here, something simple like dir/l>list.txt but then don't know how count would be kept?
Run the aGallery-dl.bat in the first 5 sub-folders of the list, take a pause of say 15 mins (timeout?), loop and then hit the next 5 folders, and so on until 1400+ are done... Doesn't matter that if I have to reboot, or come back a week later and have to re-run that same batch file, that it starts at the first folder all over again...there is a .sqlite3 database file in each folder that retains all the previously downloaded file information. Starting at the first folder every time would be a feature, downloading only those files that have been updated.
I have no starting code/example as I can't wrap my head around it sufficiently to really begin.
Thanks in advance.
The following batch file can be used to run up to five aGallery-dl.bat parallel.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("U:\11Web\gallery-dl\*") do if exist "%%I\aGallery-dl.bat" (
start "Gallery Download %%~nxI" /MIN %ComSpec% /c "%%I\aGallery-dl.bat"
call :CheckDownloads
)
goto :EOF
:CheckDownloads
%SystemRoot%\System32\timeout.exe /T 1 >nul
for /F "delims=" %%J in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq cmd.exe" /NH /V ^| %SystemRoot%\System32\find.exe /C "Gallery Download "') do if %%J GEQ 5 goto CheckDownloads
goto :EOF
The subroutine CheckDownloads is called after starting one more command process running with a minimized window parallel to the command process which is processing this batch file for the execution of aGallery-dl.bat with a window title being Gallery Download and the folder name appended.
The subroutine waits one second before it runs one more command process in background to run TASKLIST to get a list of running cmd.exe processes which is filtered with FIND for just those command processes with Gallery Download in window title.
FIND outputs just the number of lines with the searched string which is the number of command processes running currently to do a gallery download. If this number is (greater or) equal five, the subroutine waits again one second before doing again this downloads task check.
A slightly modified version would be:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("U:\11Web\gallery-dl\*") do if exist "%%I\aGallery-dl.bat" if exist "%%I\Folder.jpg" (
start "Gallery Download %%~nxI" /MIN %ComSpec% /c "%%I\aGallery-dl.bat"
call :CheckDownloads
)
goto :EOF
:CheckDownloads
%SystemRoot%\System32\timeout.exe /T 1 >nul
for /F "delims=" %%J in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq cmd.exe" /NH /V ^| %SystemRoot%\System32\find.exe /C "Gallery Download "') do if %%J GEQ 5 goto CheckDownloads
goto :EOF
There is additionally checked if the file Folder.jpg exists in the subfolder to omit all the subfolders on which the gallery download was done already once in the past and so the file Folder.jpg does not exist anymore in the subfolder.
The timeout value can be increased from 1 to 5 or 10 or 30 seconds to reduce the CPU usage for checking the downloads tasks.
There should not be started too many gallery downloads parallel as this could be counterproductive depending on CPU cores and download bandwidth.
The batch file posted here finishes already on up to last four gallery downloads are still running parallel.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
find /?
for /?
goto /?
if /?
setlocal /?
start /?
tasklist /?
timeout /?
I adapted my linked code slightly to fit your environment. (untested, so work with a copy or have a backup)
#ECHO off
setlocal
set bunch=10
for /D %%I in ("G:\tmp\*") do (
call :loop
echo processing: %%a
start "MyCommand" cmd /c "%%I\zzzGallery-dl.bat"
)
call :loop
goto :eof
:loop REM waits for available slot
for /f %%x in ('tasklist /fi "windowtitle eq MyCommand" ^| find /c "cmd.exe"') do set x=%%x
if %x% geq %bunch% goto :loop
goto :eof
%bunch% is the number of threads that run parallel. You have to experiment a bit to get the optimal number. It depends on how much load each thread generates and the specs of your computer.
Sometimes it's right in front of my face... Works one folder at a time, but works. 1400+ folders later I should be good... Thanks for your help Stephan!!
#echo off
setlocal EnableExtensions DisableDelayedExpansion
for /D %%I in ("G:\tmp\*") do ("%%I\zzzGallery-dl.bat"
endlocal
UPDATE: Do wish though that it would run 5-10 of these at a time in parallel. Sitting here watching it do these in serial one folder at a time, even after the first run-thru, gonna take days to complete 1400+ even just for updates.
Anyone able to assist in that regard would be GREATLY appreciated.
Can I use a batch file to determine if a program is running or open and, if it is, close a different program?
For example, how could I check to see if game.exe is running and, if it is, then close google.exe?
#For /f "Delims=:" %A in ('tasklist /v /fi "WINDOWTITLE eq New Folder"') do #if %A==INFO (
echo Prog not running
) else (
taskkill /im program.exe /f
)
Also see for /?. You may want to pipe through findstr to clean it up a bit.
I'm trying to check what the status is on my XenApp servers for the spoolsv.exe process. I've got the command down to run individually from my XP workstation, but can't seem to get it to iterate through a text file. Here's what I have so far, what will make this populate Servers X-XX on my CMD screen?
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO echo tasklist /S %%G /u domain\userid | find "spoolsv.exe"
pause
I can't seem to get it to run correctly, and sometimes it will just pop up my servers.txt file in notepad and not even run. What am I missing?
As you have it presented, tasklist never runs. The "do echo tasklist..." snippet means the literal string "tasklist /S server-one..." is being echo'ed to stdout. Since none of these literal strings contain "spoolsv.exe", the "find" command won't match anything.
Try the following instead:
#echo off
FOR /F "usebackq" %%G IN ("C:\Documents and Settings\userid\Desktop\Scripts\servers.txt") DO call :RunTasklistForOneServer %%G
pause
goto :EOF
:RunTasklistForOneServer
set ServerName=%1
echo Calling server %ServerName%
tasklist /S %ServerName% /u domain\userid | find "spoolsv.exe"