Solved

vbscript edit text file

Posted on 2011-03-22
9
477 Views
Last Modified: 2012-05-11
I have 61 text files in a directory.

Each text file includes the phrase "Use [db1]".

Is it possible to create a vb script which loops through each text file and deletes this phrase?

 
0
Comment
Question by:Mr_Shaw
  • 5
  • 4
9 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35190869
Does it literally say [db1], or are there different names in the brackets?

Can you provide a sample file for testing?

~bp
0
 

Author Comment

by:Mr_Shaw
ID: 35191017
The files are SQL scripts.. an example would be:

Use [db1]

select * from dbo.tb1
0
 

Author Comment

by:Mr_Shaw
ID: 35191028
I need to remove the Use [db1] so that I migrate a set of stored procedures to a new schema.
0
 

Author Comment

by:Mr_Shaw
ID: 35191071
I have 625 files which i need to remove "Use [db1]"
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35191168
See if this makes sense, you will need to save as a VBS, adjust the path of the folder to process, and run.

' Create File System object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Get folder to process files in
Set oFolder = oFSO.GetFolder("C:\Temp")

' Loop through all files in folder
For Each oFile In oFolder.Files
   ' Read entire file into memory
   Set oFile = oFSO.OpenTextFile(oFile.Path, 1, false, -1)
   sData = oFile.ReadAll
   oFile.Close
   Set oFile = Nothing
   
   ' Make desired changes to file content
   sData = Replace(sData, "Use [db1]", "")

   ' Overwrite file with changed data
   Set oFile = oFSO.OpenTextFile(oFile.Path, 2, true, -1)
   oFile.Write sData
   oFile.Close
   Set oFile = Nothing
Next

' Cleanup
Set oFolder = Nothing
Set oFSO = Nothing

Open in new window

~bp
0
 

Author Comment

by:Mr_Shaw
ID: 35191360
Thanks for your help....

When I runt the script I get an eror:

Line: 11
Char : 4
Error: Input past end of file
Code 800A003E
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 35195403
Try changing the -1 to a -2 at the end of the two OpenTextFile lines.

Also, did any files process correctly?

Also, could any of the files be of 0 length?

If none of that works, then as I mentioned before I will need a file that is causing the error to test.

~bp
0
 

Author Closing Comment

by:Mr_Shaw
ID: 35198448
thanks
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 35201497
Welcome.

~bp
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now