Page 1 of 1
Background program in a batch file
Posted: 14 Feb 2011 09:08
by ridgeman
I have written a program in BASIC to do my backups for me and placed this program in a batch file. It works ok but I want to run another program (I guess in background) to place the elapsed time on the screen during the backup. Some of the directories take a long time to backup so I don't know how to update the time during the program. I am actually using the Shell to do the copy. I thought I could run the elapsed time program in background and display it on the screen while the BASIC program runs. I may be going about this all wrong. I need help. Thanks in advance.
Ridgeman
Re: Background program in a batch file
Posted: 14 Feb 2011 13:17
by aGerman
Well, I said it a hundred times: Processing DateTime values in a batch file is strange.
Without guarantee:
Code: Select all
@echo off &setlocal &mode con cols=50 lines=3
set "appPath=C:\Windows"
set "appName=notepad.exe"
title Start %appName%
echo(
call :GetDelCr
call :GetInternational
call :GetSecs "%date%" "%time%" startSecs
start "" "%appPath%\%appName%"
:loop
tasklist /fi "imagename eq %appName%" /nh | findstr /bic:"%appName%" >nul &&(
setlocal enabledelayedexpansion
call :GetSecs "%date%" "%time%" currentSecs
set /a diffSecs=currentSecs-startSecs
call :GetFormattedTime !diffSecs! formattedTime
<nul set /p "=%del%!cr! %appName% running !formattedTime! "
title Elapsed !formattedTime!
endlocal
goto loop
)||(
setlocal enabledelayedexpansion
call :GetSecs "%date%" "%time%" currentSecs
set /a diffSecs=currentSecs-startSecs
call :GetFormattedTime !diffSecs! formattedTime
<nul set /p "=%del%!cr! %appName% finished after !formattedTime! "
title Finished After !formattedTime!
endlocal
)
pause>nul
goto :eof
:GetDelCr
:: creates DEL and CR character
setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=#" %%a in (
'prompt #$H#^&for /f %%b in ^('copy /z "%~f0" nul'^) do %%b# 2^>nul'
) do (endlocal&set "del=%%a"&set "cr=%%b")
goto :eof
:GetInternational
:: Sets a bundle of variables by reading the registry settings
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"') do set "%%a=%%c"
goto :eof
:GetSecs "dateIn" "timeIn" secondsOut
:: Output: Seconds elapsed since 1th Jan. 1970 00:00:00
setlocal
set "dateIn=%~1"
for /f "tokens=2" %%i in ("%dateIn%") do set "dateIn=%%i"
for /f "tokens=1-3 delims=%sDate%" %%a in ("%dateIn%") do (
if %iDate%==0 set /a mm=100%%a%%100,dd=100%%b%%100,yy=10000%%c%%10000
if %iDate%==1 set /a dd=100%%a%%100,mm=100%%b%%100,yy=10000%%c%%10000
if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100
)
for /f "tokens=1-3 delims=%sTime%%sDecimal% " %%a in ("%~2") do (
set "hh=%%a"
set "nn=%%b"
set "ss=%%c"
)
if 1%hh% lss 20 set hh=0%hh%
if "%nn:~2,1%" equ "p" if "%hh%" neq "12" (set "hh=1%hh%" &set /a hh-=88)
if "%nn:~2,1%" equ "a" if "%hh%" equ "12" set "hh=00"
if "%nn:~2,1%" geq "a" set "nn=%nn:~0,2%"
set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2,j=j/5+dd+y*365+y/4-y/100+y/400-2472633,j=j*86400+hh*3600+nn*60+ss
endlocal &set "%~3=%j%"
goto :eof
:GetFormattedTime secsIn timevalueOut
:: generates a time value
setlocal
set /a h=%~1/3600, x=(%~1%%3600), m=x/60, s=x%%60
set "hh=0%h%" &set "mm=0%m%" &set "ss=0%s%"
endlocal&set "%~2=%hh:~-2%:%mm:~-2%:%ss:~-2%"
goto :eof
I used notepad as an example. Change the values of appPath and appName to your BASIC application.
Regards
aGerman
[EDIT]
wrong line corrected from
if %iDate%==2 set /a yy=10000%%c%%10000,mm=100%%b%%100,dd=100%%a%%100to
if %iDate%==2 set /a yy=10000%%a%%10000,mm=100%%b%%100,dd=100%%c%%100[/EDIT]
Re: Background program in a batch file
Posted: 14 Feb 2011 19:51
by ridgeman
Thanks, I'll give it a shot.
Dan
Re: Background program in a batch file
Posted: 16 Feb 2011 14:11
by jeb
Hi aGerman,
it is a nice idea in the :GetDelCr function, to combine them into one.
But I doubt that the :GetSecs conversion, can calculate the correct values.
I suppose it should always overflow, because of the year 2011, 2011*365*86400 is much greater than 2^32
The (usable range) is only 2^31 and therefore this is enough for a range of ~68years
jeb
Re: Background program in a batch file
Posted: 16 Feb 2011 14:28
by aGerman
jeb wrote:The (usable range) is only 2^31 and therefore this is enough for a range of ~68years
Hello jeb, you're absolutely right.
aGerman wrote::GetSecs "dateIn" "timeIn" secondsOut
:: Output: Seconds elapsed since 1th Jan. 1970 00:00:00
For that reason it works (hopefully).
Regards
aGerman
Re: Background program in a batch file
Posted: 16 Feb 2011 14:35
by jeb
Ok you got me
I didn't read the comment and I didn't realize the -2472633 before the multiplication begins.
jeb