Results 1 to 12 of 12

Thread: [RESOLVED] Adding characters to file name to prevent overwriting?

Hybrid View

  1. #1
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Adding characters to file name to prevent overwriting?

    Quote Originally Posted by MattP View Post
    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:
    1. Private Function GetUniqueFileName(filePath As String) As String
    2.         Dim folderName = Path.GetDirectoryName(filePath)
    3.         Dim fileName = Path.GetFileNameWithoutExtension(filePath)
    4.         Dim extension = Path.GetExtension(filePath)
    5.  
    6.         Dim fileNames = Directory.GetFiles(folderName, fileName & "*" & extension)
    7.         Dim fileNumber = 1
    8.  
    9.         If fileNames.Contains(filePath) Then
    10.             Do
    11.                 filePath = Path.Combine(folderName, String.Format("{0} ({1}){2}",
    12.                                                                   fileName,
    13.                                                                   fileNumber,
    14.                                                                   extension))
    15.                 fileNumber += 1
    16.             Loop While fileNames.Contains(filePath)
    17.         End If
    18.  
    19.         Return filePath
    20.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Adding characters to file name to prevent overwriting?

    Quote Originally Posted by jmcilhinney View Post
    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.
    Huh? I agree that the fileNumber variable should be scoped to within the If block but the others have to be outside of it.
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width