Windows equivalent "echo -n" no longer works in Win7

I had a great trick in the Windows cmd.exe(at least up to XP), to emulate the UNIX echo behavior without the new line echo -n. For example, the command:

<nul: set /p junk= xyzzy

exactly six characters will be output, the leading space and the string "xyzzy", and nothing more.

If you're wondering why this works, this is actually an input command that outputs " xyzzy", since the prompt is waiting for user input before assigning this input to a variable junk. In this particular case, it does not wait for user input, since it captures the input from the device nul.

This was quite useful in scenarios cmdwhen (for example) it processes files in a loop (one iteration per file), where you want to list more than one per line. Using this trick, you can simply print each file name followed by a space, not a new line, after that, after the loop ends, print a new line:

Processing files:
    file1.txt file42.txt p0rn.zip

Now I find that on Windows 7, spaces are no longer output, so I get:

Processing files:
file1.txtfile42.txtp0rn.zip

Is there a way to get set /pto start honoring my spaces again, or is there another way in Win7 to achieve the same effect?

I tried quotes using .(which works in echo) and even avoids the c line ^, but none of them work:

C:\Pax> <nul: set /p junk= xyzzy
xyzzy

C:\Pax> <nul: set /p junk=" xyzzy"
xyzzy

C:\Pax> <nul: set /p junk=' xyzzy'
' xyzzy'

C:\Pax> <nul: set /p junk=. xyzzy
. xyzzy

C:\Pax> <nul: set /p junk=^ xyzzy
xyzzy

What I need:

C:\Pax> some_magical_command_with_an_argument xyzzy
 xyzzy

which will give me a space at the beginning and without a new line at the end.

+5
4

set cmd - , cscript ( Windows), . cmd ( ) vbs.

cmd :

rem Create the VBS file to output spaces and a word.

echo.for i = 1 to WScript.Arguments.Item(0) >spacetext.vbs
echo.     WScript.StdOut.Write ^" ^" >>spacetext.vbs
echo.next >>spacetext.vbs
echo.WScript.StdOut.Write WScript.Arguments.Item(1) >>spacetext.vbs

rem Do this once per word you want output (eg, in a loop).

cscript /nologo spacetext.vbs 0 Hello,
cscript /nologo spacetext.vbs 1 my
cscript /nologo spacetext.vbs 1 name
cscript /nologo spacetext.vbs 1 is
cscript /nologo spacetext.vbs 4 Pax.

rem Do this at the end to add newline and kill temp file.

echo.
del spacetext.vbs

- , :

Hello, my name is    Pax.
0

paxdiablo, , JScript/ VBScript.

My script jEval.bat - JScript stdout, . script .

, jEval.bat , - PATH, - :

call jeval "' xyzzy'"

script. . , .

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::************ Documentation ***********
:::
:::jEval  JScriptExpression  [/N]
:::jEval  /?
:::
:::  Evaluates a JScript expression and writes the result to stdout.
:::
:::  A newline (CR/LF) is not appended to the result unless the /N
:::  option is used.
:::
:::  The JScript expression should be enclosed in double quotes.
:::
:::  JScript string literals within the expression should be enclosed
:::  in single quotes.
:::
:::  Example:
:::
:::    call jEval "'5/4 = ' + 5/4"
:::
:::  Output:
:::
:::    5/4 = 1.25
:::

::************ Batch portion ***********
@echo off

if "%~1" equ "" (
  call :err "Insufficient arguments"
  exit /b
)
if "%~2" neq "" if /i "%~2" neq "/N" (
  call :err "Invalid option"
  exit /b
)
if "%~1" equ "/?" (
  setlocal enableDelayedExpansion
  for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
    set "ln=%%A"
    echo(!ln:~3!
  )
  exit /b
)
cscript //E:JScript //nologo "%~f0" %*
exit /b

:err
>&2 echo ERROR: %~1. Use jeval /? to get help.
exit /b 1


************ JScript portion ***********/
if (WScript.Arguments.Named.Exists("n")) {
  WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
  WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}
+1

, : <space> Win7 set /p. Windows:

Syntax                |  XP                                 |  Vista and Windows 7
----------------------+-------------------------------------+-------------------------------------
<nul set /p =!msg!    |- If 1st char is quote, then trims   |- Trims leading white space chars.   
       or             |  that quote, and if at least one    |- If 1st non white space char is     
<nul set /p "=!msg!"  |  additional quote, than trims last  |  quote, then that quote is treated  
                      |  quote and all remaining chars.     |  as white space and trimmed, and if
                      |- If 1st non trimmed char is =, then |  at least one additional quote, then
                      |  syntax error.                      |  trims last quote and all remaining
                      |                                     |  chars.
                      |                                     |- If 1st non trimmed char is =, then
                      |                                     |  syntax error.
----------------------+-------------------------------------+-------------------------------------
<nul set /p ="!msg!"  |- Trims leading control chars and    |- Trims leading white space chars.
       or             |  spaces.                            |- If 1st non trimmed char is =, then 
<nul set /p "="!msg!""|- If 1st non trimmed char is =, then |  syntax error.
                      |  syntax error.                      |
----------------------+-------------------------------------+-------------------------------------

On Vista and Windows 7, the trimmed leading white space chars are:
    9  0x09  Horizontal Tab
   10  0x0A  New Line
   11  0x0B  Vertical Tab
   12  0x0C  Form Feed
   13  0x0D  Carriage Return
   32  0x20  Space
  255  0xFF  Non-breaking Space

.

0

?

@ECHO OFF
SETLOCAL
FOR /l %%i IN (1,1,4) DO <NUL SET /p var="item %%i "

:

item 1 item 2 item 3 item 4

?

0

All Articles