?
Solved

vbscript edit text file

Posted on 2011-03-22
9
Medium Priority
?
522 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 56

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Accepted Solution

by:
Bill Prew earned 2000 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 56

Assisted Solution

by:Bill Prew
Bill Prew earned 2000 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 56

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 10 hours left to enroll

752 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