Print Page | Close Window

Creating files

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=10111
Printed Date: 31 March 2026 at 4:33pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Creating files
Posted By: Davej2k
Subject: Creating files
Date Posted: 23 April 2004 at 11:56am

Hi there,

I'm having a bit of trouble looking for a script to create a txt file in asp.

Basically I am runing a database to update the news feature on my site, but each actually story comes from a text file rarther than a pure line of text in from the database. So what I require is a script which allows me to type a story into a text box and save it to a folder (e.g. http://www.mynewssite.co.uk/stories/ - http://www.mynewssite.co.uk/stories/ ) with a unique file name prferably one that is automatically generated using the day's date or something similar. But one where a text box is used to enter the file name would work just aswell.

Any help would be great, cheers guys.

Dave




Replies:
Posted By: Mart
Date Posted: 23 April 2004 at 12:27pm
Google for 'FSO in classic asp' or try this http://www.asptutorial.info/learn/OpenReadCreate-files.asp - http://www.asptutorial.info/learn/OpenReadCreate-files.asp


Posted By: Davej2k
Date Posted: 23 April 2004 at 1:29pm

Thanks for the link...

the only problem I have is each file needs to have a different name. e.g. news_story1.txt news_story2.txt etc...

The one's on those examples all write to a preset file.



Posted By: Mart
Date Posted: 23 April 2004 at 2:25pm
For the code below it will create the file then write to it


<%
thetext="Write this text in the file"

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.CreateTextFile("c:\Mydir\myfile.txt", True)
wfile.Write (thetext)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("Text created")
%>





Posted By: Semikolon
Date Posted: 23 April 2004 at 3:38pm
replace "c:\Mydir\myfile.txt" with a variable were you have placed the file name


Posted By: Davej2k
Date Posted: 23 April 2004 at 6:00pm
Sounds straight forward but is there a line of code that will do this as I don't want to have to be changing that line everytime I want to start a new file.


Posted By: Semikolon
Date Posted: 23 April 2004 at 6:07pm
make a form field called for example filename and place the value of that into a variable called for example strFileName and Replace "c:\Mydir\myfile.txt" with strFileName

to get the value of a field use Request.Form("fieldname")


Posted By: Davej2k
Date Posted: 23 April 2004 at 6:33pm

OK so far here's what I have an initial page which contains the form fields filename and story and posts to this page:

<%

Dim strfilename

Dim Story

filename = Request.Form("fieldname")

Story = Request.Form("story")

Set fs = CreateObject("Scripting.FileSystemObject")

Set wfile = fs.CreateTextFile("strfilename", True)

wfile.Write (Story)

wfile.close

Set wfile=nothing

Set fs=nothing

response.write("Text created")

%>

At the moment I'm getting an error saying permission denied probably to the folder I'm trying to add the file to. Can you point me in the way of any other faults. I'm new to asp BTW and really appreciate your help!



Posted By: Mart
Date Posted: 24 April 2004 at 3:50am
Ok, firstly youve dimmed strfilename but not set it to anything. And on the line "Set wfile = fs.CreateTextFile("strfilename", True)" you have put quotation marks around strfilename...  Remove them. The error your getting will probably be  caused by incorrect NTFS permissions on the folder that you are trying to write to. But since you haven'y posted the error I cannot be sure. There is a tutorial on this site about setting permissions (it's aimed at access database but its the same concept) take a look at that if you are not sure what to do.


Posted By: Semikolon
Date Posted: 24 April 2004 at 5:39am
oh sorry, forgot to tell you that FSO needs the full phusical path on the server. try this code..

<%
Dim strfilename
Dim Story

filename = Request.Form("fieldname")
Story = Request.Form("story")

Set fs = CreateObject("Scripting.FileSystemObject")
Set wfile = fs.CreateTextFile(Server.MapPath("stories/" & strfilename), True)

wfile.Write(Story)

wfile.close
Set wfile=nothing
Set fs=nothing

response.write("Text created")
%>

If you havn't specified an extension for the file in the filename textbox add & ".<extension>" after strFilename: Set wfile = fs.CreateTextFile(Server.MapPath("stories/" & strfilename & ".txt"), True)


Posted By: Davej2k
Date Posted: 24 April 2004 at 4:03pm
Cheers guy's that's really helped. I might come back to this thread if I need anymore help, but what so far everything seems ok.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net