Solved

file extension in vbscript

Posted on 2007-04-03
9
405 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

930 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

10 Experts available now in Live!

Get 1:1 Help Now