3

When appending to a file using Windows batch commands, how to append immediately after the next word in the file?

For example, these commands

echo here is the date of > c:\arun.txt
date /t >> c:\arun.txt 

write the following text to the arun.txt file:

here is the date of
29-03-2010

But I want the output to be like this:

here is the date of 29-03-2010

How to avoid carrage return while appending?

Helen
  • 87,344
  • 17
  • 243
  • 314
Arunachalam
  • 5,417
  • 20
  • 52
  • 80

2 Answers2

7

The echo output always includes a trailing new line. To output text without a trailing new line, you can use the set /p trick described here and here:

< nul (set /p s=Today is ) > c:\arun.txt
date /t >> c:\arun.txt

But in this specific case, you can simply use the %date% variable instead of date /t, as %date% uses the same format:

echo Today is %date% > c:\arun.txt
Community
  • 1
  • 1
Helen
  • 87,344
  • 17
  • 243
  • 314
1

you can store to a variable and append

C:\test>set s=here is today's date
C:\test>for /F "tokens=*" %i in ('date /t') do set d=%i    
C:\test>set d=Tue 03/30/2010    
C:\test>echo %d%%s%
Tue 03/30/2010 here is today's date    
ghostdog74
  • 327,991
  • 56
  • 259
  • 343