I am trying to work out how to run a batch file when a text file is created. I have tried to research but am unable to find anything that helps.
The Pseudocode of what I want is something like this:
When "Response.txt" is created run "2 Week.bat".
Thanks.
#echo off
:start_loop
if exist "c:\Response.txt" (
start cmd /c "c:\2weeks.bat"
goto :exodus
)
rem :: wait for 1 minute
w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:60 >nul 2>&1
goto :start_loop
:exodus
This will work on Vista and later:
#echo off
:loop
if exist "c:\folder\Response.txt" "c:\folder\2 week.bat"
rem - reduce CPU usage by waiting for 15 seconds
timeout /t 15 /nobreak
goto :loop
Related
This can be two separate .bat files if needed, but I would prefer if it's possible on one.
I need to run a certain program, but it sometimes crashes.
I have this for a .bat so far.
It works as far as resetting the program every hour, but it does crash sometimes in between restarts.
I'd like to just have a check, so it launches if the program isn't found.
Is that possible?
#echo off
:loop
start "xx" "xxpath"
timeout /t 3600 >null
taskkill /f /im "xx" >null
timeout /t 4 >null
goto loop
Here is a sample batch that can check if any instance of chrome.exe is running or not
If not, we start it !
#echo off
Color 0A
Set WaitTimeSeconds=20
Set App_Path=C:\Program Files\Google\Chrome\Application\chrome.exe
for %%i in (%App_Path%) do set App_Name=%%~nxi
Title Checking if any "%App_Name%" instance is running ...
:Loop
Rem Clear the screen
cls
Rem Kill any application that have a status equal to not responding !
Taskkill /f /fi "status eq not responding">nul 2>&1
Rem Check if any application instance is running ; if not we start it !
TASKLIST | FINDSTR %App_Name% || START "" "%App_Path%"
Timeout /t %WaitTimeSeconds% /nobreak>nul
Goto Loop
I had made an auto-backup for some videogame files and directorys. This little .bat just make a backup every 30 minutes. Now my problem is this: I want to close this window/process/file. Can I close it directly from itself if I press the X button on keyboard?
I've made this file on Win XP Home so please don't write commands that are only for WIN 10 or something else, if possible.
If it can be helpuful here is my .bat file code:
#echo off
:Minimize
if "%1" == "" start "" /min "%~f0" MY_FLAG && exit
:path ompressor
set d=%:\backup%
set e=%:\Documents and Settings\admin\Application Data\.minecraft%
:backup
set b=%date:/=%
set c=%time%
mkdir "C%d%\saves\%b%\%c%"
xcopy /s /e /y "C%e%\saves" "C%d%\saves\%b%\%c%"
set b=%date:/=%
set c=%time%
mkdir "C%d%\resorcepack\%b%\%c%"
xcopy /s /e /y "C%e%\resourcepacks" "C%d%\resorcepack\%b%\%c%"
msg * Worlds backupped!
:Countdown_Setup
set Days=0
set Hours=0
set Minutes=30
set Seconds=0
:Countdown
CLS
ECHO %Days% - %Hours% - %Minutes% - %Seconds%
ECHO Days Hours Mins Secs
sleep 1
IF "%Seconds%"=="0" (
IF "%Minutes%"=="0" (
IF "%Hours%"=="0" (
IF "%Days%"=="0" (
GOTO backup
)
SET /a Days -=1
SET Hours=24
)
SET /a Hours -=1
SET Minutes=60
)
SET /a Minutes -=1
SET Seconds=60
)
SET /a Seconds -=1
GOTO Countdown
Sorry if I ask a so unusual thing but I searched a lot on the net and I've tried a lot of commands like IF, SET, AT, TSKILL.
I'm a bit new on programming so please make something steb-by-step if you can.
Thanks in advance
Sounds to me like your script ia heavy on memory, but regardless.
In your main script, after #echo off add:
Title gamebackup
Create a new batch file called. Killgame.cmd
The simply add:
taskkill /F /FI "WindowTitle eq gamebackup*"
Then run the secondbatch to kill the process.
I program to both learn how and to make fun files for jokes and tricks. I'm trying to make a batch file that will run a label inside the batch file for a set amount of time like 30 seconds without having to use a for-do statement. what I have so far is shown below and is reduced to a small test, but uses a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
pause
:matrix
echo %random%%random%
use timeout command, it can set amount of time like 30 seconds without having to use a for-do statement.
# echo off
for /l %%a in (1,1,10) do (
call :matrix)
echo Thanks for using the MATRIX
timeout 30
:matrix
echo %random%%random%
if you don't want to show count done message, you can use timeout 30 >nul
this starts a second cmd process (minimized), that simply does a timeout 30. It's existence is the signal to repeat the loop.
#echo off
start /min "MyTimer" timeout 30
:loop
echo %random%%random%
tasklist /fi "windowtitle eq MyTimer" | find "Console" >nul && goto :loop
Sadly, tasklist doesn't give useful errorlevels, so we have to use find.
Note: for debugging purposes you may want to remove the start switch /min.
I need a oneliner to wait for a file until it is created.
Is there a way to write that as a windows batch command? I cant have GOTO in it because I use pushd.
something like:
IF NOT EXIST \\blablabal\myfile.txt sleep(30)
The solution below is a one-liner that should be executed at the command-prompt, as requested (a Batch file is not a "one-liner"):
cmd /C for /L %i in () do if not exist \\blablabal\myfile.txt (timeout /T 30 ^>NUL) else exit
If you wish, you may insert this line in a Batch file doubling the percent sign.
I'd suppose that you want to avoid goto statement and :label inside a parenthesized code block. Use call as follows:
(
rem some code here
call :waitForFile
rem another code here
)
rem yet another code here
rem next `goto` skips `:waitForFile` subroutine; could be `goto :eof` as well
goto :nextcode
:waitForFile
IF EXIST \\blablabal\myfile.txt goto :eof
TIMEOUT /T 30 >NUL
goto :waitForFile
:nextcode
However, if you need a oneliner to wait for a file until it is created, written as a windows batch script: save next code snippet as waitForFile.bat
#ECHO OFF
SETLOCAL EnableExtensions
:waitForFile
IF EXIST "%~1" ENDLOCAL & goto :eof
TIMEOUT /T 30 >NUL
goto :waitForFile
Use it as follows:
from command line window: waitForFile "\\blablabal\myfile.txt"
from another batch script: call waitForFile "\\blablabal\myfile.txt"
Be sure that waitForFile.bat is present in current directory or somewhere in path environment variable.
cmd /c "#echo off & for /l %z in () do (if EXIST c:\file.ext exit)"
Hammers the cpu though...
I have 4 batch files. I want to run one.bat and two.bat at once, concurrently. After completion of these two batch files, three.bat and four.bat should run at once, in parallel. I tried with many ways but mot works fine.
Can anyone help me over this?
This is easily done using a much simplified version of a solution I provided for Parallel execution of shell processes. Refer to that solution for an explanation of how the file locking works.
#echo off
setlocal
set "lock=%temp%\wait%random%.lock"
:: Launch one and two asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" one.bat
start "" cmd /c 9>"%lock%2" two.bat
:Wait for both scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2) do (
( rem
) 9>"%lock%%%N" || goto :Wait
) 2>nul
::delete the lock files
del "%lock%*"
:: Launch three and four asynchronously
start "" cmd /c three.bat
start "" cmd /c four.bat
I had this same dilemma. Here's the way I solved this issue.
I used the Tasklist command to monitor whether the process is still running or not:
:Loop
tasklist /fi "IMAGENAME eq <AAA>" /fi "Windowtitle eq <BBB>"|findstr /i /C:"<CCC>" >nul && (
timeout /t 3
GOTO :Loop
)
echo one.bat has stopped
pause
You'll need to tweak the
<AAA>, <BBB>, <CCC>
values in the script so that it's correctly filtering for your process.
Hope that helps.
Create a master.bat file that starts one.bat and two.bat. When one.bat and two.bat end correctly, they echo to file they have finished
if errorlevel 0 echo ok>c:\temp\OKONE
if errorlevel 0 echo ok>c:\temp\OKTWO
Then the master.bat wait for the existence of the two files
del c:\temp\OKONE
del c:\temp\OKTWO
start one.bat
start two.bat
:waitloop
if not exist c:\temp\OKONE (
sleep 5
goto waitloop
)
if not exist c:\temp\OKTWO (
sleep 5
goto waitloop
)
start three.bat
start four.bat
Another way is to try with the /WAIT flag
start /WAIT one.bat
start /WAIT two.bat
but you don't have any control on errors.
Here's some references
http://malektips.com/xp_dos_0002.html
http://ss64.com/nt/sleep.html
http://ss64.com/nt/start.html
Just adding another way, maybe the shortest.
(one.cmd | two.cmd) && (three.cmd | four.cmd)
Concept is really straight forward. Start one and 2 in paralel, once done and errorlevel is 0 run three and four.