×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Use VB6 to convert UNIX.TXT file to PC.txt file

Use VB6 to convert UNIX.TXT file to PC.txt file

Use VB6 to convert UNIX.TXT file to PC.txt file

(OP)
Need help finding or building a routine to convert an UNIX type text file to a PC type text file.  I think all I need to do is add a carriage return to the linefeed within the UNIX file.

RE: Use VB6 to convert UNIX.TXT file to PC.txt file

Unfortunately the VB function Replace doesn't work for control characters, so, you have to do it the long way.



Private Sub ConvertFile( _
                    ByVal strInFileName As String, _
                    ByVal strOutFileName As String _
                    )

Dim strInFileContent As String
Dim strOutFileContent As String
Dim i As Long, j As Long

Open strInFileName For Input As #1
Open strOutFileName For Output As #2
Do Until EOF(1)
    Line Input #1, strInFileContent
    strOutFileContent = ""
    j = 1
    For i = 1 To Len(strInFileContent)
        If Asc(Mid(strInFileContent, i, 1)) = 10 Then
            strOutFileContent = strOutFileContent & vbCrLf
            j = j + 2
        Else
            strOutFileContent = strOutFileContent & Mid(strInFileContent, i, 1)
            j = j + 1
        End If
    Next
    Print #2, strOutFileContent
Loop
Close #2
Close #1

End Sub

RE: Use VB6 to convert UNIX.TXT file to PC.txt file

Attribute VB_Name = "CnvUnix2Dos"
Sub Main()
'Usage:     Windows Explorer shell content menu handler.
'Purpose:   Converts Unix formatted text files to Dos / Windows format.
'Output:    File is converted to the same name as orginal file, which is renamed to filename.unx.

'When applied to Windows Explorer Context Menu,
'it converts 'param1' text file and backup the original as 'filename.unx'.

'Revision 1.01 added convertable filetype checking.
' Feel free to use and modify
' regards Bengt Ruusunen

'Add
' HKEY_CLASSES_ROOT\*\shell\Unix2Win Converter\command\unix2dos.exe %1
' key to registry before start.

' REGEDIT4
'
' [HKEY_CLASSES_ROOT\*\Shell\Unix2Win Converter\command]
' @="\"D:\\Unix2Dos\\Unix2Dos.exe\" \"%1\""
'
'see file: 'Unix2Dos Content menu Registry key.reg'
'
'Usage tip:
' 1. Use Start -> Find -> Find files & folders to search for example .java files.
' 2. Select all found files and press right mouse button and select 'Unix2Win Converter' from opening content menu.

On Error GoTo Error_Handler

Dim strParameter As String
Dim In_File_Handle
Dim Out_File_Handle
Dim In_FileName As String
Dim Out_FileName As String
Dim FilenameEndCompare As String
Dim a As Long

strParameter = Command()

'Debug Start...
    'strParameter = "C:\Temp\testitiedsto.doc.vb6.jpg.txt"
'Debug End

'Filename might contain double quotes. -> remove them if any.
a = RemoveCharFromString(strParameter, Chr$(34)) 'Chr$(34) -> "

'Todo: Check file existence...

If Len(strParameter) = 0 Then
    MsgBox "Syntax: unix2dos <filename>", vbInformation, App.Title
    End
End If
    
Select Case strParameter
    
    Case " "
        MsgBox "Syntax: unix2dos <filename>", vbInformation, App.Title
        End
    Case Else
        
        In_FileName = strParameter 'input file
        
        FilenameEndCompare = ExtractFileEnd(In_FileName)
        
        If IsProperFileType(FilenameEndCompare) Then
        
            Out_FileName = In_FileName & ".temp" 'temp output file
            'open files
            In_File_Handle = FreeFile
            Open In_FileName For Binary As #In_File_Handle
            
            Out_File_Handle = FreeFile
            Open Out_FileName For Binary As #Out_File_Handle
    
            CR$ = Chr$(13) '&h0D
            LF$ = Chr$(10) '&h0A
            'Unix textline '0A'
            'Dos/windows textline '0D0A'
            strSpc$ = " "
            
            While Not EOF(In_File_Handle)
                Get #In_File_Handle, , strSpc$
                'If strSpc$ = CR$ Then 'File is not an unix format text file.
                '    Close #Out_File_Handle 'close file
                '    Kill Out_FileName 'and delete temporary file.
                    
                '    'MsgBox "File: " & vbCrLf & vbCrLf & In_FileName & vbCrLf & vbCrLf & " is not in Unix format." & vbCrLf & vbCrLf & _
                            "File already contains linefeed characters.", vbExclamation, App.Title
                '    End
                'End If
                If strSpc$ = LF$ Then Put #Out_File_Handle, , CR$
                If Asc(strSpc$) <> 0 Then Put #Out_File_Handle, , strSpc$
            Wend
            
            Close #In_File_Handle
            Close #Out_File_Handle
    
            FileCopy In_FileName, In_FileName & ".unx" 'Kopioidaan alkuperäinen tiedosto filename -> filename.unx
            Kill In_FileName 'Poistetaan alkuperäinen tiedosto
            Name Out_FileName As In_FileName 'Nimetään temp filename alkuperäiseksi filename.temp -> filename
        Else
            GoTo Quit_App 'Explicitly quit application.
        End If 'If ISProperFileType Then
        
