How do I properly use the regular expression (RegExp) to replace specific patterns in a string?

Good evening Experts,

usually, I am pretty adept at Vbscript, but I have come across problem that shouldn't be a problem. The code snippet below is simply supposed to replace the occurrence of "COM"  with "123", but it doesn't work :

Flder = "J:\MAC"


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & Flder & "'} " _
        & "WHERE AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")
            
k=1
For Each objFolder In colSubfolders
	objFldr = objFolder.FileName
k=k+1

  Set regEx = New RegExp            ' Create regular expression.
  regEx.Pattern = "COM"            ' Set pattern.
  regEx.IgnoreCase = True            ' Make case insensitive.
  ReplaceTest = regEx.Replace(objFldr, "123")   ' Make replacement.
      Wscript.Echo ucase(objFldr)

Next

Open in new window


thanks in advance for your anticipated responses and possible solution.
P.S I already tried the regular VBS replace function and it doesn't work either.
BLACK THANOSAsked:
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.

hieloCommented:
>> ReplaceTest = regEx.Replace(objFldr, "123")   ' Make replacement.
The augmented string should be in ReplaceTest, so try:
Wscript.Echo ucase( ReplaceTest )
0
Bill PrewCommented:
No need to use REGEX for this, a simple replace() can be used, as follows.  Keep in mind this wont rename any files, will just display the name with the replacement.  Not sure what you are trying to actually accomplish, more info would be helpful on that.

Flder = "J:\MAC"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & Flder & "'} " _
        & "WHERE AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")
            
k = 1
For Each objFolder In colSubfolders
   strFolderName = objFolder.FileName
   k = k + 1

   strReplaceTest = Replace(strFolderName, "COM", "123", 1, -1, vbTextCompare)
   Wscript.Echo strReplaceText
Next

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
BLACK THANOSAuthor Commented:
Bill Prew,
your solution was the most effective. The change I was looking for was exactly what I wanted. Know I can put extra code in the script to actually rename the folders.

Awesome
Regards,
Regis Hyde
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Bill PrewCommented:
Great, glad that helped.

~bp
0
hieloCommented:
FYI:  folder/file names such as "COMMUNICATIONS" or "COMMON" contain "COM" and the "general" replace function will make a replacement on that substring.  If  "COM" is meant to be a "whole word", then use the regex:
 regEx.Pattern = "\bCOM\b"

which will not make a replacement if, for instance, "COMMUNICATIONS" or "COMMON" are found.  It's not clear what was your intention.

Regards,
Hielo
0
BLACK THANOSAuthor Commented:
thx for the extra info
0
hieloCommented:
You are welcome.
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
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.