Friday, October 15, 2010

Removing carriage returns from a text file using a DOS batch file



Hello there. Well this is really a test post more than anything else to get me aquainted with the SyntaxHighlighter set of files for code publishing on a blog available at alexgorbatchev.com. But in doing so I thought I would give an example of how to remove CR/LF characters from a text file using only batch commands.

The only trick to this is to note that when doing string manipulations in DOS, the enabledelayedepansion option needs to be set, and the variables then need to be accessed as !VAR! instead of %VAR% when being reused in a FOR loop.

@echo off 
setlocal enabledelayedexpansion

for /f %%j in (%1) do (
  set linea=%%j 
  set line=!line!!linea!
 )
echo !line! > %1