?
Solved

vbscript edit text file

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

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

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

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 59

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 59

Expert Comment

by:Bill Prew
ID: 35201497
Welcome.

~bp
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

840 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