-
Jan 26th, 2005, 10:58 AM
#1
Thread Starter
Addicted Member
fso.movefile
I was trying to rename a file under the same directory,like,
rename "c:\program files\test\file1.txt"
to "c:\program files\test\file_1.txt"
I use fso.movefile("c:\program files\test\file1.txt" ,"c:\program files\test\file_1.txt")
but it only works fine if file_1.txt doesn't exist,
if "file_1.txt" already exists, there is a message box pop up asking " the file already exists, do you want to replace it?" , I clicked o.k and nothing happened, didn't rename at all. how do I write the code to let user choose to really overwrite it or not to overwrite?
thanks for your help!
-
Jan 26th, 2005, 12:13 PM
#2
Re: fso.movefile
Code:
MoveFile Method
Description
Moves one or more files from one location to another.
Syntax
object.MoveFile source, destination
The MoveFile method syntax has these parts:
Part Description
object Required. Always the name of a FileSystemObject.
source Required. The path to the file or files to be moved. The source argument string can contain wildcard characters in the last path component only.
destination Required. The path where the file or files are to be moved. The destination argument can't contain wildcard characters.
Remarks
If source contains wildcards or destination ends with a path separator (\), it is assumed that destination specifies an existing folder in which to move the matching files. Otherwise, destination is assumed to be the name of a destination file to create. In either case, three things can happen when an individual file is moved:
If destination does not exist, the file gets moved. This is the usual case.
If destination is an existing file, an error occurs.
If destination is a directory, an error occurs.
An error also occurs if a wildcard character that is used in source doesn't match any files. The MoveFile method stops on the first error it encounters. No attempt is made to roll back any changes made before the error occurs.
Important This method allows moving files between volumes only if supported by the operating system.
According to this doc, you have to take care manually to make sure the destination filename does not exist.
Nobody knows what software they want until after you've delivered what they originally asked for.
Don't solve problems which don't exist.
"If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)
2 idiots don't make a genius.
-
Jan 26th, 2005, 01:34 PM
#3
Re: fso.movefile
As an alternative to FSO which does create some unnecessary dependencies you may simply use built-in functionality:
VB Code:
Private Sub Command1_Click()
If Dir("c:\temp\test1.txt") = "" Then
Name "c:\temp\test.txt" As "c:\temp\test1.txt"
Else
MsgBox "File: '" & "c:\temp\test1.txt" & "' already exist"
End If
End Sub
-
Jan 26th, 2005, 01:44 PM
#4
To Take RhinoBull's Code One Step Further
VB Code:
Private Sub Command1_Click()
Dim vResponse As VbMsgBoxResult
If Dir("c:\temp2.txt") = "" Then
Name "c:\temp.txt" As "c:\temp2.txt"
Else
vResponse = MsgBox("File: c:\temp2.txt already exists. Would you like to overwrite it?", vbQuestion + vbYesNo, "Overwright File?")
If vResponse = vbYes Then
Kill "c:\temp2.txt"
Name "c:\temp.txt" As "c:\temp2.txt"
End If
End If
End Sub
-
Jan 26th, 2005, 01:46 PM
#5
Re: fso.movefile
Yea, that will do it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|