Find and replace by file extension

Hello,

I need to do a text find and replace against all files with the extension ".abc".  Additionally I need to run multiple find and replaces against all .abc files.  There are probably 40 lines that need to change within hundreds of these files.

Search for and replace 40 lines in all .abc files:

"L:\somedir\somedir" change to "i:\newdir\newdir"
"L:\somedir\somedir2" change to "i:\newdir\newdir2"
"L:\somedir\somedir3" change to "i:\newdir\newdir3"
40 additional lines to change.

Thanks in advance for any assistance.
crash1624Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Here's a small VBS example, just adjust the folder path as needed to specify the location of the files to process.

Naturally, make a backup copy before testing...

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "C:\Files\"
strBaseExt = ".ABC"

' 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(Right(objFile.Name, Len(strBaseExt))) = UCase(strBaseExt) 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, "L:\somedir\somedir", "i:\newdir\newdir", 1, -1, vbTextCompare)
        strData = Replace(strData, "L:\somedir\somedir2", "i:\newdir\newdir2", 1, -1, vbTextCompare)
        strData = Replace(strData, "L:\somedir\somedir3", "i:\newdir\newdir3", 1, -1, vbTextCompare)

        ' 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
crash1624Author Commented:
Thanks BP, I need to search the entire volume D: for .abc files and make these changes.
0
crash1624Author Commented:
Can anyone else help with this, I need to search all directories on D: using the script provided by bp above.  Changing
strBaseDir = "D:\"

Open in new window

does nothing.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Bill PrewCommented:
Give this a try, it will recursively dig down into all subfolders under the starting folder and process files in each of those as well.

Keep in mind that running this on a full disk drive will take quite a bit of time, and you may also run into some permissions errors on certain folders if you do not have access to them.

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "D:\"
strBaseExt = ".ABC"

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

ProcessFolder(objFolder)

Sub ProcessFolder(objFolder)
   ' Process each file in the folder
   For Each objFile in objFolder.Files

       ' See if it matches the pattern of filenames we want
       If UCase(Right(objFile.Name, Len(strBaseExt))) = UCase(strBaseExt) 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, "L:\somedir\somedir", "i:\newdir\newdir", 1, -1, vbTextCompare)
           strData = Replace(strData, "L:\somedir\somedir2", "i:\newdir\newdir2", 1, -1, vbTextCompare)
           strData = Replace(strData, "L:\somedir\somedir3", "i:\newdir\newdir3", 1, -1, vbTextCompare)

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

       End If

   Next

   ' Recusively process all subfolders of this one
   For Each objSubFolder In objFolder.Subfolders
      ProcessFolder(objSubFolder)
   Next

End Sub

Open in new window

~bp
0
crash1624Author Commented:
Thanks BP,

I'm getting an access denied error 800A0046.  I can't even run it one sub directory deep.  I have adequate permissions and have attempted to run through elevated command prompt.  Any thoughts?
0
Bill PrewCommented:
Okay, give this a try and see if it helps.

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "D:\"
strBaseExt = ".ABC"

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

' Recover from errors in the code (prevent from ending script)
On Error Resume Next

ProcessFolder(objFolder)

Sub ProcessFolder(objFolder)
   ' Recover from errors in the code (prevent from ending script)
   On Error Resume Next

   ' Process each file in the folder
   For Each objFile in objFolder.Files
      If Err.Number = 0 Then

         ' See if it matches the pattern of filenames we want
         If UCase(Right(objFile.Name, Len(strBaseExt))) = UCase(strBaseExt) 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, "L:\somedir\somedir", "i:\newdir\newdir", 1, -1, vbTextCompare)
             strData = Replace(strData, "L:\somedir\somedir2", "i:\newdir\newdir2", 1, -1, vbTextCompare)
             strData = Replace(strData, "L:\somedir\somedir3", "i:\newdir\newdir3", 1, -1, vbTextCompare)

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

         End If

      End If

   Next

   ' Recusively process all subfolders of this one
   For Each objSubFolder In objFolder.Subfolders
      If Err.Number = 0 Then
         ProcessFolder(objSubFolder)
      End If
   Next

End Sub

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
crash1624Author Commented:
No errors this time and it changed the file on the root of D: but it didn't change anything in any sub directories under D:
0
Bill PrewCommented:
Okay, we'll probably need to add some logging to see what is going on.

~bp
0
crash1624Author Commented:
I used vbsedit debug and nothing came back.  What else should I do?
0
Bill PrewCommented:
Okay, try with this extra logic.

' Constants for I/O
Const ForReading = 1
Const ForWriting = 2

' Define folder to search
strBaseDir = "D:\"
strBaseExt = ".ABC"

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

' Recover from errors in the code (prevent from ending script)
On Error Resume Next

ProcessFolder(objFolder)

Sub ProcessFolder(objFolder)
   ' Recover from errors in the code (prevent from ending script)
   On Error Resume Next

   ' Process each file in the folder
   For Each objFile in objFolder.Files
      If Err.Number = 0 Then

         ' See if it matches the pattern of filenames we want
         If UCase(Right(objFile.Name, Len(strBaseExt))) = UCase(strBaseExt) Then

             Wscript.Echo "Processing file [" & objFile.Path & "]"

             ' 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, "L:\somedir\somedir", "i:\newdir\newdir", 1, -1, vbTextCompare)
             strData = Replace(strData, "L:\somedir\somedir2", "i:\newdir\newdir2", 1, -1, vbTextCompare)
             strData = Replace(strData, "L:\somedir\somedir3", "i:\newdir\newdir3", 1, -1, vbTextCompare)

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

         End If
         
      Else
         ' Handle any access errors for files we can't get to
         Wscript.Echo "ERROR: Accessing file [" & objFile.Path & "]"
         Err.Clear
      End If

   Next

   ' Recusively process all subfolders of this one
   For Each objSubFolder In objFolder.Subfolders
      If Err.Number = 0 Then
         ProcessFolder(objSubFolder)
      Else
         ' Handle any access errors for folders we can't get to
         Wscript.Echo "ERROR: [" & Err.Number & "] Accessing subfolder [" & objSubFolder.Path & "]"
         Err.Clear
      End If
   Next

End Sub

Open in new window

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.