Solved

vb.net - convert text file from unix to dos

Posted on 2011-09-21
3
1,447 Views
Last Modified: 2012-05-12
hello there,
I have a little app that can convert a text file from unix into windows dos..
http://www.thefreecountry.com/tofrodos/index.shtml
I would like to know how can I do the same thing with vb.net
0
Comment
Question by:XK8ER
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36576106
How are you loading and reading the unix file?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 36576114
well i dont know how to load it or read it since I think its a different approach here
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 36576193
As I understand it, the only difference between a Unix file and a Windows file is that Unix uses a newline character as a line-ending, whereas Windows uses a carriage return/newline pair (assuming you're talking about text files).

Since StreamReader.ReadLine will read up to the next line ending (whether it's a linefeed, carriage return, or carriage return/linefeed pair), and return the text without the line ending - and StreamWriter.Write will automatically append whatever line ending is defined in Environment.NewLine (which is always a carriage return/linefeed pair on Windows), you should be able to just open the file, StreamReader.Read a line, StreamWriter.Write the line to the output file and repeat until you reach the end of input.

Imports System.IO
Imports System.Text

Module Module1

    Sub Main(ByVal commandLineArgs() As String)

        If commandLineArgs.Length <> 2 Then
            Console.WriteLine("Usage:")
            Console.WriteLine("{0}{1} <in file> <out file>", vbTab, Reflection.Assembly.GetExecutingAssembly().Location)
            Console.WriteLine("{0}Press any key to exit...{1}", vbTab, vbCrLf)
            Console.ReadKey()
            Environment.Exit(1)
        End If

        Try
            Using inFile As New StreamReader(commandLineArgs(0))
                Try
                    Using outFile As New StreamWriter(File.Open(commandLineArgs(1), FileMode.CreateNew))
                        Dim buffer As String = String.Empty
                        Try
                            While Not inFile.EndOfStream
                                buffer = inFile.ReadLine()
                                outFile.WriteLine(buffer)
                            End While
                            Environment.Exit(0)
                        Catch ex As Exception
                            Console.WriteLine("Error performing conversion: {0}", ex.Message)
                            Console.WriteLine("Press any key to exit...{0}", vbCrLf)
                            Console.ReadKey()
                            Environment.Exit(4)
                        End Try
                        
                    End Using
                Catch ex As Exception
                    Console.WriteLine("Could not open out file: {0}", ex.Message)
                    Console.WriteLine("Press any key to exit...{0}", vbCrLf)
                    Console.ReadKey()
                    Environment.Exit(3)
                End Try
            End Using
        Catch ex As Exception
            Console.WriteLine("Could not open in file: {0}", ex.Message)
            Console.WriteLine("Press any key to exit...{0}", vbCrLf)
            Console.ReadKey()
            Environment.Exit(2)
        End Try
    End Sub

End Module

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question