|
-
Jan 16th, 2013, 06:30 PM
#7
Re: Adding characters to file name to prevent overwriting?
 Originally Posted by MattP
An alternative to jmcilhinney's solution getting an array of files matching the file name and extension and checking against that rather than hitting the file system each time to check if a file exists.
vb.net Code:
Private Function GetUniqueFileName(filePath As String) As String Dim folderName = Path.GetDirectoryName(filePath) Dim fileName = Path.GetFileNameWithoutExtension(filePath) Dim extension = Path.GetExtension(filePath) Dim fileNames = Directory.GetFiles(folderName, fileName & "*" & extension) Dim fileNumber = 1 If fileNames.Contains(filePath) Then Do filePath = Path.Combine(folderName, String.Format("{0} ({1}){2}", fileName, fileNumber, extension)) fileNumber += 1 Loop While fileNames.Contains(filePath) End If Return filePath End Function
While it's not really a big deal, only one of those five declared variables is used unless you enter the If block so the other four really belong inside the If block too.
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
|