0

Consider this:

cd ..

The output of the above command is:

C:\Users\rodio>cd ..
                         <--- Get rid of this empty line.
C:\Users>

What I want to achieve, however, is:

C:\Users\rodio>cd ..
C:\Users>

Is it possible in a batch file (.bat, .cmd)?

coderodde
  • 1,269
  • 4
  • 17
  • 34
  • No. The empty line is produced by _the executed command_. Try: `set /P "=Hello" < NUL` – Aacini May 13 '23 at 05:47
  • More specifically, the command output (usually) ends with a `CRLF`. Then the Prompt starts with another `CRLF` (to be sure it's on a new line in case the previous command/script does *not* end with a `CRLF` (like @Aacini's code line) (A. check the output of a simple script with `echo on` vs. `echo off`. You can see, that the prompt adds an additional linefeed) (B. enter `echo off` at the command line and retry your commands. You see that the additional linefeed is missing now - together with the prompt) – Stephan May 13 '23 at 07:09
  • What is your real goal? Perhaps, you want to write a batch file, the output there could be optimized the way you're asking for – jeb May 13 '23 at 07:58
  • @jeb Yes, it will go to a batch file. – coderodde May 13 '23 at 08:00
  • Then you should show some real examples from your batch code, as `cd ..` doesn't ouput anything. Probably this solves your not asked question [echo without new line](https://stackoverflow.com/questions/7105433/windows-batch-echo-without-new-line/7105690#7105690) – jeb May 13 '23 at 08:07
  • @jeb I already have seen the post you linked. Close. All I need is the same for the `cd` command. – coderodde May 13 '23 at 08:20
  • 1
    As said, the `cd ` has no output at all, so there is no linefeed that could be removed. But without showing your real problem (you only showed unrelated code) it's hard to solve – jeb May 13 '23 at 09:21

2 Answers2

1

Perhaps this is an X-Y problem, and the need to remove the newline is caused by your solution to an unstated requirement? The behaviour is with cmd.exe rather than the cd command specifically.

In a batch file you can block all command echo and prompt output with @echo off. So for example:

@echo off
echo Start
cd ..
echo End

Outputs:

Start
End

If you need to report the current working directory, then that is output by cd with no parameter: e.g:

@echo off
echo Start
cd ..
cd
echo End

executed starting from C:\Users\<username> outputs:

Start
C:\Users
End
Clifford
  • 88,407
  • 13
  • 85
  • 165
0

Only guessing your real question...

@echo off
echo The current path is %CD% and here is no line feed

or for other commands without a shadow variable

@echo off
REM *** Get the output of a command
FOR /F "tokens=* %%A in ('ver') do set "verion=%%A"
echo The verion "%version%" was found
jeb
  • 78,592
  • 17
  • 171
  • 225