-1

How do you concatenate a string to a text file in batch file? Example: I have a text file - cdrom.txt with this output:

Caption                  SerialNumber      Size          
SMI USB DISK USB Device  AA00000000000485  15825438720   
Hitachi HTS547550A9E384  2J1100159GVPAZ    500105249280  

I need to put a label at the topmost of the line so that it would look like this:

CDROM
Caption                  SerialNumber      Size          
SMI USB DISK USB Device  AA00000000000485  15825438720   
Hitachi HTS547550A9E384  2J1100159GVPAZ    500105249280  
user352156
  • 99
  • 1
  • 4
  • 14

2 Answers2

0

How about:

ren cdrom.txt cdrom.tmp
echo CDROM > cdrom.txt
type cdrom.tmp >> cdrom.txt
del cdrom.tmp
Monacraft
  • 6,510
  • 2
  • 17
  • 29
0

Chevron > characters provide redirection in windows command processor. So, to write a string to somefile.txt, we write:

echo myString > somefile.txt

executing this command writes a new file each time, due to the single > (write redirection).

Using a double chevron >> appends the prefix text to the file instead. So the command:

echo myString2 >> somefile.txt

would append myString2 to the somefile.txt file, usually on a new line, since the command processor prefixes a newline (Windows style CRLF) to the prefix of the >> operator before writing it out to the file.

Building on this, your specific case would lead to :

echo CDROM > cdrom1.txt
type cdrom.txt >> cdrom1.txt
del cdrom.txt
ren cdrom1.txt cdrom.txt

If you'd like more fine-grained stream redirection, you'll need to read up more :P

Look here

codeBreath
  • 11
  • 3