3 Replies - 1691 Views - Last Post: 26 May 2008 - 11:34 AM Rate Topic: -----

#1 Pawel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-May 08

Randomized File Name

Post icon  Posted 25 May 2008 - 08:18 PM

Hello,

i have the followin question; how can i make with each run of that program that the tool dont save each time the same file named 123.txt

i search an string for an random named file name

hope you understand my english is very bad, so i try a 2nd time;

the file what is saved with this line;
Open "123.txt" For Append As #1

is named 123.txt when i run the program, so how can i make it that the name of the file is at each run an other one.

hope someone can help me

have a nice weekend
pawel


this is my code;



Private Sub Command1_Click()
Dim a As String
Dim b As String


Open "123.txt" For Append As #1
Print #1, Text1.Text
Print #1, Text2.Text
Close #1

With Inet1
.AccessType = icUseDefault
.Protocol = icFTP
.RemotePort = "21"
.URL = "ftp.ftp.com"
.UserName = "midddd"
.Password = "nedddd"
a = "test.txt"
b = "test.txt"
.Execute , "PUT " & a & " " & b

End With

End Sub





Is This A Good Question/Topic? 0
  • +

Replies To: Randomized File Name

#2 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: Randomized File Name

Posted 25 May 2008 - 09:35 PM

You could certainly make it random, but that's a lot more code than need be. All you need to do is have it start at "0" and count up from there.


Try this..

Declare "strFileName" at the top of all your code in the General Declarations.

Dim strFileName As String



Then in the Form_Load event, put this..

strFileName = "0"



Then all you gotta do is in the Command1_Click event, do this..
strFileName = strFileName + 1

Open strFileName For Append As #1



Make sure to replace the "123.txt" with strFileName


Of course, if you're going to be running the program multiple times then it will always start at 0, so to avoid that you'll have to check which files already exist before writing, and start from that number.

To test if a file already exists, you do this..
If Dir("C:\0.txt") <> ""



And you'll complete the If block based on the result.
If it returns an empty string, the file does NOT exist, and if returns something else, then it exists.

I'm sure you can figure out how to implement that into your program.


Alternatively, and probably simpler now that I think about it, you could make a separate text file that holds the current number you're on. You just created the file "0.txt", so you'd put "1" in the other textfile. That way you could just check what value the textfile holds when the program starts and start counting from there.

This post has been edited by Zhalix: 25 May 2008 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 Pawel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-May 08

Re: Randomized File Name

Posted 26 May 2008 - 07:01 AM

Thanks that you have taken time for my question.

My Scriptcode is now that one, if i start the program it save a file named; 1 and not a randome named file....

how can i write it down that the file is not named only; 1

the file should named; file7373838.txt, file72626151.txt , file3262626.txt

it should be not the same at each run.

anyone can test it?

thanks an advance
pawel
Private Sub Command1_Click()
Dim a As String
Dim b As String
Dim strFileName As String
strFileName = "0"
strFileName = strFileName + 1


Open strFileName For Append As #1
Print #1, Text1.Text
Print #1, Text2.Text
Close #1


With Inet1
.AccessType = icUseDefault
.Protocol = icFTP
.RemotePort = "21"
.URL = "ftp.extra.hu"
.UserName = "******"
.Password = "*******"
a = "0"

.Execute , "PUT " & a & " " & b

End With

End Sub






[Admin Edit: Removed Username/Password from code]

This post has been edited by skyhawk133: 27 May 2008 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#4 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: Randomized File Name

Posted 26 May 2008 - 11:34 AM

I don't think you read my post all the way through.. oh well.

I guess we'll just go with random.


Since you're just doing numbers it's actually pretty easy to get a random number, I thought you wanted random letters..

Randomize Timer
strFileName = "file" & Int(Rnd * 10000000)

Open strFileName For Append As #1
...
...


This post has been edited by Zhalix: 26 May 2008 - 11:34 AM

Was This Post Helpful? 1

Page 1 of 1