I recently had the need to write a batch file that wrote a single line of text to a file for processing by an application. I used the DOS command ‘ECHO’ and directed the output to file, however this wasn’t working and after a bit of troubleshooting I discovered that the resulting file couldn’t end with a new line.
At first I thought this could be a DOS/UNIX issue and ran a simple command-line application (DOS2UNIX) to convert my newlines to a format suitable for Unix based machines. Unfortunately this wasn’t good enough and I needed to find a way of removing the new line from my file. Eventually I found another simple application named CHANGE, which allows you to find and replace characters within files from the command line.
As the characters I needed to replace were a space and the carriage return/line feed I needed to be able to specify the characters as bytes. CHANGE.COM is able to do this, so the following command enabled me to remove the trailing new line:
CHANGE MYFILE.TXT 32,13,10 ""
The CHANGE.COM file can be downloaded from here.
Update: I found a Windows Script that will remove the line here, but have instead written a script that replaces the batch file.
Tags: automation, batch file, dos, search-and-replace
It’s been almost 15 years since I’ve used this technique, but can’t you do:
COPY CON: FILENAME.TXT
then hit Ctrl+D whenever you’re finished (I don’t believe you have to do a newline). It just copies from the keyboard buffer’s file handle to file. Admittedly the last time I used this was on DOS 6.2!
Thought I’d Google it. Turns out my memory perhaps isn’t so good (or maybe it is and there’s more than one way to skin a cat) but someone else suggests: copy con filename.txt (without the colon) then using Ctrl+Z.