How to center batch files when they open up?

05
2014-04
  • Kevin

    How do I center batch files when they open up? I mean not by dragging but every time they open up they are automatically centered like is there a command? I am trying to make a game like that. And these are the game files I want to be centered. They're separate:

    @echo off
    type c1.txt
    gotoxy 0 1
    ping localhost -n 2 > Nul
    start c2.bat
    exit
    
  • Answers
  • Moses

    The easy way is simply modify the screen width of the Dos Box.

    To set it up in a batch file:

    mode con: cols=170 lines=50


    FYI, I don't know how to "lock that. It will change the resolution during the Batch File run, however next time you open a command prompt, it reverts to the old settings. At least the batch File runs as you wish.

    To determine best size for what your doing:

    • open cmd
    • Right click upper rail of CMD Window, choose Properties
    • Click "Layout" -Middle Choice=Window Size
    • Change that to
    • Width: 150
    • Height: 70

      • "Window Position: Check "Let System Position Window"

    For your resolution, might be different (no clue how to make it user specific)


    170x50 = Absolutely perfect window size for me. I can still see top url and bottom tabs, but takes up almost entire screen.

    As an F.Y.I.

    As I type this it's 2013, thus "Popular" is the Wide LCD/Short/Squat Height.

    My Resolution is set at 1400x1050 It's rated "20-Inch" It's 21"-across/12"-tall (see, wide/squat)
    (might make a difference, consider tweaking it)

  • Matthieu Cartier

    You can do this with IE popups using VBS, but as far as I am aware that cannot be done to manipulate an existing command prompt. You may want to try using an AutoHotkey script within the batch file to do something like (A_ScreenWidth/2)-(Width/2), or use CenterWindow() in VC.

  • Linker3000

    In the days of DOS, you used to be able to manipulate the screen output by loading up the ANSI.SYS driver and using ESCape sequences or by creating your text/images using an app like thedraw.

    Checking for a modern solution for ANSI.SYS I came across ANSICON. With this app/utility installed, putting ANSI escape sequences in your txt file may work - I can't try it at the moment as I only have a Linux laptop at my disposal so feedback would be useful!

    Edit: Thinking about things, I don't believe this will make centring text any easier as I cannot find an ANSI sequence that defines 'centre text' on a display - only for a printer - but this may be of use for designing screens so I will leave the answer here as it is.


  • Related Question

    Exit batch file from subroutine
  • Questioner

    How can I exit a batch file from inside a subroutine?

    If I use the EXIT command, I simply return to the line where I called the subroutine, and execution continues.

    Here's an example:

    @echo off
    ECHO Quitting...
    CALL :QUIT
    ECHO Still here!
    GOTO END
    
    :QUIT
    EXIT /B 1
    
    :END
    EXIT /B 0
    

    Output:

    Quitting...
    Still here!
    


    Update:

    This isn't a proper answer, but I ended up doing something along the lines of:

    @echo off
    CALL :SUBROUTINE_WITH_ERROR || GOTO HANDLE_FAIL
    ECHO You shouldn't see this!
    GOTO END
    
    :SUBROUTINE_WITH_ERROR
    ECHO Simulating failure...
    EXIT /B 1
    
    :HANDLE_FAIL
    ECHO FAILURE!
    EXIT /B 1
    
    :END
    ECHO NORMAL EXIT!
    EXIT /B 0
    

    The double-pipe statement of:

    CALL :SUBROUTINE_WITH_ERROR || GOTO HANDLE_FAIL
    

    is shorthand for:

    CALL :SUBROUTINE_WITH_ERROR 
    IF ERRORLEVEL 1 GOTO HANDLE_FAIL
    

    I would still love to know if there's a way to exit directly from a subroutine rather than having to make the CALLER handle the situation, but this at least gets the job done.


    Update #2: When calling a subroutine from within another subroutine, called in the manner above, I call from within subroutines thusly:

    CALL :SUBROUTINE_WITH_ERROR || EXIT /B 1
    

    This way, the error propagates back up to the "main", so to speak. The main part of the batch can then handle the error with the error handler GOTO :FAILURE


  • Related Answers
  • JMD

    Add this to the top of your batch file:

    @ECHO OFF
    SETLOCAL
    
    IF "%selfWrapped%"=="" (
      REM this is necessary so that we can use "exit" to terminate the batch file,
      REM and all subroutines, but not the original cmd.exe
      SET selfWrapped=true
      %ComSpec% /s /c ""%~0" %*"
      GOTO :EOF
    )
    

    Then you can simply call:

    • EXIT [errorLevel] if you want to exit the entire file
    • EXIT /B [errorLevel] to exit the current subroutine
    • GOTO :EOF to exit the current subroutine
  • harrymc

    If you do not want to come back from the procedure, don't use call: instead use goto.

    @echo off
    ECHO Quitting...
    GOTO :QUIT
    ECHO Will never be there!
    GOTO END
    
    :QUIT
    EXIT /B 1
    
    :END
    EXIT /B 0