 |
VB How-To Ask your "How do I do this with VB?" questions in this forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

May 10th, 2007, 08:59 AM
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
how do i navigate from one file to another -VB6
hi there vb-gurus
am trying to develop a small program that manipulate files by copying the file name of one file and renaming another file using the file name copied. OK,I have managed to do that but what i wanted to do is to do some loop and let the program move from one file to another and select those files to rename becase i have a lot of files. So how do i move from one file to another and do a loop?(files are in a folder) Here is the code that i have written:
Private Sub Command1_Click()
Dim Oldfile As String
Dim newFile As String
Dim FileName As String
CommonDialog1.ShowOpen
Oldfile = CommonDialog1.FileName
'copy the filename
FileName = Oldfile
'delete this file as it is no loger needed
Kill Oldfile
CommonDialog2.ShowOpen
'select the file that you want to rename
newFile = CommonDialog2.FileName
'rename the file
Name newFile As FileName
End Sub
|

May 10th, 2007, 11:06 AM
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Code:
Private Sub Command1_Click()
Dim OldFile As String ' These Dim statements are part of the block
Dim newFile As String ' bounded by Sub and End Sub. IMHO, they
Dim FileName As String ' should be indented with the rest of
' that block for clarity.
CommonDialog1.ShowOpen
OldFile = CommonDialog1.FileName ' Oldfile is a String, not an object.
' Its value will persist after file is
' deleted.
FileName = OldFile ' So this line is superfluous,
' and variable âFileNameâ is also.
Kill Oldfile ' Delete the file; no longer needed.
CommonDialog2.ShowOpen
newFile = CommonDialog2.FileName ' Select the file to rename.
Name newFile As FileName ' Rename the file
End Sub
So:
Private Sub Command1_Click()
Dim OldFile As String
Dim NewFile As String
With CommonDialog1
.ShowOpen
OldFile = .FileName ' Get FileName
End With
Kill OldFile ' Delete the file; itâs no longer needed.
With CommonDialog2
.ShowOpen ' Get name of file to rename.
NewFile = .FileName ' Select the file to rename.
End With
Name NewFile As OldFile ' Rename the file
End Sub
Now, if you want to use OldFile again and again, you can put the second half of the procedure in a loop that is exited once cancel is hit on the common dialog. I'm not sure the result of that (you'll need to investigate), but let's say that it is that .FileName is an empty string. Then you would use:
Code:
Private Sub Command1_Click()
Dim OldFile As String
Dim NewFile As String
With CommonDialog1
.ShowOpen
OldFile = .FileName
End With
Kill OldFile
With CommonDialog2
.ShowOpen ' Get the initial file to be renamed
Do Until .Filename = "" ' Was one selected?
NewFile = .FileName
Name NewFile As OldFile
.ShowOpen ' Get the additional names, 1 by 1.
Loop
End With
End Sub
Or are you trying to do something else?
|

May 11th, 2007, 02:02 AM
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks brianWren for your reply. its almost exactly what i wanted to do, but i didnt want to use the common dialog to select the files manually, i wanted the program select the files automatically, do the necessary and move on to the next file( just as you would use the 'movenext' in the database). And in addition the files are sorted in an order that would work fine i what am thinking is possible.
Here is is the sample of how the files are arranged:
0001 'this is the file that i need to copy its filename , but the deleted one
0001(1) 'this is the actual file that i need to rename and keep
0002
0002(2)
0003
0003(1)
I'll be grateful for your reply.
|

May 11th, 2007, 07:31 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there...
sort a directory will be a tricky thing to do.. but you can do a loop using dir("*.*") the first time and dir() the rest of the time... it will give you all the files in a folder.
HTH
Gonzalo
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
|

May 11th, 2007, 03:05 PM
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
To expand on what Gonzalo said...
Dir() and Dir$() return file names (the name only, no path info).
The first call should include a file specification (As in Gonzoâs "*.*"). All calls without an argument will return the next file that matches the specification.
Once there are no more that match, the next call returns an empty string, and all calls after that raise an error.
So substitute for
Code:
With CommonDialog2
.ShowOpen ' Get the initial file to be renamed
Do Until .Filename = "" ' Was one selected?
NewFile = .FileName
Name NewFile As OldFile
.ShowOpen ' Get the additional names, 1 by 1.
Loop
End With
the folowing
Code:
Dim Pth As String
Pth = "C:\WhatEver\"
NewFile = Dir$(Pth & "WhateverThePattern.is") ' Get the 1st file name.
Do Until NewFile = "" ' If one was found,
Name Pth & NewFile As OldFile ' do the work
NewFile = Dir$() ' Get remaining file names
Loop
|

May 14th, 2007, 03:44 AM
|
Authorized User
|
|
Join Date: May 2007
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi briawren,thanks for the reply. I could not understand this line of code
NewFile = Dir$(Pth & "WhateverThePattern.is")
what do i replace "WhateverThePattern.is" with?
thanks
|

May 14th, 2007, 02:01 PM
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
You would replace that with pattern that includes the files you want to rename. If you want to rename every file in the folder, it would be "*.*". If only Word documents, "*.DOC".
Of course, whatever you use will trigger an error on the 2nd file as this is currently written, because it would try to name 2 files in the same folder with an identical name (which, of course, is not permitted).
|
|
 |