Batch file taking a long time to run Stuck on error - batch-file

I am trying to run this simple script (Below)but it takes it forever to run. I get an error: the rpc server in unavailable. then it sits there for a long time. It will finally finish but takes a long time. How can I add something to the script to make it quit looking on errors and move on to the next device?
#echo off
FOR /F "tokens=*" %%I in (C:\Users\owner\Downloads\listt.txt) do call :pinger %%I goto :eof
:pinger
echo %TIME% >> ping_09_28_17.txt
ping -n 1 %1 >> ping_09_28_17.txt
wmic /user:username /password:XXXXXXXX /node:%1 computersystem get username >> ping_09_28_17.txt
:: DONE

Related

Comparing User input variable to time command in batch

I'm trying to make a batch file that'll run in the background and shut my computer off when the regional time corresponds to a time I've set so that I can fix my sleep schedule.
However, I'm running into issues testing how to compare the current time to the time I've set as a variable.
I believe that it's due to the format that is returned by the time command and the format I entered not being the same.
I would like to enter a time in the 12-hour format and the computer shuts down at the designated time. But the time command returns a 24-hour format string unless paired with the /t switch.
This is what I currently have as test code that returns a printed line instead of shutting down my computer:
#echo off
set /p shutdownTime=What time would you like to shut down the computer?
:begin
set time=time /t
if "%time%" EQU "%shutdowmTime%" echo Time Confirmed
goto begin
I know that instead of an if statement in a loop I could probably do a for or while loop, but I'm honestly trying to keep it as simple as I can for my nocturnal smooth brain that woke up about 3 hours ago.
Any help is appreciated. Thank you.
Try this:
#echo off
FOR /F "tokens=* USEBACKQ" %%F IN (`time /t`) DO (
SET var=%%F
)
echo %var%
pause
will output 12hr time
#echo off
:resettime
echo make sure to Use caps for AM/PM and no spaces
set /p stime=What time would you like me to shutdown your computer?[example 03:45(AM/PM)]:
cls
echo your computer will shutdown at %stime%
:R
set /p p=would you like to change this time [y/n]:
if %p%==y goto resettime
if %p%==n goto loop
if not %p%==y goto R
if not %p%==n goto R
:loop
FOR /F "tokens=* USEBACKQ" %%F IN (`time /t`) DO (
SET var=%%F
)
set var=%var: =%
if %stime%==%var% goto endloop
goto loop
:endloop
shutdown /s
:a
set /p prompt=Would you like to abort shutdown [y/n]:
if %prompt%==y goto s
if not %prompt%==y goto a
:s
shutdown /a
I also quickly made the program described in your question. Its a bit buggy but it works.

Why is this CMD .bat file stopping?

I've written a simple .bat file to monitor the ping between my PC and Google (in an attempt to monitor roughly the health of the network, and record any dropouts).
The following is in a file called Pingtest.bat
#ECHO OFF
:LOOPSTART
FOR /F "tokens=* USEBACKQ" %%i IN ('%DATE:~0% %TIME:~0,8%') DO (SET "TIMED=%%i")
FOR /F "tokens=* USEBACKQ" %%g IN (`ping -n 1 -w 10000 www.google.co.uk`) do (SET "PING=%%g")
FOR /F "tokens=3 delims=," %%a in ("%PING%") do (set AVG_PING=%%a)
SET AVG_PING=%AVG_PING:~11,-2%
set /a varCheck=%AVG_PING%
IF %VarCheck%==%AVG_PING% (goto :OUTPUT) else (goto :ERROR)
:ERROR
SET AVG_PING=ERROR
:OUTPUT
SET OUTPUT=%TIMED% %AVG_PING%
echo %OUTPUT% >> Pingtest.txt
set "TIMED="
set "PING="
set "AVG_PING="
set "varCheck="
timeout /t 5 /nobreak > NUL
GOTO LOOPSTART
Every 5 seconds, this outputs a timestamp and the ping result in ms (e.g. 23/07/2021 23:35:40 15) and appends it to the Pingtest.txt file. This should run indefinitely.
This .bat file is executed via a .vbs file which executes the .bat silently (code from this post), executed at startup.
The issue is that I would expect this code to run indefinitely until the user session ended (shutdown/logoff), but the code seems to stop on its own after ~350 executions (~30 mins). Is there a reason for this, and can this by bypassed/resolved such that the code can run indefinitely?
P.S. Fully aware this is is probably awfully-written code, so open to any feedback/improvements.
Thanks
IF %VarCheck%==%AVG_PING% (goto :OUTPUT) else (goto :ERROR)
This is the core of the issue. If the DNS lookup or ping outright fails, VarCheck ends up being empty, then this line is parsed nonsensically, and the end result is the rest of the batch file is ignored. Changing it to something like:
IF [%VarCheck%]==[%AVG_PING%] (goto :OUTPUT) else (goto :ERROR)
Will handle the case where VarCheck is empty.

