Solved

vb.net - convert text file from unix to dos

Posted on 2011-09-21
3
1,423 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 49
Help with adding DLL file in Windows project 20 32
vb.net class 3 17
How to duplicate form in Visual Studio 2015 2 18
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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