I have to record a session with a program and want the windows task (1hour) to run a .Bat file to hit for example "F1" to stop the recording then run another .bat file to shutdown the pc.
I dont have problem with turning off pc but the one for "F1".
This is the code i thought it gonna work.
#echo off
cd "C:\Program Files\ShareX"
Start "" /b sharex.exe
timeout /T 5 /nobreak >nul
%SendKeys% "{F1}"
timeout /T 5 /nobreak >nul
taskkill /IM sharex.exe /F
Thanks
So, if I understood this question...
This will create the sendkey.vbs to you and run it in your code...
{ sorry my limited English }
#echo off & setlocal enabledelayedexpansion
set "_temp_vbs=%temp%\_temp_file_4vbs_.vbs"
>"!_temp_vbs!"^
(
echo/ Set WshShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Set objShell = WScript.CreateObject^("WScript.Shell"^)
echo/ Wscript.Sleep 1500
echo/ objShell.AppActivate "sharex.exe"
echo/ Wscript.Sleep 1500
echo/ WshShell.SendKeys "({F1})"
)
cd /d "C:\Program Files\ShareX" & Start "" /b sharex.exe
timeout /t 3600 /nobreak && start "" /w "%Windir%\System32\wScript.exe" //nologo "!_temp_vbs!"
>nul 2>nul ((
tasklist | findstr /lic:"ShareX.exe"
) && (
timeout /t 5 /nobreak >nul & taskkill /f /im "ShareX.exe" & del /q /f "!_temp_vbs!"
) || (
call shotdown_pc.bat & exit /b
)) >nul 2>nul
Related
I am using this batch to automatically restart some program, they are running very well, but sometimes the batch exit, which causes process_1 and process_2 also exit. It seems that everything is normal, but the batch itself crashes. Why ?
#echo off
cd %~dp0
set process_1=process_1.exe
set process_2=process_2.exe
set interval=10
:check_service
tasklist > task_tmp.txt
findstr %process1% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_1
)
findstr %process2% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_2
)
timeout /t %interval% /nobreak > NUL
goto check_service
:start_1
start /b "" %process_1%
echo %date% %time% %process_1% " down, up it" >> start_note.txt
goto check_service
:start_2
start /b "" %process_2%
echo %date% %time% %process_2% " down, up it" >> start_note.txt
goto check_service
You can maybe simplify like this:
#echo off
Set "MyProcess1=process_1.exe"
Set "MyProcess2=process_2.exe"
:check
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess1%">nul || echo starting %MyProcess1% && start "process 1" "%MyProcess1%"
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess2%">nul || echo starting %MyProcess2% && start "Process 2" "%MyProcess2%"
%SystemRoot%\System32\timeout.exe /t 10 /nobreak >nul
goto check
Full qualified file names are used for the commands tasklist, find and timeout to make this batch file independent on the values of the local environment variables PATH and PATHEXT and to avoid that the Windows command processor has to search for these three executables.
You can shorten your code like this:
#echo off
pushd "%~dp0"
:chkservice
tasklist | find /I "Process_1.exe" >nul 2>&1
if errorlevel 1 (
start Process_1.exe
echo %date% %time% Process_1.exe Down up, it >>startnote.txt
)
tasklist | find /I "Process_2.exe" >nul 2>&1
if errorlevel 1 (
start Process_2.exe
echo %date% %time% Process_2.exe Down up, it >>startnote.txt
)
timeout /t 10 /nobreak >nul 2>&1
goto chkservice
I have tried multiple different way to make the below possible but cannot find a way to do it. I have a ROBOCOPY script that outputs a VBS messagebox depending on the exit code. However, I want the batch script to continue running and exit even after the messagebox appears. As of right now, I have to click Ok for the VBS message box to close and then the command line closes.
Here is my code:
#ECHO OFF
::Delete DB File
CD "C:\Program Files (x86)\SAP\Agentry Client"
DEL "Agentry.db"
CLS
::Copy new DB file from Fil003
2>nul PUSHD "\\sampleserver\123\123" >nul
ROBOCOPY /ZB /IS /IT /R:3 "\\sampleserver\123\123" "C:\Program Files (x86)\SP\AClient" "AClient.db" /log:"C:\Program Files (x86)\SP\AClient\Acleint.txt" /tee
if %ERRORLEVEL% EQU 16 GOTO ER16
if %ERRORLEVEL% EQU 1 GOTO ER1
if %ERRORLEVEL% EQU 0 GOTO ER0
:ER0
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "NoCopy.vbs"
TIMEOUT /T 3
POPD
exit
:ER1
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start "" cmd /c cscript "Success.vbs"
TIMEOUT /T 3
POPD
exit
:ER16
2>nul POPD >nul
PUSHD "\\sampleserver\123\123\"
Start /b "" cscript "FatalError.vbs"
TIMEOUT /T 3
POPD
exit
You can use mshta in vbs rather than msgbox
Function mshta(ByVal MessageText, ByVal Title, ByVal PauseTimeSeconds)
Set WScriptShell = CreateObject("WScript.Shell")
ConfigString = "mshta.exe vbscript:close(CreateObject(""WScript.Shell"")." & "Popup(""" & MessageText & """," & PauseTimeSeconds & ",""" & Title & """))"
WScriptShell.Run ConfigString
End Function
'PauseTimeSeconds' will close the message, or you can set it to 0 for it to remain open until user clicks ok.
How to end/stop a task/programm
in batch-files quickly ?
If it's possible to stop all programms with one command please say it to me.
I triedtaskkill firefox
But it doesnt work
Thank you for answers ; )
For Example,
TASKKILL /IM notepad.exe
You can replace the taskname notepad.exe with your process.
You can check running process from Task manager » Process
you can find the Firefox process name there, if it is running.
Here is a batch file to kill a process or many processes separated by a space :
#echo off
Title Process Killer by Hackoo 2017
Mode 80,5 & color 0B
Set "TmpFile=%Temp%\TmpFile.txt"
Set "Result=%Temp%\KillResult.txt"
If Exist "%TmpFile%" Del "%TmpFile%"
If Exist "%Resultat%" Del "%Resultat%"
echo(
echo Enter the process name or processes names separated by a space
echo(
set /p "process=What process(es) do you want to kill ? > "
cls & color 0C
Title Killing "%process%" ...
echo(
echo %date% *** %time% >"%TmpFile%"
For %%a in (%process%) Do Call :KillMyProcess %%a
Cmd /U /C Type "%TmpFile%" >"%Result%"
Timeout /T 2 /nobreak>nul
Start "" "%Result%"
::*********************************************************************************************
:KillMyProcess
Set "Process=%~1"
echo Killing "%process%" ...
echo "%Process%" | find /I ".exe" >nul 2>&1 && (
Taskkill /IM "%Process%" /F >>"%TmpFile%" 2>&1
) || (
Taskkill /IM "%~n1.exe" /F >>"%TmpFile%" 2>&1
)
echo *****************************************************************************>>"%TmpFile%"
exit /b
::**********************************************************************************************
I want to manage to execute and manipulate my bat file, but, în background i want a countdown so that after a specific time, no matter what, my bat calls another one that deletes it, or simply closes.
Also i want the bat file to show up on execution maximized. I have set /max after start command, but still minimized. I used în my bat /min and it worked, cant figure out why /max doesent work.
To make it start maximized put this at the top of your script, below #echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
I'll update this answer later with the other part, but I think opening a new batch with a timeout as soon as this one starts with start /B should help a lot.
EDIT
So this starts your script with a second script in it. That second script kills the first script and starts a third cmd to delete itself:
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
SET PID=%A:~16%
echo #echo off > killParent.bat
:: replace the 5 on the next line with any other number
echo timeout /t 5 /nobreak >> killParent.bat
echo start "" cmd ^/c timeout ^/t 1^^^&del "%%~f0"^^^&exit ^/b >> killParent.bat
echo taskkill /f /PID %PID% >> killParent.bat
START /B CMD /C CALL killParent.bat >NUL 2>&1
::any logic here instead of pause
pause
Place your code where the pause is and replace the 5 with the number of seconds you want to wait.
This does have the drawback of not being able to finish before the timer runs out, you can fix that by putting echo title parentKiller >> killParent.bat below echo #echo off > killParent.bat and putting
del killParent.bat
taskkill /F /FI "WINDOWTITLE eq parentKiller *" /T
at the end of your execution path, so at the bottom of your batch file normally. This would then look like this:
#echo off
if not "%1" == "max" start /MAX cmd /c %0 max & exit/b
set T=%TEMP%\sthUnique.tmp
wmic process where (Name="WMIC.exe" AND CommandLine LIKE "%%%TIME%%%") get ParentProcessId /value | find "ParentProcessId" >%T%
set /P A=<%T%
SET PID=%A:~16%
echo #echo off > killParent.bat
echo title parentKiller >> killParent.bat
:: replace the 5 on the next line with any other number
echo timeout /t 5 /nobreak >> killParent.bat
echo start "" cmd ^/c timeout ^/t 1^^^&del "%%~f0"^^^&exit ^/b >> killParent.bat
echo taskkill /f /PID %PID% >> killParent.bat
START /B CMD /C CALL killParent.bat >NUL 2>&1
::any logic here instead of pause
pause
del killParent.bat
taskkill /F /FI "WINDOWTITLE eq parentKiller *" /T
I have a batch file that moves some things around and organizes them. I want to add to the end of that batch some code to rename all the files to their same name but the date in addition to the end in only numbers. Maybe I'm bad at searching because I swear this has been covered but I just couldn't find it. So to summarize, I need help writing code that will convert every file in the folder that does not already have the date at the end, to have the date added to the end. So it needs to check whether there are 8 digits at the end and if not then add the date. I'll post my batch file just in case you need to know what I'm doing
rem #echo off
SETLOCAL enableextensions
Set dat=Date
for %%x in (*.pdf) do (
set "_pdfname=%%x"
call :doAllWork
)
goto :eof
:doAllWork
ECHO start
start %_pdfname%
TIMEOUT /T 2 /NOBREAK
start select.vbs
TIMEOUT /T 1 /NOBREAK
start copy.vbs
TIMEOUT /T 1 /NOBREAK
for /F %%g in ('
wmic OS get LocalDateTime /value^|findstr "="
') do for /F %%G in ("%%g") do set "_%%G"
echo %_LocalDateTime:~0,14%
type NUL > TextFiles\%_LocalDateTime:~0,14%.txt
start TextFiles\%_LocalDateTime:~0,14%.txt
TIMEOUT /T 2 /NOBREAK
ECHO close PDF
start close.vbs
TIMEOUT /T 2 /NOBREAK
start window.vbs
TIMEOUT /T 1 /NOBREAK
start paste.vbs
TIMEOUT /T 1 /NOBREAK
start save.vbs
TIMEOUT /T 1 /NOBREAK
start close.vbs
start enter.vbs
move /-y "%_pdfname%" "OldTimesheets\"
TIMEOUT /T 1 /NOBREAK
ECHO exit loop
if exist *.pdf (
goto :eof
) else (
goto :end
)
:end
cscript MessageBox.vbs "This will be shown in a popup."
So at the End all of the new PDFs in OldTimesheets need to be renamed but the old ones will have the date they were put in there.
Thanks ahead of time! This Community is always great!
#echo off
SETLOCAL enableDelayedExpansion
for /f "delims=" %%x in ('dir /b *.pdf') do (
call :doAllWork "%%x"
)
for /f "delims=" %%x in ('dir /b *.pdf') do (
echo %%x | findstr /r "_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\." >nul
if errorlevel 1 ren "%%x" "%%~fx_!_LocalDateTime:~0,8!%%~nx"
)
cscript MessageBox.vbs "This will be shown in a popup."
pause
exit
:doAllWork
ECHO start
start %1
TIMEOUT /T 2 /NOBREAK & start select.vbs
TIMEOUT /T 1 /NOBREAK & start copy.vbs
TIMEOUT /T 1 /NOBREAK
for /F "delims== tokens=2" %%g in ('
wmic OS get LocalDateTime /value ^| find "="
') do set _LocalDateTime=%%g
echo !_LocalDateTime:~0,14!
type NUL > TextFiles\!_LocalDateTime:~0,14!.txt
start TextFiles\!_LocalDateTime:~0,14!.txt
TIMEOUT /T 2 /NOBREAK & ECHO close PDF & start close.vbs
TIMEOUT /T 2 /NOBREAK & start window.vbs
TIMEOUT /T 1 /NOBREAK & start paste.vbs
TIMEOUT /T 1 /NOBREAK & start save.vbs
TIMEOUT /T 1 /NOBREAK & start close.vbs & start enter.vbs
move /-y "%1" "OldTimesheets\"
TIMEOUT /T 1 /NOBREAK & ECHO exit loop
exit /b
Notes:
Replaced direct enumeration of *.pdf with dir /b *.pdf to prevent possible re-processing of files after they have been renamed.
Added SETLOCAL enableDelayedExpansion to make _LocalDateTime change its value at each assignment, also ! instead of % should be used in such case
Removed SETLOCAL enableextensions as it's enabled by default
Simplified a few things to improve readability
I figured it out on my own! I added
set str=%date%
echo.%str%
set str=%str:/=%
echo.%str%
at the begging and used
ren OldTimesheets\%_pdfname% %str%%_pdfname%
right after the move line. Works like a charm! Thanks for the help anyways!