In my vb.net winform application, I am moving the file (ex: sample.xls from one folder to another. If file already exists with the same name, the new file name should be incremented (ex:sample(1).xls). How can i acheive this?
Calling getUniqueFileName(new File('/dir/example. txt') when 'example. txt' already exists while generate a new File targeting '/dir/example (1). txt' if that too exists it'll just keep incrementing number between the parentheses until a unique file is found, if an error happens, it'll just give the original file name.
If the file name doesn't yet exist, it will work fine. But if the file name already exists, then the user will get the "nameAlreadyExists()" alert dialog but the file will still be added and overwritten.
A filename may (depending on the file system) include: name – base name of the file. extension (format or extension) – indicates the content of the file (e.g. . txt , .exe , .
Hi here's a pretty "procedural" answer:
Dim counter As Integer = 0
Dim newFileName As String = orginialFileName
While File.Exists(newFileName)
counter = counter + 1
newFileName = String.Format("{0}({1}", orginialFileName, counter.ToString())
End While
you will need an imports statement for System.IO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With