Solved

vb script to check for specific file in a folder and then open each file and replace a text in file.

Posted on 2012-03-09
4
243 Views
Last Modified: 2012-03-10
Hi,

I need a vb script that will check specific file, ex: all files that starts with a "D" and if it finds it i need to open that file and edit it to replace a specific character in the file.

Thanks
0
Comment
Question by:tvae
  • 2
  • 2
4 Comments
 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 1 with the location of the folder containing the files.  Customize the value of the strMask variable on line 2 with the file mask to search for.  Change the value of the blnRecurseSubfolders variable on line 3 to True if you wish to search for files in subfolders.  Customize the value of the strOldText variable on line 4 with the text to be replaced.  Customize the value of the strNewText variable on line 5 with the text with which to replace it.

Back up your files before running this, and ensure that it is performing exactly as you intend before using it with production data.  It will edit the files without any prompts or warnings.
strFolder = "c:\files"
strMask = "D*.txt"
blnRecurseSubfolders = False
strOldText = "the string to find"
strNewText = "the string to replace it with"

For Each strFile In DirToArray(strFolder, strMask, blnRecurseSubfolders)
    StringReplace strFile, strOldText, strNewText
Next

Function DirToArray(strRootFolder, strFileMask, blnRecurse)
    Dim strFolder, strCommand, objShell, objWshScriptExec
    Dim objStdOut, intLimit, strFile, arrOutput()
    
    If Right(strRootFolder, 1) = "\" Then
        strFolder = strRootFolder
    Else
        strFolder = strRootFolder & "\"
    End If

    strCommand = "%comspec% /c @dir " & Chr(34) &_
        strFolder & strFileMask & Chr(34) & " /a:-d /b"
        
    If blnRecurse Then
        strCommand = strCommand & " /s"
    End If

    Set objShell = CreateObject("WScript.Shell")
    Set objWshScriptExec = objShell.Exec(strCommand)
    Set objStdOut = objWshScriptExec.StdOut
    
    intLimit = 0

    Do Until objStdOut.AtEndOfStream
        strFile = objStdOut.ReadLine
        
        If Not blnRecurse Then
            strFile = strFolder & strFile
        End If
        
        ReDim Preserve arrOutput(intLimit)
        arrOutput(intLimit) = strFile
        intLimit = intLimit + 1
    Loop
    
    DirToArray = arrOutput
End Function

Sub StringReplace(strTextFile, strToFind, strToReplace)
    Const ForReading = 1
    Const ForWriting = 2
    Const TriStateUseDefault = -2
    Dim objFSO, objTextFile, strText, strNewText

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(strTextFile, ForReading, False, TriStateUseDefault)
    strText = objTextFile.ReadAll
    objTextFile.Close

    strNewText = Replace(strText, strToFind, strToReplace, 1, -1, vbTextCompare)

    Set objTextFile = objFSO.OpenTextFile(strTextFile, ForWriting)
    objTextFile.Write strNewText
    objTextFile.Close
End Sub

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Can you be more specific about:
I need a vb script that will check specific file, ex: all files that starts with a "D" and if it finds it i need to open that file and edit it to replace a specific character in the file.
Do you want to just look for matching files in a single folder, or recursively in child subfolder as well?

What do you mean by replace a specific character?  Is this like a search and replace, where all "A" characters become "B".   Or is this on a specific line, and at a specific position in the line?  Or what?

~bp
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 300 total points
Comment Utility
Depending on the answers to my above questions, here's a starting shell of how I would approach it.
' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "C:\temp\"

' Access the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strBaseDir)

' Process each file in the folder
For Each objFile in objFolder.Files

    ' See if it matches the pattern of filenames we want
    If UCase(Left(objFile.Name, 1)) = "D" Then

        ' Read the entire file
        strFile = objFile.Path
        Set objReader = objFSO.OpenTextFile(strFile, ForReading)
        strData = objReader.ReadAll
        objReader.Close

        ' Make desired replacements in the file
        strData = Replace(strData, "A", "B")

        ' Write the updated data back to the file
        Set objWriter = objFSO.OpenTextFile(strFile, ForWriting, True)
        objWriter.Write strData
        objWriter.Close
    End If
Next

Open in new window

~bp
0
 
LVL 38

Expert Comment

by:Shift-3
Comment Utility
Heh, well played.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

16 Experts available now in Live!

Get 1:1 Help Now