Jump to content

Recommended Posts

if, for whatever reason, you want to shutdown your pc after a certain amount of time but dont want/can do that manually, i have written a little batch file for that

@echo off
::set default of variables
set "hours=0"
set "minutes=0"
set "seconds=0"

::Input stuff
set /p hours=Enter Hours: 
set /p minutes=Enter Minutes: 
set /p seconds=Enter Seconds: 

::convert hours and minutes to seconds and add them all together
::use shutdown command with entered time converted into seconds as /t argument
set /a shutdownArgument=%hours%*3600+%minutes%*60+%seconds%
shutdown /s /t %shutdownArgument%
echo Computer will shutdown in %hours%:%minutes%:%seconds% (%shutdownArgument%)
set /p p=Hit Enter to continue...
exit

How to use:

  • create  a empty .txt file
  • paste code above into said .txt file
  • save and close file
  • rename .txt file into .bat (if you dont see the file name extention you'll have to turn it on, google is your friend)
  • double klick on the file and be asked to enter hours, minutes and seconds. Computer will shutdown after entered time has passed

alternatively you can just open the CMD and enter "shutdown/s /t [TIME IN SECONDS]" but you'll have to convert hours, minutes and seconds manually, which is why i wrote this thing cuz im lazy

 

!!WARNING!!

i didnt include a check if you have entered a number or not. not too sure what would happen if you entered a character instead of a number

  • Like 2
  • Upvote 2

Share this post


Link to post
Share on other sites

Nice batch scripting there!  *(yes, the bbcode spoiler tag does not work here... have editor button in the works, but need more experience in CSS, and working with IPS to add button to editor)

Re.  Check for numeric value, do what the pro's do... StackOverflow.com FTW - grab first answer, modify as needed:

https://stackoverflow.com/questions/17584282/how-to-check-if-a-parameter-or-variable-is-numeric-in-windows-batch-file

SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%i
if defined var (echo %1 NOT numeric) else (echo %1 numeric)
Quote

Replace %1 with %yourvarname% as appropriate

:drinks:

 

  • Upvote 2

Share this post


Link to post
Share on other sites
1 hour ago, =VG= l3RY4N said:

alternatively you can just open the CMD and enter "shtudown /s /t [TIME IN SECONDS]" but youll have to convert hours, minutes and seconds manually, which is why i wrote this thing cuz im lazy

if you want to do this via a remote connection you have to add an "/f" (for force) after "shutdown" (at least on win7) and/or "/r" if you want to restart your computer.

Quote

shutdown /f /r

if you are in the 1 minute countdown to shutdown and you want do abort it you can open the CMD and type "shutdown /a"

 

 

  • Upvote 1

Share this post


Link to post
Share on other sites

did revise it with semlers solution. kinda ugly but it doesnt allow characters.

@echo off
::set default of variables
set "hours=0"
set "minutes=0"
set "seconds=0"

::Input stuff
:inputH
set /p hours=Enter Hours: 
call :checkIfNumber %hours% inputH
:inputM
set /p minutes=Enter Minutes: 
call :checkIfNumber %minutes% inputM
:inputS
set /p seconds=Enter Seconds: 
call :checkIfNumber %seconds% inputS

::convert hours and minutes to seconds and add them all together
::use shutdown command with entered time converted into seconds as /t argument
set /a shutdownArgument=%hours%*3600+%minutes%*60+%seconds%
shutdown /s /t %shutdownArgument%
echo Computer will shutdown in %hours%:%minutes%:%seconds% (%shutdownArgument%)
set /p p=Hit Enter to continue...
exit

::The following checks if 1st argument is a number, 2nd argument is the label to jump to if args[1] isnt a number
::If it is a number exit subroutine and continue
:checkIfNumber
SET "var="&for /f "delims=0123456789" %%i in ("%1") do set var=%%i
if defined var (echo Please enter a number! & goto %2)
exit /b

BUT it has problems with code injection. (eg. if you enter a "&" it will recognize it as code not as a symbol in the variable, used the same principle for the jumps from the test function to the individual inputs)

Edited by =VG= SemlerPDX
disabled syntax highlighting (no batch highlighting available in options)
  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, =VG= l3RY4N said:

BUT it has problems with code injection. (eg. if you enter a "&" it will recognize it as code not as a symbol in the variable, used the same principle for the jumps from the test function to the individual inputs)

Oh, yea, that's actually specifically for discerning A-Za-z from 0-9  -- per notes in that thread at StackOverflow, entering symbols or something like 0ABC  (staring with zero) also would not be handled correctly.

Thing is, with small user utilities, there is no real need to catch every exception, but when you do want to, you go for function over looks every time.  If anyone uses this knowing what it is, and enters anything but a number when prompted to do so, deserves whatever errors or chaos ensues forth.

  • Upvote 1

Share this post


Link to post
Share on other sites
6 minutes ago, =VG= SemlerPDX said:

Thing is, with small user utilities, there is no real need to catch every exception, but when you do want to, you go for function over looks every time.  If anyone uses this knowing what it is, and enters anything but a number when prompted to do so, deserves whatever errors or chaos ensues forth

thats the reason i didnt bother with it in the first place. But if a solution is presented on a sliver platter i'm not gonna say no.

  • Upvote 1

Share this post


Link to post
Share on other sites

I used to use GOTO a bit in batch -- so you do your comparison and if it's not numeric (checking for symbols, too), jump back up to the 'get input' section

Share this post


Link to post
Share on other sites

Funny thing, I actually just had to use a GoTo in a VB.net function I've been working on for my AVCS4 thing ... part of the new systems that let users create new voice commands using voice commands...

I have to check if something is not right in geek town, and GoTo a section to handle the exception if so... thinking of any possible ways something can break, or any other ways it can receive data is the best way to make sure shit works right the first time, every time.  Trial and error, testing and more testing... when you're chicken plucking your way along the self-taught route like I do, it takes years to get any sense of confidence

 

 

spoilerIMG2415905122018.PNG

Yea... cool spoiler tag thingy will look and work like this whenever I get it integrated into the website proper - this is me doing website admin stuff to put it in here, fysa

 

Public Class VAInline
  dim VPBexe as integer
  dim VPBindex as integer
  dim VASnum as string
  dim commandCheck as string
  dim commandBase as string
  dim commandFull as string
  dim commandArray() as string
  dim entry() as string
  
  Public Sub Main()
    
    VPBexe = 0
    
    if VA.GetText("~avcs_vpb_command_array") IsNot nothing
      'Get voice command actions array and eval for actions'
      commandArray = VA.GetText("~avcs_vpb_command_array").Split(",")
      
      'Handle voice command that only executes another voice command'
      VPBarray:
      if(VPBexe > 0)
        commandCheck = "AVCS_VPB_COMMAND_" + VPBexe.ToString()
        if (VA.GetText(commandCheck)) IsNot nothing
          commandArray = nothing
          commandBase = VA.GetText(commandCheck)
          commandFull = "AVCS_VPB_COMMAND_" + commandBase
          if (VA.GetText(commandFull)) IsNot nothing
            commandArray = VA.GetText(commandFull).Split(",")
          end if
        end if
      end if
      
      if(commandArray) IsNot nothing
        for each commFunc as string in commandArray
          if not(commFunc = "")
            'Check if this command only calls other commands'
            if(commFunc.StartsWith("VPB#"))
              entry = commFunc.Split("#")
              VPBindex = Convert.ToInt32(entry(1))
            else if(commFunc.StartsWith("COM#"))
              entry = commFunc.Split("#")
              VPBexe = Convert.ToInt32(entry(1))
              if(not(VPBindex = VPBexe))
                GoTo VPBarray
              end if
            'Get the Keypress Action for this command'
            else if(commFunc.StartsWith("VPBKEY"))
              entry = commFunc.Split("/")
              VA.SetText("~VPBKEY", entry(1))
            'Get additional options for this command (if any)'
            else if(commFunc.Contains("PTTON"))
              VA.SetBoolean("~PTTON", true)
            else if(commFunc.Contains("AFMON"))
              VA.SetBoolean("~AFMON", true)
            else if(commFunc.Contains("VAASC"))
              VA.SetBoolean("~VAASC", true)
              VA.SetBoolean("~VASON", true)
            else if(commFunc.StartsWith("VAS"))
              VASnum = commFunc.Substring(3,1)
              VA.SetInt("~VAS", VASnum)
              VA.SetBoolean("~VASON", true)
            else if(commFunc.Contains("SFSON"))
              VA.SetBoolean("~SFSON", true)
            else if(commFunc.Contains("SFSYES"))
              VA.SetBoolean("~SFSYES", true)
            else if(commFunc.Contains("SFStrue"))
              VA.SetBoolean("~SFS", true)
            else if(commFunc.Contains("SFSfalse"))
              VA.SetBoolean("~SFS", false)
            end if
          end if
        next
      end if
    
    end if
  End Sub
End Class

 


↑ basically, example of me checking at every step if something is not right, if something is nothing, if something is > 0 (hence, not nothing)... ya gotta check everything if ya don't want headaches later, scratching your head until it bleeds trying to figure out why something didn't work right, or as expected

Share this post


Link to post
Share on other sites
34 minutes ago, =VG= SemlerPDX said:

so you do your comparison and if it's not numeric (checking for symbols, too), jump back up to the 'get input' section

jup. put a tag for each variable (hour, minute, second) to jump to forcing the user to keep entering stuff until he enters a valid number or exits the program.
I put the tag where it should jumpt to in the 2nd argument. 

  • Like 1

Share this post


Link to post
Share on other sites
24 minutes ago, =VG= SemlerPDX said:

basically, example of me checking at every step if something is not right

which program are you using to code?

Share this post


Link to post
Share on other sites

Normally for VB.net/C# I use VisualStudio, but this is an in-line function inside the VoiceAttack program itself, which makes use of a bunch of embedded proxy functions provided by VoiceAttack.  That particular function is getting and setting variables for the VoiceAttack program to use in other functions and/or voice commands (much faster than VoiceAttack could do so with it's  code-block style actions).  The code above is VB.net, which is a C style language


edit:  Use LUA for batch highlighting, it's closest, doesn't break, and provides some basic highlighting -- I already edited your post

Edited by =VG= SemlerPDX

Share this post


Link to post
Share on other sites

does it load settings from a file or does it just make sure everything is properly set and the values needed are already loaded by the main program?

asking out of curiosity

scratch that. i'm blind as fuck, but what i dont get is this here

10 hours ago, =VG= SemlerPDX said:

if(commFunc.StartsWith("VPB#"))
  entry = commFunc.Split("#")
  VPBindex = Convert.ToInt32(entry(1))
else if(commFunc.StartsWith("COM#"))
  entry = commFunc.Split("#")
  VPBexe = Convert.ToInt32(entry(1))
  if(not(VPBindex = VPBexe))
  	GoTo VPBarray
  end if

 

if it starts with VPB... set entry and VPBindex variables but if it doesnt check if it starts with COM. if true then set entry and VPBexe then ask if not VPBindex (which in this case was never initialized) equal to VPBexe. when true go back to label and do that code from there again

where was VPBindex initialized in the second case?

Share this post


Link to post
Share on other sites
2 hours ago, =VG= l3RY4N said:

does it load settings from a file or does it just make sure everything is properly set and the values needed are already loaded by the main program?

asking out of curiosity

scratch that. i'm blind as fuck, but what i dont get is this here

if it starts with VPB... set entry and VPBindex variables but if it doesnt check if it starts with COM. if true then set entry and VPBexe then ask if not VPBindex (which in this case was never initialized) equal to VPBexe. when true go back to label and do that code from there again

where was VPBindex initialized in the second case?

VPB# always appears before COM# in the commandArray... First time through, it sets the VPBindex - does not disappear before iterating to next 'commFunc' in the commadArray of data - second time through it finds the COM# and sets VPBexe, compares them, if not equal, GoTo the above special function handler.

VPBindex is always initialized before it gets to any 'commFunc' containing COM#, and the exception handling that VPBindex is not set is well enough if VPBexe doesn't equal it.... One is the index number of the command, the other is the command index (if different, the action is to find the command data from a different index numbered command)

Sometimes, it is needed for the action of one command to simply be calling another command.  It is a very edge case, but needed to factor in.

AVCS_VPB_DATA_COMMAND_1=VPB#1,COM#1,VPBKEY/e
AVCS_VPB_COMMAND_1=[Engage;Begin;Turn On;Start] [Hyperspace;Frame Shift;Warp] [Drive;Engines]
↑ that could be VPB index 1, command 1 -- it presses the 'e' key when recognized

AVCS_VPB_DATA_COMMAND_2=VPB#2,COM#1
AVCS_VPB_COMMAND_2=punch it chewy
↑ that could be some other command like a single alternate phrase, or wildcard with no special dynamics, or etc., and it doesn't do anything except execute COM#1

That's the reason for that GoTo, and why I need to handle so many exceptions on that one.

 

*edit: Also, there are functions not shown from elsewhere in the program, that's not a one-off.... this is a bit from inside another command, again, parts not show, but this is where data is extracted from that bit above into all the various phrases, so any one of the combinations is equal to the array of data for simple retrieval
(following the above example, one would be: AVCS_VPB_COMMAND_start warp drive=VBP#1,COM#1,VPBKEY/e   )     ←this is the stuff that gets loaded into RAM, the ones above are in a save file

spoilerIMG2415905122018.PNG
        phrases = (VA.GetText("AVCS_VPB_COMMAND_" + cmdIndex.ToString()))
        phraseData = (VA.GetText("AVCS_VPB_DATA_COMMAND_" + cmdIndex.ToString()))
        phraseArray = vaProxy.Utility.ExtractPhrases(phrases, true, true)
        for each phrase in phraseArray
          if ((not(phrase = "")) and (not(phrasesFinal.Contains(phrase))))
            phrasesFinal.Add(phrase)
            phrasesUnique = phrasesUnique + phrase + ";"
            VA.SetText("AVCS_VPB_COMMAND_" + phrase, phraseData)
          end if
        next

 

Edited by =VG= SemlerPDX
edit at *

Share this post


Link to post
Share on other sites
7 hours ago, =VG= SemlerPDX said:

First time through

I'm blind. i just noticed the for-each.

 

So.. you need to save what input/command executes what command?

Did i get that right?

Sorry haven't been doing much programming lately

  • Upvote 1

Share this post


Link to post
Share on other sites
3 hours ago, =VG= l3RY4N said:

So.. you need to save what input/command executes what command?

Pretty much.  Like I said, there are plenty of functions elsewhere that do tons of stuff to make this all work.  The goal for me is optimization - I don't mind the initial program load time being a little longer if it means when someone actually says something that gets recognized, that it enacts the actions (and/or keypress(es)) within a tenth of a second.  Issues I had in the past included accurate recognition, but a good 1 to 2 seconds before anything happened, which is far too long for interactivity in video games of most any kind.

This new system lets me set some variables to RAM after loading from the save file, and those speed everything up... there are a lot of them, 'Open [the;] Cargo Bay [Door;Doors]' for example has 4 different ways to say that phrase, so each one gets parsed out to be a variable set to RAM that contains that command's data array (the VPB#,COM#, keys (if any), and options list)

On file, it's just 2 variables for each command, but once loaded by the program, all get expanded into their multiple phrase variations, ending up with easy to reach data just based on what the user said,
AVCS_VPB_COMMAND_open cargo bay door=VPB#1,COM#1,VPBKEY/e
AVCS_VPB_COMMAND_open cargo bay doors=VPB#1,COM#1,VPBKEY/e
AVCS_VPB_COMMAND_open the cargo bay door=VPB#1,COM#1,VPBKEY/e
AVCS_VPB_COMMAND_open the cargo bay doors=VPB#1,COM#1,VPBKEY/e

So, when they say something like, 'open cargo bay doors', it will toss the known variable name before it to get the data, "AVCS_VPB_COMMAND_{CMDSPOKEN}" which resolves to that command's data


FYSA, this is all WIP alpha build, still evolving even as we chat it up here, having fun talking about these things for sure... but will have more fun when I release it as a working program.  Tons of fun!!

Share this post


Link to post
Share on other sites

Here's something you might like - a batch script I wrote long ago when I needed to be able to restart VoiceAttack using a voice command -- worked with a windows scheduled task I created to launch the VoiceAttack program as administrator, but bypass that annoying UAC pop-up to allow it.

Found long ago that killing a task is no exact science, and need to be able to handle the case that it doesn't happen, and to try again until successful:
 

@echo off
title startVAwInput1.bat
REM by SemlerPDX Jan2019
REM VETERANS-GAMING.COM

:kill_VA
taskkill /f /im VoiceAttack /fi "windowtitle eq VoiceAttack.exe"
tasklist | find /i "*VoiceAttack*" >nul 2>&1
if errorlevel 1 (
  ping -n 3 127.0.0.1>nul
  schtasks /run /TN "VAnoUACwINPUT1"
  exit
) else (
  ping -n 2 127.0.0.1>nul
  goto kill_VA
)

↑ ping is the preferred "pause" or "sleep" method for Batch -- timeout is depreciated, where a ping such as that has a finite execution time, so increasing the number of times to ping effectively increases the 'pause' or 'sleep' time.  Ya pick these things up over the years, again, self taught amateur just poking around trying to make things that work, then trying to make them work well/better in time.

Share this post


Link to post
Share on other sites

I'd rather be called a geek than a nerd any day.  :dirol:

Edited by =VG= SemlerPDX
↑ a nerd would have left that unedited, with wording "I'd rather be a geek than a nerd..." open to interpretation as me being an asshole, as if calling Bludklot a nerd...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×