Solved

file extension in vbscript

Posted on 2007-04-03
9
410 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
ID: 18845672
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
ID: 18845679
...just to be sure though, use this line instead...ensures the case matches...

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

Author Comment

by:eaweb
ID: 18845747
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
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!

 
LVL 67

Accepted Solution

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

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

Author Comment

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

Expert Comment

by:sirbounty
ID: 18845810
Sorry?  I don't understand...?
0
 
LVL 67

Expert Comment

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

Author Comment

by:eaweb
ID: 18845895
YES!!!!!!!

that's it.

Thanks
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18845904
Happy to help :^)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 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