Código fuente de 'Edita y manipula ficheros de texto.asp'
<html>
<head>
<title>Edita y manipula ficheros de texto - Códigos asp, programacion asp, descargas asp, rutinas asp</title>
</head>
<body style="font-family: Arial; font-size: 11pt">
<center><b><font face="Arial" size="3">Edita y manipula ficheros de texto</font></b></center><br>
<br>
Este ejemplo permite, dado un fichero de texto, modificar o eliminar las líneas que contengan un cierto
contenido. <br>
P.ej: <br>
En este caso se han cambiado las líneas del fichero "Texto.txt"
que contenían Astalaweb.net o Astalaweb.org por "Cambiado Astalaweb.org a
Astalaweb.com" y "Cambiado Astalaweb.net a Astalaweb.com<br>
<!--'**************************************
' By: T Runstein
' Assumes:Make sure you change the file names (strpath and strFldr) or create a
' C:\FirstFile.txt before running the script.
' http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6454&lngWId=4
-->
<%
On Error Resume Next
'Since this was written for Windows Scripting Host,
'it uses VBScript which doesn't use types.
'To use this with VB, as types to the declarations
Dim objFSO 'as FileSystemObject
Dim fle1 'as file
Dim fle2 'as file
Dim strPath 'as String
Dim strFldr 'as String
Dim strLine 'as String
strPath = server.mappath("FirstFile.txt") 'Put In the file you want To edit
strFldr = server.mappath("TempFile.txt")
Main 'This Calls the Main Sub
Sub Main()
Dim rtn 'as Integer
rtn = CopyStuff() 'This calls and runs the CopyStuff function
if rtn = 1 Then
MsgBox "Copy is complete"
else
MsgBox "An Error was found and the process was aborted. " & Cstr(rtn)
'The & Cstr(rtn) will display the number returned by CopyStuff
'After you've got your script running, you may want To remove this feature
End if
'Cleanup
if Not fle1 is nothing Then Set fle1 = nothing
if Not fle2 is nothing Then Set fle2 = nothing
if Not objFSO is nothing Then Set objFSO = nothing
End Sub
function CopyStuff()
Set objFSO = CreateObject("Scripting.FileSystemObject") 'This creates the FSO
'I've included Error handling after Each step
if err.number <> 0 Then
MsgBox "Error In Creating Object: " & err.number & "; " & err.description
CopyStuff = 0 'Returns this number
Exit function 'Stop processing, go back to Main
End if
if Not objFSO.FileExists(strPath) Then 'The file To copy is not present
MsgBox "The " & strPath & " file was Not found On this computer"
CopyStuff = 2
Exit function
End if
if objFSO.FileExists(strFldr) Then
objFSO.DeleteFile(strFldr) 'If the temp file is found, delete it
End if
Set fle1 = objFSO.OpenTextFile(strPath) 'Open
if err.number <> 0 Then
MsgBox "Error opening " & strPath & ": " & err.number & "; " & err.description
CopyStuff = 3
Exit function
End if
Set fle2 = objFSO.CreateTextFile(strFldr) 'Create the temp file
if err.number <> 0 Then
MsgBox "Error creating temp ini: " & err.number & "; " & err.description
CopyStuff = 4
Exit function
End if
'Here's the work horse that does the copying
Do While Not fle1.AtEndofStream 'Change this line, Change this one too
strLine = fle1.ReadLine
Select Case strLine
Case "Astalaweb.org"
'When the above line is found, it is replaced With the line below
fle2.WriteLine "Cambiado Astalaweb.org a Astalaweb.com"
Case "Astalaweb.net"
fle2.WriteLine "Cambiado Astalaweb.net a Astalaweb.com"
Case Else
'This copies whatever was read In fle1
fle2.WriteLine strLine
End Select
Loop
if err.number <> 0 Then
MsgBox "Error transfering data: " & err.number & "; " & err.description
CopyStuff = 5
fle1.close
fle2.close
Exit function
End if
fle1.close
Set fle1 = nothing
fle2.close
Set fle2 = nothing
objFSO.DeleteFile strPath, True 'This deletes the original file
objFSO.MoveFile strFldr, strPath 'This moves and renames the temp file, replacing the original
if err.number <> 0 Then
MsgBox "Error replacing " & strPath & " With new file: " & err.number & "; " & err.description
CopyStuff = 6
Else
CopyStuff = 1 'Remember that In Main, a 1 means successful
End if
End function
%>