Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert a text file from UNIX / MAC to DOS format

1,411 views
Skip to first unread message

dizzler

unread,
Apr 27, 2006, 5:46:03 PM4/27/06
to
I have a directory which is populated with multiple files.
e.g.
-----------------------------
d:\myfiles\
-----------------------------
d:\myfiles\file1.csv
d:\myfiles\file2.txt
d:\myfiles\file3.noquotes
d:\myfiles\file4.rpt


regardless of extension, i know that the files will be plain text, my
quandary is being able to automatically convert MAC ( \r ) or *NIX ( \n
) files to DOS format, using DOS ( \r\n ).

Is it possible to do this purely in DOS,If not post recommendations
please?

Ted Davis

unread,
Apr 27, 2006, 8:02:43 PM4/27/06
to
On 27 Apr 2006 14:46:03 -0700, "dizzler" <john.d...@gmail.com>
wrote:

awk "BEGIN{RS=\"[\012\015]\"}{print $0}" foo > bar

where foo is the source file, bar is the target file, and awk is
gawk.exe (free and open source) from
<http://gnuwin32.sourceforge.net/packages/gawk.htm>.

That command line script sets the input record (line) separator to
either CR or LF (note - in DOS, \n is usually CRLF), then as each line
is read in, it prints the line with the default newline marker for the
OS it's running in.


--
T.E.D. (tda...@gearbox.maem.umr.edu)

Phil Robyn

unread,
Apr 27, 2006, 8:42:53 PM4/27/06
to

http://www.textpad.com/add-ons/files/utilities/convert.zip

Convert 3 programs to batch convert files between DOS, MAC and UNIX. For
example, type "ToUnix *.HTML" to convert all your DOS & MAC files before
uploading them to your UNIX web server. (56KB)

--
Phil Robyn
University of California, Berkeley

foxidrive

unread,
Apr 28, 2006, 12:19:46 AM4/28/06
to

One tool for Mac / *nix / Window

Horst's PBATS - Batch Tools

http://home.mnet-online.de/horst.muc

------------------------------------------------------------------------
CRLF CR/LF handling utility Ver 1.3 (c) 1998 Horst Schaeffer
------------------------------------------------------------------------

DOS expects plain ASCII files to be terminated by a two characters,
a CR+LF (Carriage Return + Line Feed) pair. ASCII files from other
operating systems may have lines terminated by LF only (Unix) or CR
only (Apple Mac).

CRLF.COM checks an ASCII file for line termination, and corrects it
where necessary. Optionally, CRLF.COM will convert files in any of the
three formats to either of the other two, in one pass. It will neither
change nor add to lines that are already correctly terminated for the
format requested.

Message has been deleted

Torpedro

unread,
Apr 28, 2006, 2:51:35 AM4/28/06
to
Here is a quick batch without additional tools, still has problems with
leading empty lines:

@echo off
set UFile=%1
set DFile=%2
if "%DFile%" EQU "" set DFile=%UFile%.dos
echo UFN:%Ufile% to %Dfile%
if exist "%Dfile%.old" del %Dfile%.old
if exist "%DFile%" ren %DFile% %Dfile%.old
if not exist "%UFile%" (echo Please enter Filename&goto:EOF)
for /f "tokens=1,* skip=2 delims=]" %%A in ('find /v /n "" "%UFile%"')
do (
echo Processing Line %%A
if "%%B" equ "" (echo.>>o.txt) else (echo %%B>>o.txt)
)

Torpedro

unread,
Apr 28, 2006, 2:55:08 AM4/28/06
to

if "%%B" equ "" (echo.>>%DFile%) else (echo %%B>>%DFile%)
)

dizzler

unread,
Apr 28, 2006, 2:07:03 PM4/28/06
to
Thanks to all for the suggestions, I will let you know which method
works best for me.

Timo Salmi

unread,
Apr 30, 2006, 4:36:22 AM4/30/06
to
Ted Davis wrote:
> awk "BEGIN{RS=\"[\012\015]\"}{print $0}" foo > bar

Yes.

And using SED:
@echo off & setlocal enableextensions
rem UNIX newlines to PC eolns
set source_=C:\_M\UNIX.TXT
set target_=C:\_M\PC.TXT
sed -e "s/\x0a/\x0d\x0a/" %source_% > %target_%
endlocal & goto :EOF
In fact, any "dummy" sed conversion will do:
sed -e "s/ / /" %source_% > %target_%

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

dizzler

unread,
May 9, 2006, 10:36:39 AM5/9/06
to
I recently tested the execution of

awk "BEGIN{RS=\"[\012\015]\"}{print $0}" foo > bar

I find that in some files, the conversion works, then in others i end
up with a file which has nothing but blank lines on it...is anyone
familiar with this error?

Ted Davis

unread,
May 9, 2006, 4:00:13 PM5/9/06
to
On 9 May 2006 07:36:39 -0700, "dizzler" <john.d...@gmail.com>
wrote:

It's never failed for me. What are you using for awk? What kinds of
files fail?

--
T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

0 new messages