Coping and Renaming multiple files using VB6 Code

0 pts.
Tags:
VB.NET
VBA
VBScript
Visual Basic
Hi Everyone I would like to know how can we copy multiple files from one location to another location, and having the files automatically renamed. I want to copy Images from C:Original to C:Main but i would like VB to automatically rename the files. Naming must start from 000001 to the last image for example 000100. How do i write the code for this? Any help or guidance will be much appreciated. Thanks
ASKED: November 16, 2005  8:08 AM
UPDATED: November 18, 2005  11:02 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

‘add reference to “Microsoft Scripting Runtime”

Dim fso As New Scripting.FileSystemObject
Dim rfldr As Scripting.Folder
Dim ts As Scripting.TextStream
Dim f As Scripting.File
Dim f2 As Scripting.File

Public Sub GetFiles(rfldr As Scripting.Folder)
Dim i As Integer

‘Copy all the files to a new folder
For Each f In rfldr.Files
fn = f.Name ‘name with extension
fn = fso.GetBaseName(fn) ‘name w/o extension

f.Copy (“C:temptemp2″)
Next

‘These next two steps may not be nescessary
Set rfldr = Nothing
Set rfldr = fso.GetFolder(“C:tempTemp2″)

‘rename the files you just copied
i = 1
For Each f2 In rfldr.Files
f2.Name = “newName” & i
i = i + 1
Next
End Sub

‘Button click event
Private Sub Command1_Click()
‘ set the folder name
Set rfldr = fso.GetFolder(“C:temp”)

‘send it to a sub routine
Call GetFiles(rfldr)
End Sub

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • DaveInAZ
    Here's one way. Private Sub CopyRenameFiles() Dim FSO As New FileSystemObject Dim fsoFldr As Scripting.Folder Dim fsoFile As Scripting.File Dim intCounter As Integer 'get the folder Set fsoFldr = FSO.GetFolder("C:Original") 'this is all it takes to copy the files to the other folder fsoFldr.Copy ("C:Main") 'change reference to the Main folder Set fsoFldr = FSO.GetFolder("C:Main") 'initialize the counter intCounter = 1 'start processing For Each fsoFile In fsoFldr.Files 'rename with the desired format fsoFile.Name = Format(intCounter, "00000000") 'increment the counter intCounter = intCounter + 1 Next End Sub
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following