Solved

vbscript edit text file

Posted on 2011-03-22
9
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 55

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Mr_Shaw
ID: 35191071
I have 625 files which i need to remove "Use [db1]"
0
 
LVL 55

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 55

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 55

Expert Comment

by:Bill Prew
ID: 35201497
Welcome.

~bp
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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