kali Posted May 23, 2016 Posted May 23, 2016 Is there any way to run c/c++ script with bat file? I need batch command to create Digital Stopwatch but got a c++ script. I don't know c++. So I need bat help. Spoiler #include<conio.h> #include<process.h> #include<iostream.h> #include<dos.h> int h=0,m=0,s=0,ms=0; char ch=’p’; void main() { void watch(); watch(); while(1) { if(kbhit()) ch=getch(); if(ch==’s’||ch==’S’) break; if(ch==’e’||ch==’E’) exit(0); } while(1) { watch(); delay(10); if(kbhit()) ch=getch(); if(ch==’r’||ch==’R’) { h=m=s=ms=0; watch(); while(1) { if(kbhit()) ch=getch(); if(ch==’s’||ch==’S’) break; if(ch==’e’||ch==’E’) exit(0); } } else if(ch==’p’||ch==’P’) while(1) { if(kbhit()) ch=getch(); if(ch==’s’||ch==’S’) break; if(ch==’e’||ch==’E’) exit(0); if(ch==’r’||ch==’R’) { ch=’c’; h=m=s=ms=0; watch(); } } else if(ch==’e’||ch==’E’) exit(0); if(ms!=99) ms++; else { ms=0; if(s!=59) s++; else { s=0; if(m!=59) m++; else { m=0; h++; } } } } } void watch() { clrscr(); cout<<“nnnnntttt#############”; cout<<“ntttt# Stopwatch #”; cout<<“ntttt#############”; cout<<“nntttt “<<h<<“:”<<m<<“:”<<s<<“:”<<ms; cout<<“nnnnnnnnntttttttPress Key”; cout<<“nttttttt———“; cout<<“nttttttts -> Start”; cout<<“ntttttttp -> Pause”; cout<<“ntttttttr -> Reset”; cout<<“nttttttte -> Exit”; } Source
jaclaz Posted May 23, 2016 Posted May 23, 2016 That is NOT a "C/C++ script", it is a small C++ program SOURCE CODE, which you got from here:http://www.thecrazyprogrammer.com/2013/06/cc-program-to-create-digital-stopwatch.html Normally such a Source Code needs to be COMPILED (though there are C and possibly C++ interpreters, very probably, like 99.9999% you DO NOT actually want that). Also it seems like targeted to a "pure DOS" (not Windows). Try asking what you *need* or would like to obtain (FINAL GOAL, as opposed to the way you believe you should reach that goal), right now you are slipping on a chiocolate covered banana : http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/put-down-the-chocolate-covered-banana.html jaclaz 1
gunsmokingman Posted May 23, 2016 Posted May 23, 2016 Here is a HTA that uses VBS script to create Demo Timer Here is the code for the HTA <!-- Script By Gunsmokingman Aka Jake1eye --> <TITLE>Demo Timer</TITLE> <HTA:APPLICATION ID="Demo Timer" SCROLL="No" SCROLLFLAT ="No" SingleInstance="Yes" ShowInTaskbar="No" SysMenu="No" MaximizeButton="No" MinimizeButton="No" Border="Thin" BORDERSTYLE ="complex" INNERBORDER ="No" Caption="Yes" WindowState="Normal" APPLICATIONNAME="DTimer" Icon="%SystemRoot%\explorer.exe"> <STYLE type="text/css"> Body { Padding-Top:1pt;Padding-Bottom:1pt;Margin:1pt; Font-Size:10.25pt;Font-Weight:Bold; Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS; Color:Black;BackGround-Color:#EFE9E3; Text-Align:Center;Vertical-Align:Top; } DIV { Font-Size:10.25pt;Font-Weight:Bold;Color:#00A1A1; Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS; } TD { Font-Size:10.25pt;Font-Weight:Bold;Color:#515151; Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS; } BUTTON { Height:15pt;width:51pt;Cursor:Hand; Font:8.25pt;Font-weight:bold; Font-Family:Segoe Ui, Arial,Tahoma,Comic Sans MS; Color:#404040;Text-Align:Center;Vertical-Align:Middle; filter:progid:DXImageTransform.Microsoft.Gradient (StartColorStr='#E5E5E5',EndColorStr='#7D7D7D'); Margin:1;Padding:2; Border-Left: 1px Transparent;Border-Right: 2px Transparent; Border-Top: 1px Transparent;Border-Bottom: 2px Transparent; } </STYLE> <SCRIPT LANGUAGE='VBScript'> '-> Resize And Place In Approx Center Of Screen Dim Wth, Hht :Wth = int(327) :Hht = int(150) window.ResizeTo Wth, Hht MoveTo ((Screen.Width / 2) - (Wth / 2)),((Screen.Height / 2) - (Hht / 2)) Dim C1, H1, M1, T1, Tm1, txt Function Window_OnLoad() C1=0 :H1=0 :M1=0 Counter() End Function '-> To Keeps The Script In An Infinte Loop Until User Interaction Function Counter() '-> Reset The Seconds And Minutes Every 60 Cycles If C1 = 60 Then :M1 = M1 + 1 :C1 = 0 If M1 = 60 Then :H1 = H1 + 1 :M1 = 0 '-> To Display 02 Digits Ex 1 = 01 If Len(C1) = 1 Then C1 = "0" & C1 If Len(M1) = 1 Then M1 = "0" & M1 If Len(H1) = 1 Then H1 = "0" & H1 T1 = Split(Time(),":") If Len(T1(0)) = 1 Then T1(0) = "0" & T1(0) If Len(T1(1)) = 1 Then T1(1) = "0" & T1(1) Tx1.style.color="#9A0000" :Tx1.innerHTML=H1 Tx2.style.color="#009a00" :Tx2.innerHTML=M1 Tx3.style.color="#00009a" :Tx3.innerHTML=C1 Dim S :S = Split(T1(2), " ") If Len(S(0)) = 1 Then S(0) = "0" & S(0) Tx4.style.color="#009595" Tx4.innerHTML=T1(0) & ":" & T1(1) & ":" & S(0) & " " & S(1) C1 = C1 + 1 Tm1=window.setTimeout("Counter()",1000,"VBScript") End Function </SCRIPT> <BODY Scroll='No'><TABLE>Demo Timer</TABLE> <TABLE> <TD><TD>Hours »</TD><TD><DIV ID='Tx1'>00 </DIV></TD></TD> <TD><TD>Minutes »</TD><TD><DIV ID='Tx2'>00</DIV></TD></TD> <TD><TD>Seconds »</TD><TD><DIV ID='Tx3'>00</DIV></TD></TD> </TABLE> <TABLE> <BUTTON ID='Bn1' OnClick="window.clearTimeout(Tm1)">Stop</BUTTON> <BUTTON ID='Bn2' OnClick="Counter()">Re-Start</BUTTON> <BUTTON ID='Bn3' OnClick="window.clearTimeout(Tm1): Window_OnLoad()">Re-Set</BUTTON> <BUTTON ID='Bn4' OnClick="window.clearTimeout(Tm1): window.close()">Close</BUTTON> </TABLE> <TABLE> <TD><DIV ID='Tx4'>00:00 PM</DIV></TD> </TABLE> </BODY> Demo_TimerCount.zip
kali Posted May 23, 2016 Author Posted May 23, 2016 1 hour ago, jaclaz said: Try asking what you *need* or would like to obtain Thanks jaclaz. I need just a batch command for a stop watch with h:m:s or h:m:s:ms (hour:Minutes:Seconds: millisecond) format like the picture attached.
Mcinwwl Posted May 23, 2016 Posted May 23, 2016 For what you need this? I'm asking because we don't know whether or why it must be written as a batch/cmd, maybe a script in other language or command line executable will be a better choice.
kali Posted May 24, 2016 Author Posted May 24, 2016 5 hours ago, Mcinwwl said: For what you need this? I'm asking because we don't know whether or why it must be written as a batch/cmd, maybe a script in other language or command line executable will be a better choice. I badly need it to count time during software installation. I've made some silent softwares with batch command. I need to check how much time do these softwares take to be installed.
jaclaz Posted May 24, 2016 Posted May 24, 2016 So, you don't actually want a "stopwatch", you want a "timer" instead. This latter can easily be made in batch. The following is slightly obfuscated (so that readers may have an occasion to exercise their little gray cells): @ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION REM Time format expected HH.MM.SS.hs 24h format SET N=batch Program by jaclaz for Start Time and End Time of a Program execution CALL :SET ECHO 1,6,11,3 CALL SET Var=42 %§%%P%=%* CLS IF NOT DEFINED PROGRAM ( ECHO %N% ECHO Parameter is Missing ECHO Usage: %~nx0 ^<executable^> SET PROGRAM=PING -n 6 127.0.0.1 ECHO !PROGRAM! ECHO. ) %§%%S%=!%T%! %?% %P% %S% !%S%! %_9% :M %S% %S% /W "" !%P%! %§%N=!%T%! %?% %P% %E% %N% %_9% :M N %§%/A D=%N%-!%S%! %?%. %§%/A F=%D%/%.%&%§%F=0!F! %§%/A A=(%D%-%F%*%.%)/%...%&%§%A=0!A! %§%/A J=(%D%-%F%*%.%-%A%*%...%)/%..%&%§%J=0!J! %§%/A H=(%D%-%F%*%.%-%A%*%...%-%J%*%..%)&%§%H=0!H! %?%. ECHO %P% %Y% %F:~-2%.%A:~-2%.%J:~-2%,%H:~-2% GOTO :EOF :M N O P Q SET /A %1=(1!%1:~0,2!-%..%)*%.%+(1!%1:~3,2!-%..%)*%...%+(1!%1:~6,2!-%..%)*%..%+1!%1:~9,2!-%..% GOTO :EOF :SET Answer=42 SET §=%0 &&SET §=!§:~1!&!§!#=%2-%4&!§!?=%1&!§!_9=%6 FOR /F "tokens=%#% delims= " %%N IN ('%?% %N%') DO ( %§%P=%%O&%§%!§:~0,1!=%%S&%§%!§:~1,1!=%%V&%§%!§:~2,1!=%%T&%§%Y=%%W %§%..=%200&%§%...=%3000&%§%.=%5%30000 ) jaclaz
kali Posted May 24, 2016 Author Posted May 24, 2016 1 hour ago, jaclaz said: So, you don't actually want a "stopwatch", you want a "timer" instead. This latter can easily be made in batch. The following is slightly obfuscated (so that readers may have an occasion to exercise their little gray cells): @ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION REM Time format expected HH.MM.SS.hs 24h format SET N=batch Program by jaclaz for Start Time and End Time of a Program execution CALL :SET ECHO 1,6,11,3 CALL SET Var=42 %§%%P%=%* CLS IF NOT DEFINED PROGRAM ( ECHO %N% ECHO Parameter is Missing ECHO Usage: %~nx0 ^<executable^> SET PROGRAM=PING -n 6 127.0.0.1 ECHO !PROGRAM! ECHO. ) %§%%S%=!%T%! %?% %P% %S% !%S%! %_9% :M %S% %S% /W "" !%P%! %§%N=!%T%! %?% %P% %E% %N% %_9% :M N %§%/A D=%N%-!%S%! %?%. %§%/A F=%D%/%.%&%§%F=0!F! %§%/A A=(%D%-%F%*%.%)/%...%&%§%A=0!A! %§%/A J=(%D%-%F%*%.%-%A%*%...%)/%..%&%§%J=0!J! %§%/A H=(%D%-%F%*%.%-%A%*%...%-%J%*%..%)&%§%H=0!H! %?%. ECHO %P% %Y% %F:~-2%.%A:~-2%.%J:~-2%,%H:~-2% GOTO :EOF :M N O P Q SET /A %1=(1!%1:~0,2!-%..%)*%.%+(1!%1:~3,2!-%..%)*%...%+(1!%1:~6,2!-%..%)*%..%+1!%1:~9,2!-%..% GOTO :EOF :SET Answer=42 SET §=%0 &&SET §=!§:~1!&!§!#=%2-%4&!§!?=%1&!§!_9=%6 FOR /F "tokens=%#% delims= " %%N IN ('%?% %N%') DO ( %§%P=%%O&%§%!§:~0,1!=%%S&%§%!§:~1,1!=%%V&%§%!§:~2,1!=%%T&%§%Y=%%W %§%..=%200&%§%...=%3000&%§%.=%5%30000 ) jaclaz Very complicated script. I tried it. I think it's not that I want. I've attached a screenshot. For example I'm installing office. During installing it'll show the timer massage. So I can monitor how much time (Minute or second) is going on for the installation.
jaclaz Posted May 24, 2016 Posted May 24, 2016 5 minutes ago, kali said: I tried it. I think it's not that I want. Well, here, once saved the posted batch as mytymer.cmd, when run in a command prompt it shows: batch Program by jaclaz for Start Time and End Time of a Program execution Parameter is Missing Usage: mytimer.cmd <executable> PING -n 6 127.0.0.1 Program Start 18.32.43,74 Program End 18.32.49,05 Program Time 00.00.05,31 How different is what happens there? jaclaz
gunsmokingman Posted May 24, 2016 Posted May 24, 2016 Perhaps a more simple solution would be to use a VBS script to install. I say this because VBS has a built in Timer Function, below is an example using 3 common MS apps in an Array. It will then process the time it takes you to close each app and report it with a 10 second self closing Popup messagebox. '-> Runtime Object Dim Act :Set Act = CreateObject("Wscript.Shell") '-> Array To Hold Apps Dim App :App = Array("Notepad.exe","mspaint.exe", "cmd.exe") '-> Runtime Varibles Dim Tm1, Tm2, Tm3, Tm4, i '-> Threw Each App In Tha Array For Each i In App '-> Start Time Tm1 = Timer Act.Run(i),1,True '-> End Time Tm2 = Timer '-> Results Tm3 = Tm2 - Tm1 '-> For The Popup Report Tm4 = Tm4 & Round(Tm3,2) & " seconds " & i & vbCrLf Next '-> Show The Results Act.Popup Tm4,10,"Results",4128 Rename DemoTimer.vbs.txt to DemoTimer.vbs to make active DemoTimer.vbs.txt
Mcinwwl Posted May 24, 2016 Posted May 24, 2016 If it doesn't have to be a batch (and it seems it doesn't), you might try using Powershell. If I have understood you correctly, you need to have time how much the installation takes? Measure-Command {Start-Process "C:yourpath" -wait} Seems much easier to me. Path to your file instead of C:\yourpath of course. Maybe output is not as fancy and you need to keep PS opened, but works. . This one was done fast just because I like sleeping, but if you like my way I might polish that tomorrow Hope I helped
submix8c Posted May 24, 2016 Posted May 24, 2016 Ummm. It *looks* like the OP wants it to "calculate *beforehand*. See the Picture posted (still haven't got the hang of re-linking). Seriously, you need to TIME it (roughly) FIRST (using given methods above) and THEN insert your "message". Be aware that even MS did a "rough guess" and on a FAST computer, XP installs WAY FASTER than MS original perceived.
kali Posted May 25, 2016 Author Posted May 25, 2016 9 hours ago, gunsmokingman said: Perhaps a more simple solution would be to use a VBS script to install. I say this because VBS has a built in Timer Function, below is an example using 3 common MS apps in an Array. It will then process the time it takes you to close each app and report it with a 10 second self closing Popup messagebox. '-> Runtime Object Dim Act :Set Act = CreateObject("Wscript.Shell") '-> Array To Hold Apps Dim App :App = Array("Notepad.exe","mspaint.exe", "cmd.exe") '-> Runtime Varibles Dim Tm1, Tm2, Tm3, Tm4, i '-> Threw Each App In Tha Array For Each i In App '-> Start Time Tm1 = Timer Act.Run(i),1,True '-> End Time Tm2 = Timer '-> Results Tm3 = Tm2 - Tm1 '-> For The Popup Report Tm4 = Tm4 & Round(Tm3,2) & " seconds " & i & vbCrLf Next '-> Show The Results Act.Popup Tm4,10,"Results",4128 Rename DemoTimer.vbs.txt to DemoTimer.vbs to make active DemoTimer.vbs.txt 10 hours ago, jaclaz said: Well, here, once saved the posted batch as mytymer.cmd, when run in a command prompt it shows: batch Program by jaclaz for Start Time and End Time of a Program execution Parameter is Missing Usage: mytimer.cmd <executable> PING -n 6 127.0.0.1 Program Start 18.32.43,74 Program End 18.32.49,05 Program Time 00.00.05,31 How different is what happens there? jaclaz I can use timer for calculate the time (Code below). But It's not that what I exactly want. Spoiler @echo off setlocal enabledelayedexpansion cls & title Stopwatch pushd %~dp0 for /f "tokens=1-7 delims=/-:. " %%A in ("%time%") do set /a "x=%%A%%B%%C%%D" echo. Please Wait.. echo. Installing MS Office 2010. setup.exe for /f "tokens=1-7 delims=/-:. " %%A in ("%time%") do set /a "y=%%A%%B%%C%%D" set /a z=y-x :: split string & add decimal in echo set z1=%z:~0,-2% set z2=%z:~-2,2% cls echo. echo. echo. echo. It took %z1%.%z2% seconds to be installed. echo. & pause
jaclaz Posted May 25, 2016 Posted May 25, 2016 @submix8c Anyway the getTimeInAdvanceFromCrystalBall function is not available in the API unless user is "Magician" . Just in case the needed XKCD:https://xkcd.com/612/ @Kali Hmmm, the batch you posted seemingly mixes liberally HH , MM, SS and hs, you will have a number of incorrect results in many cases. And - if I get this right now - what you want is that the thingy shows time elapsed since start of the program updating it in "real time", i.e. something more like a "progress bar". This could be done in batch of course, BUT it will hog the CPU. AND you will have the whole display flashing, as the only simple way to update a command window from batch is to clear screen and re-output the new info. BUT there is a neat trick using SET /P and the backspace character (0x08) see:http://stackoverflow.com/questions/368041/how-to-code-a-spinner-for-waiting-processes-in-a-batch-file so that we can have *something* like: Spoiler @ECHO OFF SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET Before=%Time% CALL :to_hs Before ECHO Time is now %Time% ECHO Starting program ... start /min cmd.exe /c wait.exe 10>nul :loop SET Now=%Time% CALL :to_hs Now SET /A DeltahsVal=%NOWhsval%-%BEFOREhsVal% CALL :to_HHMMShs Delta ::There are 11 0x08 characters in the line below between = and Elapsed <nul (set /p output=Elapsed %Delta%) ::This updates the display roughly every second ping -n 1 127.0.0.1>nul TASKLIST |FIND /I "wait.exe">nul&&GOTO :loop ECHO Time is now %Time% ECHO Finished in %Delta% GOTO :EOF :to_hs SET %1HH=1!%1:~0,2! SET %1MM=1!%1:~3,2! SET %1SS=1!%1:~6,2! SET %1hs=1!%1:~9,2! SET /A %1hsVal=(!%1HH!-100)*360000+(!%1MM!-100)*6000+(!%1SS!-100)*100+(!%1SS!-100) GOTO :EOF :to_HHMMShs SET /A %1HH=!%1hsVal!/360000 SET /A %1MM=(!%1hsVal!-!%1HH!*360000)/6000 SET /A %1SS=(!%1hsVal!-!%1HH!*360000-!%1MM!*6000)/100 SET %1hs=0!%1hsVal:~-2! SET %1HH=0!%1HH! SET %1MM=0!%1MM! SET %1SS=0!%1SS! SET %1=!%1HH:~-2!.!%1MM:~-2!.!%1SS:~-2!,!%1hs:~-2! For the example above I used the Wait.exe available here:http://www.softtreetech.com/24x7/archive/58.htm jaclaz 1
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now