greenstar91

I am making a program and when the user clicks exit, I want a set number of txt files to be created, all with random names.

I have thought that I could create the files individually by just copying and pasting the code but this would make my programme larger and very boring for me to make. How do I get the code to create a number of txt files of my choice and all have random names, preferably with no numbers, just letters

Thanks.




Re: Visual Basic Express Edition Need advice

Evoluator

Well if I have understood you right, this is my advise:

Solution 1:

'When the exit button is envoked
Dim LetterArray As Char() = {"a", "A", "b", "B", "c", "C", "d"} '.....
Dim Loops As Integer = 0

Do Until Loops > 10 '10 specifies 10 files to be created

Dim FileName As Char()

For ii As Integer = 0 To 16 '16 specifies the maximum length of the file name
Randomize()
FileName(ii) = (LetterArray(CInt(Int((letterArray.Length - 1) * Rnd())) - 1))
Next

My.Computer.FileSystem.WriteAllText("The desired file location\" & FileName.ToString, "The contents you want to write", False) 'False = Creates a new file, True = adds the contents to existing file (if any)

Loops += 1
Loop


Solution 2:

Instead of creating a LetterArray you could use a byte array (Dim ByteArray As Byte())
and use:

Dim loops As Integer = 0

Do Until loops > 10 'Specified 10 files to be created
Randomize()

Dim byteArray As Byte() = New Byte(31) {} ' 31 = 32 - 1 Which is the length of the desired file name (16 characters)

For iii As Integer = 0 To 31 '31 specifies the maximum length of the file name = (31 +1 ) / 2
Randomize()
byteArray(iii) = (CInt(Int(256 * Rnd())))


' Don't go greddy and use a value higher than 256 Exceptions would be thrown to your face

Next

Dim FileName As String = Nothing

Dim strBuilder As Char() = New Char(15) {} '15 = 16 -1 Because the FileName is assumed to be 16 characters long
For i As Integer = 0 To byteArray.Length / 2 - 1
strBuilder(i) = BitConverter.ToChar(byteArray, 2 * i)
Next

For i As Integer = 0 To byteArray.Length / 2 - 1
FileName = FileName & strBuilder(i)
Next

My.Computer.FileSystem.WriteAllText("The desired file location\" & FileName.ToString, "The contents you want to write", False)

'False = Creates a new file, True = adds the contents to existing file (if any)

loops += 1

Loop

Solution two makes such random filenames, even if you know the file name you think it is random!!! But seriously it is random to an extent which your will be surprised when you see the file names generated!

I hope the code was helpful







Re: Visual Basic Express Edition Need advice

greenstar91

This code is very helpful but how would I make them .txt. files Also, the first code does not work, and the second one 'contains illegal chracters'. How do I get past this problem






Re: Visual Basic Express Edition Need advice

AmazingAnt

In either of his solutions, the way to make them .txt files would be to add + ".txt" to the line that writes the text, so it would go from this:

My.Computer.FileSystem.WriteAllText("The desired file location\" & FileName.ToString, "The contents you want to write", False)

to this:

My.Computer.FileSystem.WriteAllText("The desired file location\" & FileName.ToString + ".txt", "The contents you want to write", False)


As to the problem about the characters, I've got no clue, as I didn't read his code very carefully, so you'll have to wait for him for an answer on that.





Re: Visual Basic Express Edition Need advice

Evoluator

Ok then:

You have vaugly defined the problem and I have responded as vague as you have asked the question.

If you want to make .txt files you need to write the following code:

My.computer.FileSystem.WriteAllText("File Location" & ".txt", "The text which you want to write in the file", True) 'True would add to the existing file (if any) while False value would overwrite the existing file (if any).

[Note]:
Dim Buffer1 As Byte() ' This code would create an array with data type of Byte.

#############
You could write any value that you like inside the Buffer1 and finally use the code bellow to create a text file!


My.computer.FileSystem.WriteAllBytes("File Location" & ".txt", Buffer1, True)


forget about Solution 2 !





Re: Visual Basic Express Edition Need advice

greenstar91

Ok I will try and explain a further issue I have:

In solution 1 there is a warning message that says "Variable 'Filename' is used before it has been assigned a value. A nullreference could result at runtime."

This does happen when I debug.





Re: Visual Basic Express Edition Need advice

Dave299

To get a random file name with a .txt extension you could use the following:

Dim Filename As String

Filename = IO.Path.GetFileNameWithoutExtension(IO.Path.GetRandomFileName) & ".txt"

If you really don't want numbers in it then you could try something like:

Do

Filename = IO.Path.GetFileNameWithoutExtension(IO.Path.GetRandomFileName) & ".txt"

Loop While Filename.Contains("1") Or Filename.Contains("2") Or Filename.Contains("3") Or Filename.Contains("4") Or Filename.Contains("5") Or Filename.Contains("6") Or Filename.Contains("7") Or Filename.Contains("8") Or Filename.Contains("9") Or Filename.Contains("0")

Personally I'd rather have the numbers.





Re: Visual Basic Express Edition Need advice

Evoluator

I think Dave has given you the best .NET solution. I would have recomended you to get your hand dirty if you want to learn how the system works