MS Word renaming script help

Hello,  looking for a bit of assistance with this script:

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\renamedocs")

Set objWord = CreateObject("Word.Application")

For Each strFile in objFolder.Files
    arrNames = Split(strFile.Name, ".")
    If arrNames(1) = "doc" Then
        Set objDoc = objWord.Documents.Open(strFile.Path)
        Set objRange = objDoc.Range(0,20)
        strNewName = "C:\renamedocs\" & objRange.Text & ".doc"
        objDoc.Close
        Wscript.Sleep 3000
        objFSO.MoveFile strFile.Path, strNewName
    End If
Next

objWord.Quit

getting an error that says:  

Missing opening '(' after keyword 'for'.
At C:\scripts\docscript2.ps1:6 char:5
+ For  <<<< Each strFile in objFolder.Files
    + CategoryInfo          : ParserError: (OpenParenToken:TokenId) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

basically,  i'm trying to have the script open the files in a folder, look for the first 20 char, then rename the file that and then go on to the next file in the folder/do that for all the files in that folder.   Thank you for any assistance.   (this is a powershell script,  run on Win 7 cpu.   powershell 2.0)


  • Edited by ss4040 15 hours 19 minutes ago
May 28th, 2015 11:51am

basically,  i'm trying to have the script open the files in a folder, look for the first 20 char, then rename the file that and then go on to the next file in the folder/do that for all the files in that folder.   Thank you for any assistance.   (this is a powershell script,  run on Win 7 cpu.   powershell 2.0)


Just to point out, what you posted is not PowerShell, but VBScript.  Try renaming the script file to .vbs so it will run under wscript/cscript instead of trying to run it in Powershell. 
  • Proposed as answer by jrv 12 hours 30 minutes ago
Free Windows Admin Tool Kit Click here and download it now
May 28th, 2015 1:53pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics