Saving files from one format to another in VB6

How do I save files as RTF from another format?

Page 1 of 1

6 Replies - 2047 Views - Last Post: 12 October 2009 - 09:24 AM Rate Topic: -----

#1 mkalinowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 09

Saving files from one format to another in VB6

Posted 07 October 2009 - 12:08 PM

My code as I have it does fine to copy files to one directory to another, but I want to expand on this. How can I change this to not just copy the files but to save them in an .RTF format simultaneously?

Private Sub Form_Load()
	txtFileFrom.Text = ""
	txtFileTo.Text = ""
End Sub
Private Sub cmdCopy_Click()
Dim SourceDir As String
Dim TargetDir As String
Dim X As Integer
Dim P As Integer

	SourceDir = txtFileFrom.Text
	TargetDir = txtFileTo.Text
	CopyFile SourceDir, TargetDir, P
	MsgBox "Number of files copied = " & Str$(P)

End Sub
Sub CopyFile(SrcDir As String, TrgtDir As String, NumFiles As Integer)
	Dim OldDir As String  'source dir name
	Dim NewDir As String 'target dir name
	Dim FileName As String 'source filename
	Dim sType As String 'file type (extension)

	OldDir = SrcDir
	If Right$(OldDir, 1) <> "\" Then
		OldDir = OldDir & "\"
	End If

	NewDir = TrgtDir
	If Right$(NewDir, 1) <> "\" Then
		NewDir = NewDir & "\"
	End If

	NumFiles = 0	  'returns # files copied

	FileName = Dir$(OldDir & "*.*")
	While FileName <> ""
		On Error Resume Next
			FileCopy (OldDir & FileName), (NewDir & FileName)
			If Err = 0 Then
				NumFiles = NumFiles + 1
			Else
				Beep
				MsgBox Error$, MB_ICONEXCLAMATION, ("Error copying file" & FileName)
			End If
		On Error GoTo 0

		FileName = Dir$		'get next matching file

		DoEvents			'allow processes to occur
	Wend
	
End Sub


Mod Edit: When you are posting code please use code tags like this. :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Saving files from one format to another in VB6

#2 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Saving files from one format to another in VB6

Posted 08 October 2009 - 01:28 AM

Use a RTB (Rich Textbox Control, hidden if you want).

Create an array of files found.
Enum through array doing...
Load the file into rtb, saveas new file format in new directory
Kill old file



Good Luck
Was This Post Helpful? 0
  • +
  • -

#3 mkalinowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 09

Re: Saving files from one format to another in VB6

Posted 08 October 2009 - 10:07 AM

View Postvb5prgrmr, on 8 Oct, 2009 - 12:28 AM, said:

Use a RTB (Rich Textbox Control, hidden if you want).

Create an array of files found.
Enum through array doing...
Load the file into rtb, saveas new file format in new directory
Kill old file



Good Luck


Thanks VB, But the use of the RTB is new to me, I'm not great at understanding how to create arrays, in essence I'm too much of a novice to know this much detail. I installed the RTB from the references, and I've tried playing with it, but without success. About the only thing that works for me is if I specify a particular file, and only one it works. I can't figure out how to do multiple files by array as you mentioned, even though I've been searching the web for an answer on my own. All I have so far is this:
Private Sub RichTextBox1_Change()

		RichTextBox1.LoadFile "C:\Items_To_WorkOn\Training_March2009\PHP\Test\target\action.php", rtfText
		RichTextBox1.SaveFile "C:\Items_To_WorkOn\Training_March2009\PHP\Test\target\action.rtf", rtfRTF
End Sub


Any clue how to get me started using arrays to find files found and what this ENUM is?
Thanks

This post has been edited by mkalinowski: 08 October 2009 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

#4 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Saving files from one format to another in VB6

Posted 08 October 2009 - 10:51 PM

Okay,...

Lets start with an array of files using dir loop...
Private Function ListOfFilesFromDir(PathToSearch) As String()

Dim FileString As String, FoundFiles As String

If Right(PathToSearch, 1) <> "\" Then PathToSearch = PathToSearch & "\"
FileString = Dir$(PathToSearch & "*.*")
Do While FileString <> ""
  If FileString <> "." And FileString <> ".." Then
	FoundFiles = FoundFiles & FileString & ","
  End If
  FileString = Dir
Loop
If Right(FoundFiles, 1) = "," Then FoundFiles = Left(FoundFiles, Len(FoundFiles) - 1)
ListOfFilesFromDir = Split(FoundFiles, ",")
End Function


and you would call this...
Dim MyArrayOfFiles() As String
MyArrayOfFiles = ListOfFilesFromDir("SomePath")


Then to unemerate through the array returned...
Dim ForLoopCounter As Integer
Dim UpperBoundsOfArray As Integer
Dim LowerBoundsOfArray As Integer
LowerBoundsOfArray = LBound(MyArrayOfFiles)
UpperBoundsOfArray = UBound(MyArrayOfFiles)
For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  RTB.LoadFile MyArrayOfFiles(ForLoopCounter)
  RTB.SaveFile SomePath & SomeName & ".rtf", rtfRTF
  Kill MyArrayOfFiles(ForLoopCounter) 'only if you want to delete old files
Next ForLoopCounter


So what you are going to have to figure out is how to put this all together...



Good Luck
Was This Post Helpful? 0
  • +
  • -

#5 mkalinowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 09

Re: Saving files from one format to another in VB6

Posted 09 October 2009 - 10:34 AM

View Postvb5prgrmr, on 8 Oct, 2009 - 09:51 PM, said:

Okay,...

Lets start with an array of files using dir loop...
Private Function ListOfFilesFromDir(PathToSearch) As String()

Dim FileString As String, FoundFiles As String

If Right(PathToSearch, 1) <> "\" Then PathToSearch = PathToSearch & "\"
FileString = Dir$(PathToSearch & "*.*")
Do While FileString <> ""
  If FileString <> "." And FileString <> ".." Then
	FoundFiles = FoundFiles & FileString & ","
  End If
  FileString = Dir
Loop
If Right(FoundFiles, 1) = "," Then FoundFiles = Left(FoundFiles, Len(FoundFiles) - 1)
ListOfFilesFromDir = Split(FoundFiles, ",")
End Function


and you would call this...
Dim MyArrayOfFiles() As String
MyArrayOfFiles = ListOfFilesFromDir("SomePath")


Then to unemerate through the array returned...
Dim ForLoopCounter As Integer
Dim UpperBoundsOfArray As Integer
Dim LowerBoundsOfArray As Integer
LowerBoundsOfArray = LBound(MyArrayOfFiles)
UpperBoundsOfArray = UBound(MyArrayOfFiles)
For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  RTB.LoadFile MyArrayOfFiles(ForLoopCounter)
  RTB.SaveFile SomePath & SomeName & ".rtf", rtfRTF
  Kill MyArrayOfFiles(ForLoopCounter) 'only if you want to delete old files
Next ForLoopCounter


So what you are going to have to figure out is how to put this all together...



Good Luck

Thanks VB! Iused this and I stepped though every string so that I could understand what this was doing. It works ok now up to a point, but then something is breaking this. My code as I have it is now:
Private Sub Form_Load()
	txtFileFrom.Text = ""
	txtFileTo.Text = ""
End Sub
Private Sub cmdCopy_Click()
Dim SourceDir As String
Dim TargetDir As String
Dim X As Integer
Dim P As Integer

	SourceDir = txtFileFrom.Text
	TargetDir = txtFileTo.Text
	CopyFile SourceDir, TargetDir, P
	MsgBox "Number of files copied = " & Str$(P)

End Sub
Sub CopyFile(SrcDir As String, TrgtDir As String, NumFiles As Integer)
	Dim OldDir As String  'source dir name
	Dim NewDir As String 'target dir name
	Dim FileName As String 'source filename
	Dim sType As String 'file type (extension)

	OldDir = SrcDir
	If Right$(OldDir, 1) <> "\" Then
		OldDir = OldDir & "\"
	End If

	NewDir = TrgtDir
	If Right$(NewDir, 1) <> "\" Then
		NewDir = NewDir & "\"
	End If

	NumFiles = 0	  'returns # files copied

	FileName = Dir$(OldDir & "*.*")
	While FileName <> ""
		On Error Resume Next
			FileCopy (OldDir & FileName), (NewDir & FileName)
			If Err = 0 Then
				NumFiles = NumFiles + 1
			Else
				MsgBox Error$, MB_ICONEXCLAMATION, ("Error copying file" & FileName)
			End If
		On Error GoTo 0
		
		FileName = Dir$		'get next matching file
		
		DoEvents			'allow processes to occur
	Wend
	ListOfFilesFromDir (NewDir)
  
End Sub
Private Sub RichTextBox1_Change()
Dim MyArrayOfFiles() As String
MyArrayOfFiles = ListOfFilesFromDir(PathToSearch)

Dim ForLoopCounter As Integer
Dim UpperBoundsOfArray As Integer
Dim LowerBoundsOfArray As Integer
LowerBoundsOfArray = LBound(MyArrayOfFiles)
UpperBoundsOfArray = UBound(MyArrayOfFiles)
For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  RTB.LoadFile MyArrayOfFiles(ForLoopCounter)
  RTB.SaveFile NewDir & FileName & ".rtf", rtfRTF
  Kill MyArrayOfFiles(ForLoopCounter) 'only if you want to delete old files
