Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

FatCrab

Military
Aug 4, 2003
1
0
0
US
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.
 
Replies continue below

Recommended for you

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

Code:
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

 
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 &quot;Syntax: unix2dos <filename>&quot;, vbInformation, App.Title
End
End If

Select Case strParameter

Case &quot; &quot;
MsgBox &quot;Syntax: unix2dos <filename>&quot;, vbInformation, App.Title
End
Case Else

In_FileName = strParameter 'input file

FilenameEndCompare = ExtractFileEnd(In_FileName)

If IsProperFileType(FilenameEndCompare) Then

Out_FileName = In_FileName & &quot;.temp&quot; '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$ = &quot; &quot;

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 &quot;File: &quot; & vbCrLf & vbCrLf & In_FileName & vbCrLf & vbCrLf & &quot; is not in Unix format.&quot; & vbCrLf & vbCrLf & _
&quot;File already contains linefeed characters.&quot;, 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 & &quot;.unx&quot; '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 = &quot;Error: &quot; & Str(Err.Number) & vbCrLf & &quot; was generated by &quot; & Err.Source & vbCrLf & Err.Description & vbCrLf & vbCrLf _
& &quot;Input file: &quot; & In_FileName & vbCrLf & vbCrLf & &quot;Output file: &quot; & Out_FileName
MsgBox Msg, , &quot;Error&quot;, 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(&quot;&quot;testi&quot;&quot;,&quot;&quot;&quot;) 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(&quot;c:\Temp\testi_tiedosto.doc.txt&quot;)
'
Dim FileNameEndStartPos As Long
Dim EndStartPosSearch As Long
Dim Temp As String

EndStartPosSearch = 1

Do
EndStartPosSearch = InStr(EndStartPosSearch, Filename, &quot;.&quot;, 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 &quot;.txt&quot;, &quot;.c&quot;, &quot;.h&quot;, &quot;.bas&quot;, &quot;.doc&quot;, &quot;.cpp&quot;, &quot;.htm&quot;, &quot;.html&quot;, &quot;.js&quot;, &quot;.jsp&quot; 'Hyväksytyt tiedostopäätteet.
IsProperFileType = True
Case Else
MsgBox &quot;Only following filetypes are allowed to convert:&quot; & vbCrLf & vbCrLf & &quot;.txt, .c, .h, .bas, .doc, .cpp, .htm, .html, .js, .jsp&quot; & vbCrLf, vbInformation, App.Title
IsProperFileType = False
End Select

End Function
 
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:
[tt]
Set objText = objFSO.OpenTextFile(&quot;c:\mytext.txt&quot;, _
ForReading, False, TristateUseDefault)
strMyText = objText.ReadAll()
Call objText.Close[/tt]

Then replace all plain LFs with CRLF

[tt]
strNewText = Replace(strMyText,vbLF,vbCRLF)[/tt]


Finally write to your new file:[tt]

Set objText = objFSO.OpenTextFile(&quot;c:\mynew.txt&quot;, _
ForWriting, False, TristateUseDefault)
Call objText.Write(strNewText)
Call objText.Close[/tt]


Good Luck
johnwm
 
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.
 
Status
Not open for further replies.
Back
Top