[RESOLVED] Adding characters to file name to prevent overwriting?
Hi, I'm making a small application where the user inputs some files in a listbox, and then the files are converted and saved in a folder. However, I still don't know how to make it see if there are files with the same name inside the listbox. So, for example if there are two files called "FileX", it will convert each one and save them, but the last converted one will replace the first one. Also, it will replace any file in that folder with the same name.
I want my application to see if there are files with the same name in that folder, and if there are, add a "(1)" to the last converted file, and if there's already a file with "(1)" in its name, add 1, so it would be a "(2)", "(3)" and so on.
My code is this:
Code:
Dim W As IO.StreamWriter
Dim i As Integer
W = New IO.StreamWriter("C:\Myprogram\Converting.bat")
For i = 0 To listbox1.Items.Count - 1
Dim getname As String
getname = System.IO.Path.GetFileName(listbox1.Items.Item(i))
'I think the code should be here
W.WriteLine(*options* & System.IO.Path.GetFileNameWithoutExtension(listbox1.Items.Item(i)) & ".converted" '& some code in here?)
Next
W.Close()
Sorry if I don't provide much code, I'm still learning.
Any help would be appreciated.
Thanks in advance.
Re: Adding characters to file name to prevent overwriting?
Time for you to look into recursion.
Code:
Public Function CheckAndGenerateUniqueCopyName(path As String, Optional seed As Integer = 1) As String
'Check if the supplied path exists
If System.IO.File.Exists(path) Then
'The path exists so lets append the seed to the end of it
path &= "(" & seed & ")"
'Increment the seed value for the next iteration
seed += 1
'Call the same method with the new parameters until a result is found
path = CheckAndGenerateUniqueCopyName(path, seed, False)
End If
Return path
End Function
The above code has not been checked, but that is, in general, what you want to do.
Re: Adding characters to file name to prevent overwriting?
Recursion is not necessarily bad but, in this case, iteration seems more appropriate, e.g.
Code:
Private Function GetUniqueNumberedFilePath(filePath As String) As String
If File.Exists(filePath) Then
Dim folderPath = Path.GetDirectoryName(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Dim extension = Path.GetExtension(filePath)
Dim fileNumber = 1
Do
filePath = Path.Combine(folderPath,
String.Format("{0} ({1}){2}",
fileName,
fileNumber,
extension))
fileNumber += 1
Loop While File.Exists(filePath)
End If
Return filePath
End Function
Re: Adding characters to file name to prevent overwriting?
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
Re: Adding characters to file name to prevent overwriting?
Quote:
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.
Re: Adding characters to file name to prevent overwriting?
Quote:
Originally Posted by
jmcilhinney
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.
Re: Adding characters to file name to prevent overwriting?
Quote:
Originally Posted by
jmcilhinney
Recursion is not necessarily bad but, in this case, iteration seems more appropriate, e.g.
Code:
Private Function GetUniqueNumberedFilePath(filePath As String) As String
If File.Exists(filePath) Then
Dim folderPath = Path.GetDirectoryName(filePath)
Dim fileName = Path.GetFileNameWithoutExtension(filePath)
Dim extension = Path.GetExtension(filePath)
Dim fileNumber = 1
Do
filePath = Path.Combine(folderPath,
String.Format("{0} ({1}){2}",
fileName,
fileNumber,
extension))
fileNumber += 1
Loop While File.Exists(filePath)
End If
Return filePath
End Function
Damn you and your better solutions, I'll never get any rep with you on this forums!!!!
Re: Adding characters to file name to prevent overwriting?
Quote:
Originally Posted by
wakawaka
Time for you to look into recursion.
Code:
Public Function CheckAndGenerateUniqueCopyName(path As String, Optional seed As Integer = 1) As String
'Check if the supplied path exists
If System.IO.File.Exists(path) Then
'The path exists so lets append the seed to the end of it
path &= "(" & seed & ")"
'Increment the seed value for the next iteration
seed += 1
'Call the same method with the new parameters until a result is found
path = CheckAndGenerateUniqueCopyName(path, seed, False)
End If
Return path
End Function
The above code has not been checked, but that is, in general, what you want to do.
Just a little question, why am I getting the following error
Code:
Too many arguments foro ‘Public Function CheckAndGenerateUniqueCopyName(path As String, [seed As String, [seed As Integer=1]) As String’.
Also, I'm not very sure of how do i have to add the function to the StreamWriter.
Thanks a lot.
Re: Adding characters to file name to prevent overwriting?
You add the method into your code inside of a class and call it like so:
Code:
Dim uniquePath As String = CheckAndGenerateUniqueCopyName("C:\temp\myfile.txt")
'or like so if you want to start with a different seed value
Dim uniquePath As String = CheckAndGenerateUniqueCopyName("C:\temp\myfile.txt", 20)
'Assuming the path exists it will return a file path of "C:\temp\myfile.txt(20)"
I should mention mine is simple. jml's example is a different way to accomplish it but his also formats the path so the instead of my "myfile.txt(20)", his returns "myfile (20).txt".
Re: Adding characters to file name to prevent overwriting?
Quote:
Originally Posted by
wakawaka
jml's example is a different way to accomplish it but his also formats the path so the instead of my "myfile.txt(20)", his returns "myfile (20).txt".
myfile.txt(20) is not a good format for an output file. myfile(20).txt will open up with whatever editor is associated with txt files. You'll get a dialog asking which program to open .txt(20) files which is bad UX.
Re: Adding characters to file name to prevent overwriting?
That is why I made the comment, but if the file is being read by an application and not a person, it is perfectly fine to do it as such.
Re: Adding characters to file name to prevent overwriting?
Although I still don't understand how this code works I'll try to add it to my code, but I think I have to learn to use functions better.
Thanks a lot, you're very helpful.