Solved

How to copy and rename files

Posted on 2002-05-23
12
196 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBS file using code from 2nd file (txt or vbs) 4 30
Put text in a picture ASP.NET C# 2 50
Visual Studio 2005 text editor 10 35
Help me. 3 45
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now