batch file log multiple telnet sessions

I have a batch file that logs into a device gathers inf and logs out. the data is recorded to a txt file. it then opens another telnet session gathers data and logs. my proble is i have two logs. any way to combine them into one?
#echo off
cls
:start
#ECHO OFF
:: Get Info
set /p input1="Enter First IP Address and press ENTER "
set /p input2="Enter Second IP Address and press ENTER "
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt "%input1%"
cscript /nologo 1.vbs
timeout /t 3
taskkill /im telnet.exe /f
timeout /t 2
#ECHO OFF
::Run script to Get Info
start telnet.exe -f C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt "%input2%"
cscript /nologo 1.vbs
echo "DONE!"
pause
Just concatenate both logs in the end with type command
type C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input1%".txt C:\Users\%username%\Desktop\TELNET\CHANGE_ME_"%input2%".txt > C:\Users\%username%\Desktop\TELNET\CHANGE_ME_TOTAL.txt
Before that, you'll need to wait until second telnet is finished and maybe kill it like you did with the first one, or second log file will be locked and unreadable.

scheduling of batch script through task scheduler

I have below script which have output as well as log redirection along with date and tiem written into the log and erro files. i am saving this script with .cmd extension. Manually when i try to run this script through the command prompt it runs perfectly and first write current date and time in output and error log and then start recording the logs. but when schedueled through task schedule it only writes current date and time int othe logs but not hte actual logs. Can someone please let me know how i can schedule this script in such a way it will first record the current date and time and then start recording the logs.
Echo Date:%date% Time:%time% >> error.txt
#echo off
(
Echo Date:%date% Time:%time%
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_EXTRACT.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_EXTRACT.CONF"
:loop
for /f "tokens=2 delims=: " %%a in ('tasklist ^| find "BES_EXTRACT.exe"' ) do (
if "%ERRORLEVEL%"=="0" (
ping -n 10 localhost > nul 2>nul
goto loop
)
)
start "" /wait /b "D:\ITSMaaS\BTscripts\capgemini\BESExtract\bin\BES_DATA_MAP.exe" "-f D:\ITSMaaS\BTscripts\capgemini\BESExtract\conf\BES_DATA_MAP.conf"
) >> Output.txt 2>> error.txt
Use redirection on the batch file. In order to output date and time to both files, you could use this technique:
ECHO Date:%date% Time:%time%
1>&2 ECHO Date:%date% Time:%time%
The first ECHO will write to standard output which would be caught with >> Output.txt applied to the batch file. The second ECHO will write to standard error, that output would be redirected by 2 >> error.txt.
If you are worried that the values might differ slightly (by hundredth of a second, perhaps), you could first store the output string to a variable:
SET "datetime=Date:%date% Time:%time%"
ECHO %datetime%
1>&2 ECHO %datetime%
Set 'program/script' : 'yourfile.bat' WITHOUT PATH
Set 'Start in' the rest of path

batch file for sporadic internet connection

i've seen pingers and auto redials when theres a request time out.
problem:
but im having a hard time researching the 'counter or increment' part.
idea:
heres how it should be:
start pinging continuously while connected to internet,
otherwise
count (or accumulate) request time out until 1min or 60 request time out,
if connection went back after request time out & less than '60 times request time out',reset the
'request time out counter' to zero
if request time out reached 60x :
run another batch or reconnect re-dial up.
loop to internet connection pinging
the closest that i saw:
(but for some reason its not working on my xp)
#echo off
setLocal EnableDelayedExpansion
:loop
ping -n 2 10.174.10.48 >> log
find /i "Reply" < log > nul
if not errorlevel 1 type nul > log & goto :loop
for /f "tokens=1" %%a in ('find /c /i "Request timed out" ^< log') do (
if %%a geq 10 echo file.exe && type nul > log
)
goto :loop
source: http://www.computing.net/answers/programming/ping-bat-file/16605.html
credits to the original poster.
thank you
It would be good to know why the above script is not working. Because possibly other solutions will also not work. If you use a non-English version of windows, you need to replace the text "Reply".
I think the following should work. It just implements the counter. But you can try yourself how you need to set the counter to execute the script after 60 seconds.
#echo off
:reset
set count=0
:loop
ping -n 2 10.174.10.48 | find /i "Reply"
if not errorlevel 1 goto :reset
set /A count=%count%+1
if %count% lss 100 got :loop
call reconnect
goto :reset

Resources