Aaargh!! Renaming file issues

B

bulldog8

I've read numerous posts and have tried multiple approaches that I
found, but just cannot get a file renamed. I am using VB.Net 2002 ...

Here is what I have tried:

1) Code common to all attempts:

OldName = "c:\albums\061203\email\DSC07272.JPG"
NewName = "c:\albums\061203\email\Lalala.JPG"

sNewFileCheck = Dir(NewName)

If sNewFileCheck = "" Then
... followed by the below

a) ReName(OldFile, NewFile)
Error 53 : File not found
I am assuming it cannot find the old file here ...

b) System.IO.File.Move
Error 57
"The process cannot access the file
"c:\albums\061203\email\Lalala.JPG" because it is being used by another
process."

- Very confusing, since Lalala.jpg did not exist prior
to the Move call

c) System.IO.File.Copy(OldFile, NewFile) works,
Kill(OldFile) fails with
Error 55
"The process cannot access the file
"c:\albums\061203\email\DSC07272.JPG" because it is being used by
another process."
- Confusing also, since in debug I go and verify that the new file
has been created. Maybe adding a DoEvents before Kill?


I wish VB was still simple and BASIC .....

Any direction would be appreciated!

- Jon
 
N

Newbie Coder

Imports System.IO

Private Sub RenameFile(ByVal sOldFile As String, ByVal sNewFile As
String)
If File.Exists(sOldFile) = True Then
File.Move(sOldFile, sNewFile)
Else
MessageBox.Show("File to be renamed doesn't exist", "Rename
Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub

Usage:

RenameFile("C:\Test1.txt", "C:\Test2.txt")
-------------------------------------------------------

It sounds like your code failed trying to move the file therefor it kept the
file locked open. This would explain why the file is being used by another
process. Same thing happens when you Open a file & forget to close it etc.

I hope this helps,

Newbie Coder
 
M

Master Programmer

VB was simple and useful. VB.NET is not simple or useful - it is slow
and tedious.

Steve Ray Irwin
 
N

NickP

?

From "Master Programmer"?

Yeah okay mate...

Master Programmer said:
VB was simple and useful. VB.NET is not simple or useful - it is slow
and tedious.

Steve Ray Irwin
 
B

bulldog8

Your code is basically what I have implemented. Just to see if placing
it in a separate sub would help, I re-wrote to include your code as a
sub.

Still got Error 57 "The process cannot access the file
"c:\albums\061213\email\Xmas Kids1.JPG" because it is being used by
another process."

Where "c:\albums\061213\email\Xmas Kids1.JPG" is the NEW file name

Don't know what it would be in use by another process when the file
does not yet exist!

- Jon
 
N

NickP

Have you tried using the new method for renaming files?

..NET 2.0.

My.Computer.FileSystem.RenameFile

Nick.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top