Solved

How to copy and rename files

Posted on 2002-05-23
12
201 Views
Last Modified: 2010-05-02
Hi

  Could someoen advice me on how i could

        (1). Copy and rename files in VB.
        (2). Rename a folder

Thanx
0
Comment
Question by:b3cf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:SirNick
ID: 7030431
Hi b3cf

Look at the following link to help you copy files.

http://www.freevbcode.com/ShowCode.Asp?ID=499

0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7030433
FileCopy "<source>", "<destination>"
Name "<oldName>" as "<newName>"
0
 
LVL 3

Accepted Solution

by:
SirNick earned 50 total points
ID: 7030436
Look at the following link to help you rename files

http://www.freevbcode.com/ShowCode.Asp?ID=1697

Hope this helps...
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 2

Expert Comment

by:priya_pbk
ID: 7031253
hi b3cf,

You can try this:
----------------------------------------
Open your project and give reference to "Microsoft Scripting Runtime" by going to Projects/References..

Then write this in the code:
---------------------------------------

'Declare file system object
Dim fso As New FileSystemObject

Private Sub Command1_Click()
fso.CopyFile "c:\hello.txt", "c:\helooo.txt", True
fso.CopyFolder "c:\TmpstockIdeaFiles", "c:\TmpstockIdeaFiles2222", True
fso.DeleteFolder "C:\TmpstockIdeaFiles"
End Sub

This will surely work, there are API's too to do this, but I guess the above code accomplishes the same!

-Priya

0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7031257
also give an "If" condition for both to check if the File exists before copying and Folder exists before renaming so that you dont encounter an error.

Like this:
If fso.FileExists("c:hello.txt") then
   'code for copying the file..as given above
End if

If fso.FolderExists("c:\TmpstockIdeaFiles") then
   'code for renaming the folder
End if

-priya
 
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7032157
also, if you go with the native vb methods i posted above, you can check whether a file exists by doing:

if dir(<path>) <> "" then file exists
   
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7245194
hrmm... your question:
Could someoen advice me on how i could

       (1). Copy and rename files in VB.
       (2). Rename a folder

my answer:
1) FileCopy "<source>", "<destination>"
2) Name "<oldName>" as "<newName>"

SirNick's answer (what was in the link):
FileName = "C:\MyFile.txt"
NewFileName = "C:\Text1.txt"
Name FileName As NewFileName

I answered both #1 and #2 and SirNick's #2 is pretty much the same as my #2 and i posted first... that's cool.

0
 

Author Comment

by:b3cf
ID: 7247745
Sorry man,

  I accidently accepted SirNick's answer. How could i change it ?

Thanx
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7247751
i'll post a question in cs to do it... thanks!
0
 

Author Comment

by:b3cf
ID: 7247759
what is cs ?

Thanx
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7247762
cs = community support
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7248970
b3cf

Once an answer is accepted, it is accepted. I can't change that anymore. To smooth things out here, I will post a separate question for bobbit31 in this TA.

I hope that helps :-)

** Mindphaser - Community Support Moderator **
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question