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
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
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.
>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.
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.
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.
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.
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.
Use an external tool, like echon.exe
from this zipfile (from this site). It mimics echo -n
, like it is available in Linux.
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:
The string must not contain a carriage return
XP FINDSTR will display most control characters and many extended ASCII characters as dots. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.
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.
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"
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
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