-2

We have a program that installs a config document in %appdata%\Roaming\. I need a script that I can push out via GPO that will do the following:

  • Search a specified directory by file extension
  • Search all files of specified extension for a string
  • Change this string with another string

fin.

I attempted to start learning Visual Basic for this, but I feel vastly out of my element, as I've done nothing like this before.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • What file extension are you searching? What is a "specified extension" and what string should be found? What is the search string and what the replace? What was the outcome of your learning? So much questions and no answer ..... :( – Endoro May 29 '13 at 22:56
  • Endoro, I'm asking how this would be done. That's why it is formulated as a question and not as a declarative how-to. I'm not sure where you would get that I am insinuating that I've written this script, as I am clearly asking for a starting point on how this would work in VB or as a batch script. – Aaron Stovall May 29 '13 at 23:34
  • I'd use a hybrid batch/vbscript. The batch to find all the files since it's only a few lines of code vs many in vbscript but use vbscript to do the find/replace because batch isn't particularly good at that without a lot of code. – Matt Williamson May 30 '13 at 00:39

2 Answers2

0

The files in a given folder can be processed like this:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\your\folder").Files
  'do stuff
Next

For processing only files with a particular extension (e.g. .foo) add a conditional like this:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\your\folder").Files
  If LCase(fso.GetExtensionName(f.Name)) = "foo" Then
    'do stuff
  End If
Next

If you want to process files in subfolders as well, you need to recurse into subfolders.

The string replacement part could look like this:

text = f.OpenAsTextStream.ReadAll
If InStr(text, "some string") > 0 Then
  f.OpenAsTextStream(2).Write Replace(text, "some string", "other string")
End If
Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Here is my Hybrid Batch/VBScript that I mentioned in my comment

Copy the code and save as FindAndReplace.cmd Call it from a CMD prompt or as a command in GP like this:

FindAndReplace "String to Find" "String to Replace"

The way I have it setup, it will only search for files ending in .txt and will recurse into subfolders. set the mask variable to the top level folder and filemask. If you don't want to replace text in files in subfolders, remove the /S from the DIR command. It isn't case sensitive so

FindAndReplace "String to Find" "String to Replace" is the same as
FindAndReplace "STRING TO FIND" "STRING TO REPLACE" or
FindAndReplace "String TO FIND" "STRING To Replace"


    ::Find and Replace
    ::Matt Williamson 
    ::5/30/2013

    @echo off
    setlocal
    set mask=%appdata%\My Folder\*.txt
    set tmp="%temp%\tmp.txt"
    If not exist %temp%\_.vbs call :MakeReplace
    for /f "tokens=*" %%a in ('dir "%mask%" /s /b /a-d /on') do (
      for /f "usebackq" %%b in (`Findstr /mic:"%~1" "%%a"`) do (
        echo(&Echo Replacing "%~1" with "%~2" in file %%~nxa
        <%%a cscript //nologo %temp%\_.vbs "%~1" "%~2">%tmp%
        if exist %tmp% move /Y %tmp% "%%~dpnxa">nul
      )
    )
    del %temp%\_.vbs
    exit /b

    :MakeReplace
    >%temp%\_.vbs echo with Wscript
    >>%temp%\_.vbs echo set args=.arguments
    >>%temp%\_.vbs echo .StdOut.Write _
    >>%temp%\_.vbs echo Replace(.StdIn.ReadAll,args(0),args(1),1,-1,1)
    >>%temp%\_.vbs echo end with
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36