Sorry for my bad english expression ... i'm not native.
I have searched for an answer for a while ... maybe i'm not doing this right but it seems that my (simple) code is not working for a reason.
As soon as I open the batch programm it shuts without me having the time to read the error message :(
could some one please correct this one ??
I am grateful for any kind of help !
#echo off
color 0a
mode 1000
title THE OFFSWITCH
:start
cls
if %time%== "12:00:00.00" goto :time
echo.
echo IT IS NOT TIME YET : %time%
goto start
:time
echo hello
pause>nul
It is pure luck, if your code works. In the most cases, it will just miss the time, because the loop is not fast enough to get the correct centisecond.
Instead of
if %time%== "12:00:00.00" goto :time
you should use
if "%time%" geq "12:00:00.00" goto :time
(if time is "greater or equal" than...)
Also you should consider to put a delay into the loop, because it eats all of the CPU-Time, it can get, and thus slows down your system. For example timeout /t 1>nul waits for one second.
Related
So basically I was working on my Terminal I am creating in batch and this strange error pops up for a second and then the window closes:
"Goto was unexpected at this time"
I have no idea what's going on. Here's my code:
#ECHO off
set codename=Nature
echo Windows Bat Terminal
echo Codename "%codename%"
:terminal
set /p terminalcommand=Command:
if %terminalcommand%==help goto help
if %terminalcommand%==clr goto clear
if %terminalcommand%==exit goto exit
if %terminalcommand%==color goto color
if %terminalcommand%==time goto timedate
echo.
echo Bad command!
:terminal1
goto terminal`
To recreate simply run this in CMD.
You haven't told us what entry you made to generate this behaviour. The standard cure is to quote each side if the if comparison (if /i "%terminalcommand%"=="time" goto ... (the /i make the comparison case-insensitive)) because if you simply press enter then the command is resolved to "if ==time goto ..." and cmd will see goto where it expects a comparison operator like ==, hence the response. If you run this batch from the command prompt, the window won't close and you'll be able to see these messages better
Whenever i run the .bat file it all goes well i choose my time it says it in minutes and all until it comes to confirming your decision and i really have no clue how to fix it for some reason it just goes to :shutdown
Intention: I am using this as a simple little tool when installing games for example if i have to leave steam to download and install a large game overnight i just choose a time and leave it :)
Note: I have no experience with coding ( except changing values on LUA scripts in games ) so an explanation would be
Greatly Appreciated and yes sure i can just use the shutdown command but i wanted to create something a little more fancy
Also if someone could explain to me what the
/s in shutdown
and /a in set timedelay commands do because as i stated i have little to no experience
.
If you can't answer or can't be bothered thank you for your time either way!
Thank you in advance! :)
.
#echo off
cls
title Delayed Shutdown
echo Delayed Shutdown by Martin Angelov
echo Press Any Key To Choose Delay
Pause>NUL
:choosedelay
cls
echo Type in the desired ammount of delay
set /p timedelaysec=
set /a timedelaymin=%timedelaysec% / 60
:confirmation
echo Your current desired shutdown time is:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
echo Press 1 to Confirm Shutdown
echo Press 2 to Change Delay
echo press 3 to Exit Program
set /p confirmaation =
if "%confirmaation%" == "1" goto shutdown
if "%confirmaation%" == "2" goto choosedelay
if "%confirmaation%" == "3" goto exit
:shutdown
cls
echo Shutting Down!
echo Delay Chosen:
echo %timedelaysec% Seconds
echo ( %timedelaymin% Minutes )
pause
exit
:exit
cls
echo Exiting Program...
ping 0.0.0.0 -n 2
exit
:Temporary so it doesn't actually enable the shutdown
shutdown /s /t %timedelaysec%
In batch files, it's usually something simple and frustrating.
Try
set /p confirmaation=
(Note that I removed a space before the equals.)
If that fixes your problems, your environment variable wasn't set to any of the three values you checked for and the logic very naturally went line-by-line to :shutdown.
Good luck!
I am creating a batch file program where if you open a mspaint.exe, if its idle for 10 seconds the program will close automatically. I have created this:
#echo off
:Start
tasklist /FI "IMAGENAME eq mspaint.exe" | findstr "mspaint.exe" >nul
IF "%ERRORLEVEL%" == "0" GOTO Running
IF "%ERRORLEVEL%" == "1" GOTO NotRunning
:Running
***::Check if idle***
GOTO Terminate
:NotRunning
GOTO EOF
:Terminate
timeout 5
taskkill /im mspaint.exe /f
ECHO Paint has been terminated due to inactivity
PAUSE
:EOF
EXIT
I have trouble finding the idle syntax(seems it doesn't exist), is there another way to make the idle time work? And since this is my first time creating a batch file, I really need a helping hand here.
Not the answer you want to hear, but the one that you need. The only way to monitor another process for activity is to install a window hook, but that's going to require a much lower level language (C or C++).
If that doesn't put you off then start with the Microsoft Hooks Overview and post further questions. Good luck (you'll probably need it).
Thanks for the help but that seems complicated. I used SCHTASKS instead and it worked :)
Well, I have this problem with a little program that I'm making because my loop is endless. Right now I'm trying to use goto command but it doesn't work couse it is endless. Anyone please help.
#echo off
color 1a
:i
echo Hi, mate Let's start off by getting each others names.
timeout 5 > nul
echo What is your name ?:
set /p name=
echo Hi noob %name% my name is Youke
timeout 2 > nul
echo %name% We are going on an advenutre quest today :D
timeout 2 > nul
goto i
Somewhere between the i label on line 3 and the goto command, you need to add code that will break out of the loop—likely an if statement with another goto inside it that points to a label after your current goto.
You can see plenty of examples here: https://stackoverflow.com/a/4711984/436282
Good morrow, all.
My first question here, but I've been keeping an eye on this site for a long time. In fact, it's helped me create about three dozen (albeit simpler) batch files so far! I'm finally having a difficult time searching for an answer. Forgive me if it's covered but nothing I've located is quite right for my application.
Here is the code I am working with. The process is as follows. If file.zip exists, goto an unzip command; else, wait five minutes and check again. This will loop continuously until it finds a file.
:checkexist
IF EXIST "\\server\folder\subfolder\file.zip" (
GOTO zipexist
) ELSE (
ECHO.
ECHO File not found. Lets wait, say, 5 minutes ...
ECHO.
TIMEOUT /t 300 /nobreak
GOTO checkexist
)
:zipexist
ECHO.
ECHO Unzipping will begin in 30 seconds.
And the code continues on.
It works beautifully, actually. The issue I am having is if a file never exists - if it never got uploaded, for example or there was no file for the day. I have tried using some options with the SLEEP command and using something I found on MS TechNet about SET DELAY=nn with no avail. I am trying to find a wrapper(?) for this simple if/else statement that will only allow it to run for nn minutes and if no file.zip is found, terminate the batch file. This will, ultimately, run on a server-side process so no user will be available to make a judgement call.
Are there any suggestions on how to accomplish this in a similarly simple way? I realise there are a lot of options but sometimes the syntax confuses me; I'm still learning.
Please forgive any dumb questions that follow this initial post! :) I'm getting there, I'm sorry I'm a little slow.
This implements the suggestion but illustrates a different way to check the file and to continue when the loop has been executed 24 times = 2 hours
set num=0
:checkexist
IF NOT EXIST "\\server\folder\subfolder\file.zip" if %num% LEQ 24 (
ECHO.
ECHO File not found. waiting 5 minutes ... times (%num%^)
ECHO.
TIMEOUT /t 300 /nobreak
set /a num+=1
GOTO checkexist
)
:zipexist
ECHO.
ECHO Unzipping will begin in 30 seconds.
Try placing a counter outside your IF statement, and incrementing it inside your loop. Once the counter becomes your limit, then exit.
Sorry for not providing a code sample, batch scripting was a long time ago for me.