How to print a newline in an MS-DOS script?

63

Solution 1

echo. will produce a new line.

So your script should look something like this:

@ECHO OFF
cls
echo.
ruby foo.rb

Solution 2

Even if here are 4 answers with the same tip to use ECHO., it's not the best solution!

There are two drawbacks.

  1. It's slow
    ECHO. is ~10 times slower than a normal echo command, that's because ECHO. will force a disk access

  2. It can fail
    If a file exists with the name ECHO (without extension) then each ECHO. command will result in a file not found error, instead of echoing an empty line.

But what do you can do then?
For a simple empty line, there are many working ways.

echo+
echo=
echo;
echo,
echo:
echo(

But sometimes you want a secure way to echo the content of a variable even if the variable is empty or contain odd content like /? or \\..\..\windows\system32\calc.exe.

ECHO<character>%variable%

echo=/?
echo;/?
echo,/?
echo:\\..\..\windows\system32\calc.exe

Then the most commands will fail, only ECHO( works in any situation.
It looks a little bit strange, but it works and it does not need nor make any trouble with a closing bracket.

Solution 3

how about:

@echo off
cls
echo.
ruby foo.rb
echo.

bye

Solution 4

Try this

echo.

Solution 5

Use the echo command followed by a period to display a new line in an MS-DOS batch file:

echo.
Share:
63
Roland
Author by

Roland

Updated on October 21, 2020

Comments

  • Roland
    Roland over 3 years

    I'm trying to set the size of an array to an input received by the program, currently the size of the array is defined as a const static int.

    Board.h

    #include <iostream>
    using namespace std;
    
    class Board {
    private:
        const static int BOARDSIZE = 5;
        char board[BOARDSIZE][BOARDSIZE];
    
        const char p1Symbol = 'R';
        const char p2Symbol = 'B';
        const char trail = 'O';
        const char crash = '*';
    
        int p1Row;
        int p1Col;
        int p2Row;
        int p2Col;
    public:
        void setBoardSize(int size);
        bool isValidMove(int row, int col);
        bool isValidInput(char input);
        bool getNextMove();
        void initializeArray();
        void drawHorizontalSeparator();
    
        void drawSeparatedValues(int row);
        void displayBoard();
    };
    

    Main

    #include <iostream>
    #include "Board.h"
    
    using namespace std;
    
    int main() {
        int size;
        cout << "Enter and integer between 4 and 20 for the BoardSize: " << endl;
        cin >> size;
        Board b;
        b.setBoardSize(size);
        b.initializeArray();
        b.displayBoard();
    
        bool done = false;
    
        while (!done) {
            done = b.getNextMove();
        }
    
        return 0;
    }
    
    

    I'm trying to use this function called setBoardSize to change the size of the board in the Board.H file. I've tried removing const static but then I get all sorts of errors. Apparently it isn't possible to define an array that doesn't have a predefined size?

    • user4581301
      user4581301 over 4 years
      If you are not allowed to use standard library containers for this task, I recommend making that fact clear in the question. If you do not, you will receive answers using nothing but library containers because that's the most-sane way to do it.
    • Peter
      Peter over 4 years
      Use std::vector<int> (if you're happy to have one dimensional indexing) or a std::vector<std::vector<int> > if you insist on two dimensions. Either works, since std::vector is a (templated) resizable container of specified types, and can contain other resizable containers.
  • Bryan Locke
    Bryan Locke about 15 years
    You saved me from certain death. Thank you.
  • beltorak
    beltorak almost 13 years
    That last one does not echo a blank line. It echo's "on" or "off" depending on the state set by the first line. To get a blank line you have to echo. as others have pointed out.
  • npostavs
    npostavs over 11 years
    "ECHO. will force a disk access" really?
  • user4581301
    user4581301 over 4 years
    Good suggestion. A slight improvement is to wrap the vector in a data structure that makes it look 2D so you can't accidentally mess up the indexing in one spot without messing it up everywhere. Here's a good example example wrapper.