Next ForLoopCounter

End Sub
Private Function ListOfFilesFromDir(PathToSearch) As String()

Dim FileString As String, FoundFiles As String

If Right(PathToSearch, 1) <> "\" Then PathToSearch = PathToSearch & "\"
FileString = Dir$(PathToSearch & "*.*")
Do While FileString <> ""
  If FileString <> "." And FileString <> ".." Then
	FoundFiles = FoundFiles & FileString & ","
  End If
  FileString = Dir
Loop
If Right(FoundFiles, 1) = "," Then FoundFiles = Left(FoundFiles, Len(FoundFiles) - 1)
ListOfFilesFromDir = Split(FoundFiles, ",")


RichTextBox1_Change

End Function


First when stepping through (and adding watches), the CopyFiles functions fine, the path and FileName indicate the correct paths and filenames.

Second, I then have it call to the Function ListOfFilesFromDir(PathToSearch) As String() section, and when I step through this, again, PathToSearch shows correct path, loops to each FileName show correct files in the path through each loop. When the loop finishes, the FoundFiles shows all the correct files in the array. So far so good :-)

Now, this is where the problem is occurring and I spent a couple hours trying to rearrange/re-organize this, but I just can't get it to function right. As you can see, I then call to the RTB function next (RichTextBox1_Change). Here when you step into the MyArrayOfFiles, the PathToSearch now shows as Empty, and when the step hits this MyArrayOfFiles = ListOf... string, it auto loops back to the ListOfFilesFromDir function, and since the PathToSearch is now empty, it loops infinitely through this function only pulling an array of all the C: hard files (bat, etc).

What am I doing wrong here? Again, thanks for all your help here. I'm nearly there. In waiting for your response, I'll keep trying to figure this out on my own.
Was This Post Helpful? 0
  • +
  • -

#6 vb5prgrmr  Icon User is offline

  • D.I.C Lover

Reputation: 109
  • View blog
  • Posts: 1,016
  • Joined: 21-March 09

Re: Saving files from one format to another in VB6

Posted 10 October 2009 - 03:28 AM

Okay, step by step your program should do something like this pseudo code...

Loop through directory and make list of files
Loop through list of files and load then into RTB and then save them into new directory with new name.extension (rtf)

So, what I am saying is your code is just a little off...

First, do not use the change event of the RTB to execute your code that loads a new file into the RTB because this will cause reentrancy issues and will build up your stack space. You can use the save file code there if you want but if you do not put a doevents after the RTB.LoadFile you may also have undesirable results...

so...
Private Sub cmdCopy_Click()
Dim SourceDir As String
Dim TargetDir As String
Dim ForLoopCounter As Integer
Dim UpperBoundsOfArray As Integer
Dim LowerBoundsOfArray As Integer
Dim MyArrayOfFiles() As String

If Trim(txtFileFrom.Text) = vbNullString Then
  MsgBox "Please enter a directory to copy the files from!", vbOKOnly + vbInformation, "Ooppss!"
  Exit Sub
End If

If Trim(txtFileTo.Text) = vbNullString Then
  MsgBox "Please enter a directory to copy the files to!", vbOKOnly + vbInformation, "Whoops!"
  Exit Sub
End If

SourceDir = txtFileFrom.Text
TargetDir = txtFileTo.Text

'make sure the dirs are in the correct format
If Right(SourceDir, 1) <> "\" Then SourceDir = SourceDir & "\"
If Right(TargetDir, 1) <> "\" Then TargetDir = TargetDir & "\"

MyArrayOfFiles = ListOfFilesFromDir(SourceDir)

LowerBoundsOfArray = LBound(MyArrayOfFiles)
UpperBoundsOfArray = UBound(MyArrayOfFiles)

For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  
  'first copy the file (comment out if you only want to save in new format)
  FileCopy SourceDir & MyArrayOfFiles(ForLoopCounter), TargetDir & MyArrayOfFiles(ForLoopCounter)
  
  'next load the file into the RTB so we can save it as a RTF File
  RTB.LoadFile SourceDir & MyArrayOfFiles(ForLoopCounter)
  
  'may want to uncomment next line to strip off extension
  'MyArrayOfFiles(ForLoopCounter) = Left(MyArrayOfFiles(ForLoopCounter), InStr(1, MyArrayOfFiles(ForLoopCounter), ".") - 1)

  RTB.SaveFile TargetDir & MyArrayOfFiles(ForLoopCounter) & ".rtf", rtfRTF
  
  'ok, since you are stating that you are copying files and not moving them, I commented out the next line but if you are actually talking
  'moving the files, then uncomment next line
  'Kill SourceDir & MyArrayOfFiles(ForLoopCounter)
Next ForLoopCounter

MsgBox "Number of files copied = " & Str$(UpperBoundsOfArray - 1)

