Solved

vbscript edit text file

Posted on 2011-03-22
9
492 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 53

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

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

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 53

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 53

Expert Comment

by:Bill Prew
ID: 35201497
Welcome.

~bp
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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