There is no trivial pure batch solution if you have existing lines that may begin with spaces. It is possible to write such lines without newlines, but it takes a lot of code.
There are other issues that can further complicate a pure batch solution.
In general, Windows batch is a poor choice for manipulating text files if you want a robust, general purpose solution,
That is why I wrote JREPL.BAT - a regular expression text processing utility. JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward. No 3rd party exe file is required.
Full documentation is accessed from the command console via jrepl /?
, or jrepl /??
for paged output.
The solution is downright trivial with JREPL.
call jrepl "[\r\n]" "" /m /f "input.txt" /o "output.txt"
If you want to overwrite the original file, then
call jrepl "[\r\n]" "" /m /f "input.txt" /o -
This solution will work as long as your entire file can be read into memory by JScript. I believe the limit is close to 1 gigabyte.
Update 2020-07-14
The size limit has been eliminated starting with JREPL version 8.5 that was released 2020-02-29. Prior versions required the /M
option to load the entire file into memory. Version 8.5 introduces the /EOL
option that specifies the end of line sequence to be used when writing each line. The value can be set to an empty string, thus removing all carriage returns and line feeds, and it does this by processing one line at a time.
call jrepl "^" "" /eol "" /f "input.txt" /o "output.txt"