via delle Pappe 34 Pistoia    348 2658584   0573 1780720   posta@spataro.info

Usare un file di testo come database

2010/03/03 09:24:52
script asp

Spesso abbiamo necessità di archiviare pochi dati e un database può essere anche troppo ingombrante, ecco che allora poter leggere e scrivere delle variabili su un file di testo può essere molto comodo.

Quello messo a disposizione mi è servito per memorizzare delle variabili inerenti a dalle parti grafiche di un sito web.

Il funzionamento è semplice, basta scrivere ciò che si desidera per ogni record e poi aggiornare il file txt.
Lo scrip è facilmente espandibile e pienamente personalizzabile.

La licenza di utilizzo è libera per usi personali e lavorativi a patto di lasciare i riferimenti all'autore.

P.S. Occorre abilitare i permessi di lettura e scrittura sulla cartella.

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> Usare  un file di testo come database </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
  <!-- Autore: Pino Paolo Spataro - www.spataro.info -->
	<style>
	.settaggio {border:none; border-bottom: solid 1px #666666; width: 300px;}
	</style>
 </head>

 <body>
<%
fai=request.querystring("fai")
If fai="scrivi" then
%>
<%  'Autore: Pino Paolo Spataro - www.spataro.info
comando = Request.querystring("comando")

	primo = Request.form("primo")
	secondo = Request.form("secondo")
	terzo = Request.form("terzo")
	quarto = Request.form("quarto")
	quinto = Request.form("quinto")

	'Assegno i valori di default
	if primo="" then primo="FF0000"
	if secondo="" then secondo="000000"
	if terzo="" then secondo="000000"
	if quarto="" then secondo="000000"
	if quinto="" then secondo="000000"
	
	'if comando="geometria" then
		Set fso=CreateObject("Scripting.FileSystemObject")
		if fso.FileExists(Server.Mappath("setting.txt")) then

		   newFile = fso.GetTempName
		   Set outFile = fso.OpenTextFile( newFile, 2, True, 0)
		   Set inFile = fso.OpenTextFile(Server.Mappath("setting.txt"), 1, False, 0 )

		   Do While Not inFile.AtEndOfStream
				stringa = inFile.ReadLine & vbCrLf
				nomevariabile=Left(stringa,InStrRev(stringa,"=")-1)
				valorevariabile=Right(stringa,Len(stringa)-InStrRev(stringa,"="))
				Select Case nomevariabile
					Case chi&"primo" stringa=nomevariabile&"="&primo & vbCrLf
					Case chi&"secondo" stringa=nomevariabile&"="&secondo & vbCrLf
					Case chi&"terzo" stringa=nomevariabile&"="&terzo & vbCrLf
					Case chi&"quarto" stringa=nomevariabile&"="&quarto & vbCrLf
					Case chi&"quinto" stringa=nomevariabile&"="&quinto & vbCrLf
				End Select
			  outFile.Write stringa
		   Loop

		   inFile.Close
		   outFile.Close
		   Set inFile = Nothing
		   Set outFile = Nothing
		   
		   fso.DeleteFile Server.Mappath("setting.txt")
		   fso.MoveFile newFile, Server.Mappath("setting.txt")
			
		end if
	'end If
		
	if comando="formattazione" Then	
	'come sopra
	End If
	
	response.redirect("default.asp?msg=OK")
%>
<%else%>
<%
'ricavo i titoli delle sezioni
Set fso=CreateObject("Scripting.FileSystemObject")
if fso.FileExists(Server.Mappath("setting.txt")) then
	Set file=fso.OpenTextFile(Server.Mappath("setting.txt"),1,False,0)
	Do While file.AtEndOfStream <> True
		stringa = file.ReadLine
		nomevariabile=Left(stringa,InStrRev(stringa,"=")-1)
		valorevariabile=Right(stringa,Len(stringa)-InStrRev(stringa,"="))
		Select Case nomevariabile
			Case "primo" primo = valorevariabile
			Case "secondo" secondo = valorevariabile
			Case "terzo" terzo = valorevariabile
			Case "quarto" quarto = valorevariabile
			Case "quinto" quinto = valorevariabile
		End Select
	Loop
end if

file.Close()
Set file=Nothing
Set fso=Nothing
%>

<div style="font: 13px tahoma; color: #666666;">
<form method="post" action="default.asp?fai=scrivi">
	<input class="settaggio" type="text" name="primo" value="<%=primo%>"/> <br />
	<input class="settaggio" type="text" name="secondo" value="<%=secondo%>"/> <br />
	<input class="settaggio" type="text" name="terzo" value="<%=terzo%>"/> <br />
	<input class="settaggio" type="text" name="quarto" value="<%=quarto%>"/> <br />
	<input class="settaggio" type="text" name="quinto" value="<%=quinto%>"/> <br />
	<input type="submit" value="cambia"/>
</form>

<%messaggio=request.querystring("msg")%>
<%=messaggio%>
</div>

<%End if%>
 </body>
</html>