Script to open Unix .txt file and save as windows .txt file

Hello scripting experts - I was hoping someone could help me with a script that will save a text file that is generated on a Unix system to a Windows text file.  I'm missing the CRLF fields at the end of each line and saving as a Windows text file adds them in.  I'm doing this manually now with Textpad but it would be great to automate it.
First LastAsked:
Who is Participating?
Bill PrewConnect With a Mentor Commented:
Or if you want to do it with a VBS script, here's a smal one for that.  Just save as a vbs file, and run it passing one, or two file names.  If you pass one, it will overwrite the file with the changes.  If you pass two file names, it will read from the first, and write to the second file (good for testing).

cscript yourname.vbs input.txt output.txt

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
  WScript.Echo "No filename specified."
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
Set oInfile = Nothing

sData = Replace(sData, vbLf, vbCrLf)

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing

Open in new window

Seth SimmonsConnect With a Mentor Sr. Systems AdministratorCommented:
in linux you can use dos2unix and unix2dos to convert between the two systems
this comes in handy with those carriage returns
Gerwin Jansen, EE MVEConnect With a Mentor Topic Advisor Commented:
You're fast seth :)

unix2dos is the one you want, so:

unix2dos <your file>

Check file format with:

file <your file>

then you get either:

<your file> ASCII text, with CRLF line terminators


<your file> ASCII text
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

First LastAuthor Commented:
Ok, only problem is I'm on the windows side and don't have easy access to Linux (the file is generated by a 3rd party).  If I wanted to do this on Server 2008 what would be an easy option?  Thanks for the super-quick replies guys!
First LastAuthor Commented:
Just found unix2dos for Windows, it works perfectly and my script will very simple:

unix2dos filename.txt

Thanks guys!
Seth SimmonsSr. Systems AdministratorCommented:
you beat me to it...i was just about to post a link for the windows version of the tools on sourceforge.  glad it works for you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.