|
-
Sep 23rd, 2010, 04:40 PM
#1
Thread Starter
Member
renaming file names
hi can someone help me with the code for renaming existing files? thanks alot!
this is what ive wrote.. and it fails to rename existing files
Code:
For Each file As String In listbox1.Items
IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
While IO.File.Exists(fbCopyto.SelectedPath & file)
file= file & x & ".wav"
x += 1
End While
Next
-
Sep 23rd, 2010, 09:57 PM
#2
Fanatic Member
Re: renaming file names
Code:
For Each file As String In listbox1.Items
IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
While IO.File.Exists(fbCopyto.SelectedPath & file)
IO.File.Rename(file, "new name.wav")
x += 1
End While
Next
I'm not quite sure why you have a while statement in there for, but I wasn't sure of your context so I left it
-
Sep 23rd, 2010, 10:38 PM
#3
Re: renaming file names
Presumably you are trying to copy a file and, if a file with that name exists, you change the name numerically. E.g. if Test.txt exists then you try Test1.txt, and if that exists you try Test2.txt, etc. Here's some code that will do that:
vb.net Code:
Dim destinationFolderPath As String = fbCopyTo.SelectedPath For Each sourceFilePath As String In Me.ListBox1.Items Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath)) If IO.File.Exists(destinationFilePath) Then Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath) Dim fileNumber As Integer = 1 Dim extension As String = IO.Path.GetExtension(sourceFilePath) destinationFolderPath = IO.Path.Combine(destinationFolderPath, _ String.Format("{0}{1}.{2}", _ fileName, _ fileNumber, _ extension)) While IO.File.Exists(destinationFolderPath) fileNumber += 1 destinationFolderPath = IO.Path.Combine(destinationFolderPath, _ String.Format("{0}{1}.{2}", _ fileName, _ fileNumber, _ extension)) End While End If IO.File.Copy(sourceFilePath, destinationFilePath) Next
-
Sep 24th, 2010, 03:22 PM
#4
Thread Starter
Member
Re: renaming file names
hi jmcilhinney, thanks for the reply!
ive tried your code, unfortunately, i still cannot rename the file that has existing copy of it.
-
Sep 24th, 2010, 09:35 PM
#5
Re: renaming file names
There was an error in my code. I used 'destinationFolderPath' in a few places where I should have used 'destinationFilePath'. Here's the corrected version:
Code:
Dim destinationFolderPath As String = fbCopyTo.SelectedPath
For Each sourceFilePath As String In Me.ListBox1.Items
Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath))
If IO.File.Exists(destinationFilePath) Then
Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath)
Dim fileNumber As Integer = 1
Dim extension As String = IO.Path.GetExtension(sourceFilePath)
destinationFilePath = IO.Path.Combine(destinationFolderPath, _
String.Format("{0}{1}.{2}", _
fileName, _
fileNumber, _
extension))
While IO.File.Exists(destinationFilePath)
fileNumber += 1
destinationFilePath = IO.Path.Combine(destinationFolderPath, _
String.Format("{0}{1}.{2}", _
fileName, _
fileNumber, _
extension))
End While
End If
IO.File.Copy(sourceFilePath, destinationFilePath)
Next
You probably could have worked that out for yourself with a bit of an examination of the code and some debugging.
Here's a slightly more succinct version:
vb.net Code:
Dim destinationFolderPath As String = fbCopyTo.SelectedPath For Each sourceFilePath As String In Me.ListBox1.Items Dim destinationFilePath As String = IO.Path.Combine(destinationFolderPath, IO.Path.GetFileName(sourceFilePath)) If IO.File.Exists(destinationFilePath) Then Dim fileName As String = IO.Path.GetFileNameWithoutExtension(sourceFilePath) Dim fileNumber As Integer = 0 Dim extension As String = IO.Path.GetExtension(sourceFilePath) Do fileNumber += 1 destinationFilePath = IO.Path.Combine(destinationFolderPath, _ String.Format("{0}{1}.{2}", _ fileName, _ fileNumber, _ extension)) Loop While IO.File.Exists(destinationFilePath) End If IO.File.Copy(sourceFilePath, destinationFilePath) Next
Tags for this Thread
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
|