drawn,example,reading,writing,with,similar,co
Quick Search for:  in language:    
drawn,example,reading,writing,with,similar,co
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
ASP/ VbScript Stats

 Code: 144,429 lines
 Jobs: 168 postings

 
Sponsored by:

 

You are in:

 
Login


 

 


Latest Code Ticker for ASP/ VbScript.
Click here to see a screenshot of this code!Ping in ASP
By Michele_Garneri on 10/28

(Screen Shot)

Embed Real Player Object
By Ziae Mousavi m. on 10/27


Set Country --> Combobox
By Hohl David on 10/27


Client Side Sorting of records
By Ravi Rajan on 10/26


Recordset paging with images
By Ravi Rajan on 10/26


Click here to see a screenshot of this code!Online photo catalogue VBScript 2.1
By Ivan Loire on 10/26

(Screen Shot)

GPS 1.4 WYSIWYG
By Guo Xu on 10/25


Click here to see a screenshot of this code!A Network Monitor tool from ActivXperts Software Inc.
By Freddy Hofstadt on 10/25

(Screen Shot)

Socket samples based on Winsock, TCP/IP and client/server communication
By Ronny Bright on 10/25


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!





Affiliate Sites



 
 
   

Edit and manipulate text files

Print
Email
 
VB icon
Submitted on: 1/18/2001 11:42:45 PM
By: T Runstein 
Level: Intermediate
User Rating: By 7 Users
Compatibility:ASP (Active Server Pages), VbScript (browser/client side)

Users have accessed this code 21581 times.
 
(About the author)
 
     This is a drawn out example of reading and writing with the FileScriptingObject. This is similar to copying a file, but allows rewriting specific line(s). It's intentionally overdone so that you can delete what you don't want. Includes extensive error handling. I've included lots of comments for newbies. 'T Runstein
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    '**************************************
    ' Name: Edit and manipulate text files
    ' Description:This is a drawn out exampl
    '     e of reading and writing with the FileSc
    '     riptingObject. This is similar to copyin
    '     g a file, but allows rewriting specific 
    '     line(s). It's intentionally overdone so 
    '     that you can delete what you don't want.
    '     Includes extensive error handling. I've 
    '     included lots of comments for newbies.
    'T Runstein
    ' By: T Runstein
    '
    ' Inputs:If using in VB, include referen
    '     ce to Microsoft Scripting Runtime
    '
    ' Assumes:Make sure you change the file 
    '     names (strpath and strFldr) or create a 
    '     C:\FirstFile.txt before running the scri
    '     pt.
    '
    'This code is copyrighted and has    ' limited warranties.Please see http://w
    '     ww.Planet-Source-Code.com/xq/ASP/txtCode
    '     Id.6454/lngWId.4/qx/vb/scripts/ShowCode.
    '     htm    'for details.    '**************************************
    
    option explicit
    On Error Resume Next
    'Since this was written for Windows Scri
    '     pting Host, 
    'it uses VBScript which doesn't use type
    '     s.
    'To use this with VB, as types to the de
    '     clarations
    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 = "C:FirstFile.txt" 	'Put In the file you want To edit
    strFldr = "C: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 "Change this line"
    				'When the above line is found, it is replaced With the line below
    				fle2.WriteLine "Changed"
    			Case "Change this one too"
    				fle2.WriteLine "This line is changed"
    			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


Other 2 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
1/19/2001 5:07:31 AM:chris.humbert
Good work, I vote for you (5 
globes).
Chris.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2001 7:49:18 AM:Andre Milare
Very cool :)
5 Globes too !!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/10/2001 4:15:21 PM:Ace315
Could you make it so that instead of 
returning an error if there is no file, 
it makes the file? that would be better
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/5/2001 11:59:43 AM:Glyn Peach
Just what I was after,  nice one!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/20/2002 3:28:00 PM:Eugene
You don't seem to specify in which mode 
you open these files.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/20/2002 4:14:02 PM:T Runstein
You are correct.  On OpenTextFile there 
is an iomode argument which is 
optional.  You are welcome to specify 1 
(readonly), 2 (write) or 8 (append - 
write starting at the end of file).  
For more info, and for other optional 
arguments, see the Scripting section in 
Web Development on msdn online.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/27/2002 3:59:31 PM:Kimo
beautiful...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | ASP/ VbScript Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.