3
votes

Sorry, but I am a bit new to RegEx and hope someone is able to help.

Files in questions:

    Apples.A.Tasty.Treat.Author-JoeDirt.doc
    Cooking with Apples Publisher-Oscar Publishing.txt
    Candied.Treats.Author-JenBloc.Publisher-Event.docx

I currently use this piece of vbscript code to replace spaces or dashes in the filename with a period but I am wondering if there is a more efficient way to accomplish this?

    Set colRegExMatches = strRegEx.Execute(objSourceFile.Name)
    For Each objRegExMatch in colRegExMatches
      strResult = InStr(objSourceFile.Name, objRegExMatch)
      objTargetFile = Left(objSourceFile.Name, (strResult -1)) & objRegExMatch.Value
      objTargetFile = Replace(objSourceFile.Name, " ", ".", 1, -1, 1)
      objTargetFile = Replace(objSourceFile.Name, "-", ".", 1, -1, 1)
      objSourceFile.Name = objTargetFile
    Next

Once the script above is complete, I have the following list of files:

    Apples.A.Tasty.Treat.Author-JoeDirt.doc
    Cooking.with.Apples.Publisher-Oscar.Publishing.txt
    Candied.Treats.Author-JenBloc.Publisher-Event.docx

Now, I want to find anything beginning with Author or Publisher and simply delete the text until the extension.

    myRegEx.Pattern = (?:Author|Publisher)+[\w-]+\.

This works mostly for the files with the exception if there is an additional period to add a second part of the publisher name or year of publication or book number.

    Apples.A.Tasty.Treat.doc
    Cooking.with.Apples.Publishing.txt
    Candied.Treats.docx

I tried this code and it seems to work but I have to specify the file extensions.

    myRegEx.Pattern = (?:Author|Publisher)[\w-](\S*\B[^txt|docx|doc][\w-].)

If I try the following, it strips the extension for the Candied.Treats file

    myRegEx.Pattern = (?:Author|Publisher)[\w-](\S*\B[^][\w-].)

    Apples.A.Tasty.Treat.doc
    Cooking.with.Apples.txt
    Candied.Treats.

I have been using the RegExr Builder at http://gskinner.com/RegExr to test my patterns but am at a loss right now. Finally once my pattern is working as expected how do I use that in my vbscript? Do I simply add a new line as per below?

    objTargetFile = Replace(objSourceFile.Name, "(?:Author|Publisher)[\w-](\S*\B[^txt|docx|pdf|doc][\w-].)", "", 1, -1, 1)

Thanks.

This is the new vbscript code which seems to do nothing.

    strFixChars = InputBox("Do you want to replace spaces, dashes and strip tags? (Y/N)", "Confirmation")
    Set strRegEx = new RegExp
    For Each objSourceFile in colSourceFiles
      strFileExt = objFSO.GetExtensionName(objSourceFile)
      objLogFile.WriteLine "Input File: " & objSourceFile.Name
      strCount = Len(objSourceFile.Name)
      strRegEx.Pattern = "(?:Author|Publisher)(.+)\."
      strRegEx.IgnoreCase = True
      strRegEx.Global = True
      Set colRegExMatches = strRegEx.Execute(objSourceFile.Name)
      For Each objRegExMatch in colRegExMatches
        strResult = InStr(objSourceFile.Name, objRegExMatch)
        objTargetFile = Left(objSourceFile.Name, (strResult -1)) & objRegExMatch.Value
            If strFixChars = "Y" Then
            objTargetFile = Replace(objSourceFile.Name, " ", ".")
            objTargetFile = Replace(objSourceFile.Name, "-", ".")
            objTargetFile = Replace(objSourceFile.Name, "(?:Author|Publisher)(.+)\.", "")
        End If
        objLogFile.WriteLine "Output File: " & objTargetFile
        strFileList = strFileList & vbCrlf & objTargetFile
    Next
Next
1

1 Answers

0
votes

A quick fix for your regex would be to use (?:Author|Publisher)(.+)\. You will have to replace the first matching group with an empty string in vbscript.