Solved

file extension in vbscript

Posted on 2007-04-03
9
407 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Saving history changes to sub form 4 42
Microsoft Access combo box help 2 45
Spell Check in VB6 13 112
VBA - If Bookmark = "XXBOOKMARKXX" then 15 26
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…

776 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