End Select

Quit_App:

Exit Sub

Error_Handler:

Msg = "Error:  " & Str(Err.Number) & vbCrLf & " was generated by " & Err.Source & vbCrLf & Err.Description & vbCrLf & vbCrLf _
& "Input file: " & In_FileName & vbCrLf & vbCrLf & "Output file: " & Out_FileName
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext

Resume Quit_App

End Sub

Private Function RemoveCharFromString(ByRef StringToModify As String, CharacterToRemove As String) As Long

' Purpose    : Removes all occurances of one character. Case sensitive
' for example: a=RemoveCharFromString(""testi"",""") removes doublequotes from string.

' Parameters : StringToModify Required. String to change.
'            : CharacterToRemove Required. Character to remove from string.
'
' Returns    : Number of chars removed

Dim OCount As Long
Dim i

i = 1
Do
    i = InStr(i, StringToModify, CharacterToRemove, vbBinaryCompare) 'Case sensitive comparison
    If i Then
        StringToModify = Left(StringToModify, i - 1) & Mid(StringToModify, i + 1)
        i = i + 1
        OCount = OCount + 1
    End If
Loop While i

RemoveCharFromString = OCount

End Function

Private Function ExtractFileEnd(ByRef Filename As String) As String

' Purpose : Returns file end from a given Filename
' for example: a=ExtractFileEnd("c:\Temp\testi_tiedosto.doc.txt")
'
Dim FileNameEndStartPos As Long
Dim EndStartPosSearch As Long
Dim Temp As String

EndStartPosSearch = 1

Do
    EndStartPosSearch = InStr(EndStartPosSearch, Filename, ".", 1)
    If EndStartPosSearch Then
        FileNameEndStartPos = EndStartPosSearch
        EndStartPosSearch = EndStartPosSearch + 1
    End If
    
Loop While EndStartPosSearch

Temp$ = Mid$(Filename, FileNameEndStartPos)

ExtractFileEnd = Temp$ 'Return Filename End part ie. '.txt'

End Function

Private Function IsProperFileType(ByRef FilenameEndPart As String) As Boolean

'Returns True if FilenameEndPart is proper

Select Case FilenameEndPart
    Case ".txt", ".c", ".h", ".bas", ".doc", ".cpp", ".htm", ".html", ".js", ".jsp" 'Hyväksytyt tiedostopäätteet.
        IsProperFileType = True
    Case Else
        MsgBox "Only following filetypes are allowed to convert:" & vbCrLf & vbCrLf & ".txt, .c, .h, .bas, .doc, .cpp, .htm, .html, .js, .jsp" & vbCrLf, vbInformation, App.Title
        IsProperFileType = False
End Select

End Function

RE: Use VB6 to convert UNIX.TXT file to PC.txt file

The VB6 Replace function supports replacement of any text characters, including control characters.
Use the FileSystem object to open a file and read the entire file in:

Set objText = objFSO.OpenTextFile("c:\mytext.txt", _
              ForReading, False, TristateUseDefault)
strMyText = objText.ReadAll()
Call objText.Close


Then replace all plain LFs with CRLF


strNewText = Replace(strMyText,vbLF,vbCRLF)



Finally write to your new file:

Set objText = objFSO.OpenTextFile("c:\mynew.txt", _
                  ForWriting, False, TristateUseDefault)
    Call objText.Write(strNewText)
    Call objText.Close

Good Luck
johnwm

RE: Use VB6 to convert UNIX.TXT file to PC.txt file

Doesn't your UNIX system have doswrite and dosread?  Of course, these limit the dos filename to 8.3 format, but they handle all the CR-LF conversions for you.  If you don't have those utilities, they are probably easily obtainable.  I would think they could be used in a shell script, but I haven't had occasion to try it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources