Solved

file extension in vbscript

Posted on 2007-04-03
9
404 Views
Last Modified: 2010-04-30
hi i have this vbs code that read through a folde and replace text in files but i only want it to replace files with extension "dot". what change must i make. here the code

'ReplaceFile.vbs
Dim objFile, objOutput
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\BatchTest\")
OldData = ".2220"
NewData = ".222011"
document.write("Hello from VBScript!")
For Each file in objFolder.Files
 
  Set objFile = objFSO.OpenTextFile(file)
  arrData = Replace(objFile.ReadAll, OldData, NewData)
  objFile.Close
  Set objOutput = objFSO.CreateTextFile(file)
  objOutput.Write arrData
  objOutput.Close
  Set objOutput=Nothing
  Set objFile=Nothing

Next

Set objFSO=Nothing
0
Comment
Question by:eaweb
  • 6
  • 3
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
This should do it. :^)


'ReplaceFile.vbs
Dim objFile, objOutput
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\BatchTest\")
OldData = ".2220"
NewData = ".222011"
document.write("Hello from VBScript!")
For Each file in objFolder.Files
  If objFSO.GetExtensionName="dot" Then
    Set objFile = objFSO.OpenTextFile(file)
    arrData = Replace(objFile.ReadAll, OldData, NewData)
    objFile.Close
    Set objOutput = objFSO.CreateTextFile(file)
    objOutput.Write arrData
    objOutput.Close
    Set objOutput=Nothing
    Set objFile=Nothing
  End If
Next

Set objFSO=Nothing
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
...just to be sure though, use this line instead...ensures the case matches...

 If lCase(objFSO.GetExtensionName)="dot" Then
0
 

Author Comment

by:eaweb
Comment Utility
it doesn't seem to work:

i get the smg when running it from command of
vbscript runtime error: type mismatch

here the code again

'ReplaceFile.vbs
Dim objFile, objOutput, ext
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objFolder: Set objFolder=objFSO.GetFolder("C:\BatchTest\")
OldData = ".20002"
NewData = ".2020202"

For Each file in objFolder.Files
If lCase(objFSO.GetExtensionName)="dot" Then

  Set objFile = objFSO.OpenTextFile(file)
  arrData = Replace(objFile.ReadAll, OldData, NewData)
  objFile.Close
  Set objOutput = objFSO.CreateTextFile(file)
  objOutput.Write arrData
  objOutput.Close
  Set objOutput=Nothing
  Set objFile=Nothing
end if
Next

Set objFSO=Nothing

and could you advice me also how to display text in vbs because it doesn't work with document.write with me
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
Comment Utility
Posted too quickly...sorry about that...try this please:

If lCase(objFSO.GetExtensionName(file.name))="dot" Then
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:eaweb
Comment Utility
and how do i display (echo) a message in vbscript in th cmd
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Sorry?  I don't understand...?
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
wscript.echo "Your message"
is the way to display a message...
0
 

Author Comment

by:eaweb
Comment Utility
YES!!!!!!!

that's it.

Thanks
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Happy to help :^)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

9 Experts available now in Live!

Get 1:1 Help Now