Solved

vb.net - convert text file from unix to dos

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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 …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now