Link to home
Start Free TrialLog in
Avatar of learn
learn

asked on

rename statement?

Hi,

How to do something like
Rename file1 file2
in VB program?
ASKER CERTIFIED SOLUTION
Avatar of deighton
deighton
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This example uses the Name statement to rename a file. For purposes of this example, assume that the directories or folders that are specified already exist.

Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE"     ' Define filenames.
Name OldName As NewName     ' Rename file.

OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName     ' Move and rename file.
Avatar of Ryan Chong
Hi, try this:

Private Sub Command1_Click()
    Sourcepath = "C:\windows\desktop\"
    Targetpath = "C:\windows\desktop\"
   
    FileCopy Sourcepath & "db1.mdb", Targetpath & "db2.mdb"
    SetAttr Sourcepath & "db1.mdb", vbNormal
    Kill Sourcepath & "db1.mdb"
End Sub
Avatar of Microsoft
Microsoft

name file1 as file2
OR

you could use the FileSystemObject which is part of the Microsoft Sripting Library . (Add a reference to Microsoft Sripting Libraryin project references and use the following code

Dim fso As FileSystemObject
Set fso = New FileSystemObject
   
fso.MoveFile (SourceFile, DestFile)


Steve
write the following code snippet in whatever event u want

Open "C:\renamed.bat" For Output As #1
Print #1, "rename c:\testren.txt rentest.txt"
Close #1

dProcessId = Shell("c:\renamed.bat", vbHide)

this will do the same as that of the rename command in DOS
Avatar of learn

ASKER

Thanks a lot for all your helps.
I am giving the points to deiqhton who posted one of the correct answers in the first time .....not because he/she posted that as an answer though:-)