End Sub

Private Function ListOfFilesFromDir(PathToSearch) As String()

Dim FileString As String, FoundFiles As String

If Right(PathToSearch, 1) <> "\" Then PathToSearch = PathToSearch & "\"
FileString = Dir$(PathToSearch & "*.*")
Do While FileString <> ""
  If FileString <> "." And FileString <> ".." Then
	FoundFiles = FoundFiles & FileString & ","
  End If
  FileString = Dir
Loop
If Right(FoundFiles, 1) = "," Then FoundFiles = Left(FoundFiles, Len(FoundFiles) - 1)
ListOfFilesFromDir = Split(FoundFiles, ",")

'now, as you can see, I also removed the call to the change event of the RTB as you do not need it
End Function



and that my fellow programmer is all you need...



Good Luck

This post has been edited by vb5prgrmr: 10 October 2009 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#7 mkalinowski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 09

Re: Saving files from one format to another in VB6

Posted 12 October 2009 - 09:24 AM

View Postvb5prgrmr, on 10 Oct, 2009 - 02:28 AM, said:

Okay, step by step your program should do something like this pseudo code...

Loop through directory and make list of files
Loop through list of files and load then into RTB and then save them into new directory with new name.extension (rtf)

So, what I am saying is your code is just a little off...

First, do not use the change event of the RTB to execute your code that loads a new file into the RTB because this will cause reentrancy issues and will build up your stack space. You can use the save file code there if you want but if you do not put a doevents after the RTB.LoadFile you may also have undesirable results...

so...
Private Sub cmdCopy_Click()
Dim SourceDir As String
Dim TargetDir As String
Dim ForLoopCounter As Integer
Dim UpperBoundsOfArray As Integer
Dim LowerBoundsOfArray As Integer
Dim MyArrayOfFiles() As String

If Trim(txtFileFrom.Text) = vbNullString Then
  MsgBox "Please enter a directory to copy the files from!", vbOKOnly + vbInformation, "Ooppss!"
  Exit Sub
End If

If Trim(txtFileTo.Text) = vbNullString Then
  MsgBox "Please enter a directory to copy the files to!", vbOKOnly + vbInformation, "Whoops!"
  Exit Sub
End If

SourceDir = txtFileFrom.Text
TargetDir = txtFileTo.Text

'make sure the dirs are in the correct format
If Right(SourceDir, 1) <> "\" Then SourceDir = SourceDir & "\"
If Right(TargetDir, 1) <> "\" Then TargetDir = TargetDir & "\"

MyArrayOfFiles = ListOfFilesFromDir(SourceDir)

LowerBoundsOfArray = LBound(MyArrayOfFiles)
UpperBoundsOfArray = UBound(MyArrayOfFiles)

For ForLoopCounter = LowerBoundsOfArray To UpperBoundsOfArray
  
  'first copy the file (comment out if you only want to save in new format)
  FileCopy SourceDir & MyArrayOfFiles(ForLoopCounter), TargetDir & MyArrayOfFiles(ForLoopCounter)
  
  'next load the file into the RTB so we can save it as a RTF File
  RTB.LoadFile SourceDir & MyArrayOfFiles(ForLoopCounter)
  
  'may want to uncomment next line to strip off extension
  'MyArrayOfFiles(ForLoopCounter) = Left(MyArrayOfFiles(ForLoopCounter), InStr(1, MyArrayOfFiles(ForLoopCounter), ".") - 1)

  RTB.SaveFile TargetDir & MyArrayOfFiles(ForLoopCounter) & ".rtf", rtfRTF
  
  'ok, since you are stating that you are copying files and not moving them, I commented out the next line but if you are actually talking
  'moving the files, then uncomment next line
  'Kill SourceDir & MyArrayOfFiles(ForLoopCounter)
Next ForLoopCounter

MsgBox "Number of files copied = " & Str$(UpperBoundsOfArray - 1)

End Sub

Private Function ListOfFilesFromDir(PathToSearch) As String()

Dim FileString As String, FoundFiles As String

If Right(PathToSearch, 1) <> "\" Then PathToSearch = PathToSearch & "\"
FileString = Dir$(PathToSearch & "*.*")
Do While FileString <> ""
  If FileString <> "." And FileString <> ".." Then
	FoundFiles = FoundFiles & FileString & ","
  End If
  FileString = Dir
Loop
If Right(FoundFiles, 1) = "," Then FoundFiles = Left(FoundFiles, Len(FoundFiles) - 1)
ListOfFilesFromDir = Split(FoundFiles, ",")

'now, as you can see, I also removed the call to the change event of the RTB as you do not need it
End Function



and that my fellow programmer is all you need...



Good Luck


Excellent, Thanks VB, I now got my program to do want I want it to do!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1