Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Convert DOS file to UNIX 1

Status
Not open for further replies.

bdmangum

Technical User
Dec 6, 2006
171
US
I'm running a macro where it opens, edits, and saves a data file. The problem I'm running into is the file is orginally in UNIX form, but after I edit it with the macro it is converted to DOS. This makes it so the program which uses the file is unable to read the data properly.

Does anyone know a way to convert from DOS to UNIX using VBA? Or does anyone know of another way to edit and save a UNIX file without it automatically converting to DOS, preferably using VBA?

Thanks in advance!
 
it opens, edits, and saves a data file
Any chance you could explain how it does that ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sure thing PH. The entire goal of editing the file is to insert an "A" into the first line of the file, if it is not there already. Basically I create a temp file, then check the first line of data. If it is not an "A" then an "A" is inserted in the file, then the rest of the lines are inserted.

Code:
Sub InsertLine(YourFileName As String, YourNewString As String)
Dim intInput As Integer, inttOutput As Integer
Dim strBuffer As String

'Creates a temporary file
FileCopy YourFileName, YourFileName & ".tmp"

'Open the Input file
intInput = FreeFile
Open YourFileName & ".tmp" For Input As #intInput

'Open the output file
intOutput = FreeFile
Open YourFileName For Output As #intOutput

'Loop through the Input file
Do
  'Get a line from the temporary file
  Line Input #intInput, strBuffer
  
  'Test to current line is the first line of the file
  If intInput = 1 Then
  
    If Left(strBuffer, 1) = "A" Then
      'Write the Input String
      Print #intOutput, strBuffer
    Else
      'Write an "A", Then write the next string
      Print #intOutput, YourNewString
      Print #intOutput, strBuffer
    End If
  
  Else  'Writes all the remaining lines
    Print #intOutput, strBuffer
  End If
  
Loop Until EOF(intInput)

'Close both files
Reset
'Delete the temporary file
Kill YourFileName & ".tmp"

End Sub
 
bdmangum,
Is it an ASCII file?

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Another way:
Code:
Dim fso, f, ,ts, buf
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(YourFileName)
Set ts = f.OpenAsTextStream(1)
buf = ts.Read(1)
ts.Close
If buf <> "A" Then
  Set ts = f.OpenAsTextStream(1)
  buf = ts.Read(f.Size)
  ts.Close
  Set ts = f.OpenAsTextStream(2)
  ts.Write "A" & vbLf
  ts.Write buf
  ts.Close
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Just a procedure thatasks for input. Copy and paste itito VB MODULE AND PRESS F5.


Public Sub InsertParagraphAndText()

Dim nChapter As Integer
Dim nFigures As Integer

Dim i As Integer
nChapter = InputBox("What is the chapter number?")


nFigures = InputBox("How Many Figures?")

For i = 1 To nFigures
Selection.InsertParagraphAfter
Selection.InsertAfter " Figure " & nChapter & "." & i
Next 'i


End Sub
 
The other alternative is to use the dos2unix utility. There is of course a corresponding unix2dos...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top