Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

write to text file in flash? 2

Status
Not open for further replies.

DrCrazyDog

Programmer
Nov 26, 2000
8
US
I can use loadVariables to retrieve information from a text file, but i also want to be able to dynamically put information in that text file. please respond.
i am trying to make a list of high scores for a flash game. another way to do this would also help.
 
Aha! I just did some research on your question and as it turns out, you definitely CAN write to a TXT file using Flash and DOS commands. Here's the code I found:

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

Basically, you're opening up the DOS prompt and telling it to write (echo) the user's information into a file called "content.txt" which is located in the C:\ directory. "TypeVar" is the variable assigned to the text field from which you will save your information. I did a lot of tampering with DOS commands so I'm pretty familiar with it. If you need any additional help, you can email me at j.ski@angelfire.com. Oh and one more thing - if your file (content.txt or whatever you name it) has the first letter cut off (eg; ontent.txt instead of content.txt), simply add another "\" after "c:\" so that it reads "c:\\content.txt". Also, if you're going to save to a long file path, make sure to tell Flash the DOS path, NOT the Windows path. For example, let's take your "StartUp" folder. In Windows, the path is "c:\windows\start menu\programs\startup", but in DOS, the path is "c:\windows\startm~1\programs\startup". You can right click a folder to view the DOS name for it so you shouldn't have any trouble with that. I hope this helps :)

JuiCe
 
I know this isn't my thread or anything, but I was really interested in this one, I was wondering if maybe you explain or define those commands a little better like what each individual section does in the command line. Also I tried it out and all it did for me was open up a ms dos prompt.

Just wanted to know a littlie more.

Collin
 
Ok Collin, this one's for you :) I'll go through and explain the commands I know and i'll try and explain the others, but I may be wrong about their use. Nevertheless, I'll try :)

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

First off, we have to open the DOS prompt to execute the commands that follow. Make absolutely sure to set the path as an expression, because otherwise Flash will ignore the rest of your string.

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

Now we're adding the ASCII equivalent of the number 9, which I believe is a tab. I could be wrong, but either way, just use it as a spacebar. Put it wherever you would press the spacebar after typing in a command. Hell that's what I did and it's working great :p

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

This is another tricky one. I know it's a command line switch, but what it does exactly I couldn't say. Just keep it in there :) Heh.. it's starting to seem like maybe I shouldn't be the one instructing you :\ Anyway, we shall continue.

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

After adding another chr(9) (our spacebar ;p) we have to tell DOS what to write in the text file. To write something, DOS uses the "echo" command. To test it out in Windows, click Start -> Run and type command in the box. That will bring up your DOS prompt. Now type echo, press space, and type what you want DOS to write. It will basically just copy what you typed, or echo it? :)

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

Now we're adding the string "text3=". You do not need to do this, but if you want your Flash movie to be able to load the variables from the file it just wrote to, the text file will need to assign variables to its text. Soooooo, whatever is written in this file will be named "text3" and that's what your Flash movie will call when it wants to load the file. Again, you don't need this if you just want to write to a file.

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

This isn't a command or anything, it's just a variable assigned in the example I got this code from. So right now, DOS is being told to write the following string into a text file: "text3=(whatever TypeVar says)". So if TypeVar is a text field and the user typed "hello" into it, DOS would be writing "text3=hello" in the file. If you take the "text3=" out of the command, DOS will write "hello".

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "echo" add chr(9) add "text3=" add TypeVar add ">c:\content.txt");
}

Now this one is a little strange to me. Of course we're adding the path of the file we want to add this text to, but why there is a greater-than sign (>) in there i'll never know. It looks like it's actually emulating the DOS prompt. My guess is that you don't need that in there, but I haven't tried it. Now then, change that line to wherever you want the file, making sure to use the DOS path and NOT the Windows path. Also, as stated before, if your file is cut off, place 2 slashes before the file name ("c:\windows" will be "c:\\windows").

So there you have it. A very weak attempt at explaining that command ;p Now I know this post is long, but since it is, I may as well take the time to show you something you might be interested in. DOS is a very very powerful tool when combined with Flash. I'm sure you're aware that Flash can only open EXE and COM files (and maybe a few others), but why can't it open anything else? I was fooling around with DOS and I wrote an easy little code that will open anything on the user's computer, IF the file is associated with a program. So as long as you're not trying to open a PWL file or something, you should be ok. Mind you this will only work in a stand-alone Flash application.

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "start" add chr(9) add "c:\\windows\\media\\ding.wav");
}

Since you know the basics, we'll just get right to it. The "start" command is the equivalent of the Windows "run" command. They both open the file you specify with the program it's associated with.

on (press) {
fscommand ("exec", "command.com" add chr(9) add "/c" add chr(9) add "start" add chr(9) add "c:\\windows\\media\\ding.wav");
}

Again, we're adding the path. Copy and paste this code into one of your Flash projects (or a new one) and apply it to a button. Now mind you Flash will not play the sound if you have Sound Recorder associated with your WAV's, but it will open it. You can replace the path and the file to anything you want :) Think about it.. instead of loading variables from a text file, you can just open up the whole damn thing!

Well, I hope this article was somewhat helpful to you Collin (and anyone else who might be reading ;p). Should you need any more information or help with this, feel free to post again or contact me at j.ski@angelfire.com and i'll assist in any way possible. I think this is my favorite aspect of Flash by far, since it wields so much power :) Hope this helps!!!

JuiCe
 
hey there,

I just figured some things out, thought you might want to know.

the /c command runs command.com, the line of code you specify, and then terminates the command.com application.

ascii code chr(32) is a space, chr(9) is a tab. or alt 032 and alt 009

the > from >c:\content.com is related to the echo command, its like a pointer into where you would like the text you echoed to end up.

Now I just want to know how to keep the command from overwriting the text that is previously in the window.

Collin.
 
the > is actually a command that writes data to the path you give it so echo > text.txt writes whatever ends up in the echo command to the file text.txt, overwriting anything in text.txt. >> will do an append.

Just thought I'd add my two cents.
 
I'm not sure but I think you can only run the
fscommand ("exec",) command from a projector file and
not from the swf itself. DrCrazyDog are you going to host
this game on your server? If so Unix or NT? Now that
I think about it you would have to host it so you can
update the high score file. I think you going to have to
write asp or a cgi script to open and append to the file
and call that script from flash to post the info. I
know there's a theread on this here some place if I find
it I will post it here.
 
if it is UNIX, then the echo could be replaced with cat (or left as echo), the command.com and /c would be left out and the paths would be written with a single '/'. Which menas you could use pipes to esentially write a FLASH console for *nix....

The question becomes, why use flash? why not pass this off to a program or perl script... then it could run in a browser. Grnated you'd need to severly test the code for security as it is writing to the drive, but a Java Servlet or Perl done right and theres no problem
 
Are you guys aware that this original question was posted almost 2 years ago? Regards,

oldman3.gif
 
lol.....


Craziness!!!!!
logo.gif


carlsatterwhite@orlandomediasolutions.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top