Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a single spacebar to a txt file with 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

like image 623
LabRat Avatar asked Dec 20 '12 14:12

LabRat


1 Answers

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.

like image 195
Nick Hartung Avatar answered Sep 22 '22 06:09

Nick Hartung