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