5

I need to add a single spacebar to a textfile ussing batch

however the following dosnt work

ECHO  >C:\txt.txt

this produces the text Echo is (off) instead???

Im ussing windows batch

LabRat
  • 1,996
  • 11
  • 56
  • 91

7 Answers7

6

echo is printing the text "ECHO if off" because you haven't provided any parameters to it. If you type echo /? for usage instructions you will see this behavior defined: "Type ECHO without parameters to display the current echo setting". So, in your case, echo is set to off.

If you want to have on the space character without a newline and carriage return you will most likely need to use set. If you are using Windows XP, this should be easy.

Windows XP

>txt.txt ( <nul set /p "= " ) 

If you are running Vista or Higher it keeps a little tricky because windows strips leading spaces on the set command.

Windows Vista or Higher

You will need to create a backspace character and then print that:

:: Create a backspace character
for /f %%A in ('"prompt $H &echo on &for %%B in (1) do rem"') do set BS=%%A

<nul set /p=%BS% > txt.txt

Note that this doesn't actually print a space, but a backspace character instead. I'm not quite sure if this will work for you, but I couldn't think of any easier way. Otherwise you are probably better off using some other scripting language, like python or powershell.

Nick Hartung
  • 418
  • 3
  • 13
  • +1 for XP but I think the backspace solution would actually write the backspace char follwed by a space to the file. The only other way I can think is the stupid `copy /b src.file + onewhitespacefile.txt src.file` – Alex K. Dec 20 '12 at 15:09
  • 3
    +1 Here is a full XP example: `>txt.txt ( – David Ruhmann Dec 20 '12 at 15:28
  • @DavidRuhmann using your example because it's more clear. I originally forgot to redirect it to a file as well. – Nick Hartung Dec 20 '12 at 16:14
  • I've posted a [solution for Vista and beyond](http://stackoverflow.com/a/19441398/1012053) – dbenham Oct 18 '13 at 04:07
2
  1. Echo a space (+ line break)

    ECHO. > C:\txt.txt
    

    Note that this will output a carriage return plus line break (an enter) as well. So your file will become 3 bytes.

  2. Alternatively, you may create the file with a 0-byte using

    fsutil file createnew c:\txt.txt 1
    

    Only this doesn't add a space, but a 0 character.

  3. Alternatively, you may create a file with a single space once, and copy that to txt.txt each time you need it to contain a space.

  4. Use an external tool, like echon.exe from this zipfile (from this site). It mimics echo -n, like it is available in Linux.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • I hate to be a pain but it needs to be without a line break (an enter). its because it writes a script file wich will become confused with the line break. – LabRat Dec 20 '12 at 14:23
  • Alternative method to write a 0 character without an enter. Unfortunately this character is not a real space, so I don't know if your script accepts it. Proves to be quite hard to do this properly. – GolezTrol Dec 20 '12 at 15:27
2

Not only does SET /P not print leading white space on Vista and beyond, it also cannot print leading =.

Below is code that works on all modern versions of Windows from XP onward:

@echo off
setlocal enableDelayedExpansion
for /f %%A in ('copy /Z "%~dpf0" nul') do set EOL=%%A^


:: The above two empty lines are critical - DO NOT REMOVE
<nul set /p "=x!EOL! " >temp.tmp
findstr /v $ temp.tmp >file.txt
del temp.tmp

The above technique can be extended to support printing nearly any string without newline, with a few exceptions:

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • +1 Intersting new technic, do you can get it working with a pipe to findstr? – jeb Oct 18 '13 at 05:35
  • 1
    I get a zero byte `temp.tmp` file and an error from findstr - where does `ln.tmp` come into the picture? – foxidrive Oct 18 '13 at 06:13
  • @foxidrive - Thanks, all fixed. It was an artifact of changing the file name after testing the code, and forgetting to change it in one place. – dbenham Oct 18 '13 at 11:02
  • 1
    @jeb - Unfortunately no, a pipe cannot work because FINDSTR appends `` to piped input that does not end with ``. – dbenham Oct 18 '13 at 11:04
2

To create a single space with Win Vista or above you can't use the SET/P command anymore (as dbenham mentioned).

But you can use the copy command

@echo off
setlocal EnableDelayedExpansion
call :createSub
call :echoWithoutLinefeed "=hello"
call :echoWithoutLinefeed " world"
exit /b

:echoWithoutLinefeed
> txt.tmp (echo(%~1!sub!)
copy txt.tmp /a txt2.tmp /b > nul
type txt2.tmp
del txt.tmp txt2.tmp
exit /b

:createSub
copy nul sub.tmp /a > nul
for /F %%a in (sub.tmp) DO (
   set "sub=%%a"
)
del sub.tmp
exit /b

First the text is outputs with a simple ECHO but at the end a SUB character is appended, just before the ECHO appends CR LF.

And with copy inputfile /a outputfile /b all characters are copied up to the first SUB character.

jeb
  • 78,592
  • 17
  • 171
  • 225
1

Here are two methods for 32 bit Windows:

This method creates a file directly with hex 20 (space):

@echo off
(for %%v in (E100''20 Nfile.tmp RCX 01 W Q) do @echo %%v)|debug >nul
ren file.tmp "file with a single space.txt"

This one removes the two trailing characters from the file.tmp file

@echo off
set f=file.tmp
echo. >%f%
(for %%v in (E100''83''E9''02 L103 P N%f% W103 Q) do echo %%v)|debug %f% >nul
ren %f% "file with a single space.txt"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

This will work on Vista and higher, any bitness.

@echo off
(
echo -----BEGIN CERTIFICATE-----
echo IA==
echo -----END CERTIFICATE-----
)>file.tmp
certutil -decode file.tmp "file with a single space.txt" >nul
del file.tmp
foxidrive
  • 40,353
  • 10
  • 53
  • 68
0

Here is the way to create a one byte single space file without creating a temporary file.

cmd /d /c (prompt=$S) ^& cmd /d /k <nul >SingleSpace.txt
sst
  • 1,443
  • 1
  • 12
  • 15