3

I have a simple Git hook that calls some other (VBScript) scripts. The script runs correctly when I call it from the command line. However, when the hook is executed, it gives me the following error(s):

./RevisionDate.vbs: line 1: syntax error near unexpected token `('
./RevisionDate.vbs: line 1: `Set copyFSO = CreateObject ("Scripting.FileSystemObject")'
.git/hooks/pre-commit: line 8: Start-Sleep: command not found
test
./MovePDF.vbs: line 1: unexpected EOF while looking for matching `''
./MovePDF.vbs: line 6: syntax error: unexpected end of file

Here is the VBScript that gets called in the hook.

Set copyFSO = CreateObject ("Scripting.FileSystemObject")
copyFSO.copyFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Test2"
copyFSO.moveFile "C:\Users\Ian\Desktop\Test\*.pdf", "C:\Users\Ian\Desktop\Temp"

'------ Microsoft Excel -------

inputPrefix = cint(inputbox("Please enter two digit prefix for file you would like updated.", "File Update"))
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files
    fileName = oFile
    Set objWorkbook = nothing
    Set objSelection = nothing
    Set objWorksheet = nothing

    If UCase(oFSO.GetExtensionName(oFile.Name)) = "XLSX" Then
        Prefix = left(oFile.Name, 2)
        filePrefix = cint(Prefix)
        Set objWorkbook = objExcel.Workbooks.Open(fileName)
        Set objSelection = objExcel.Selection
        Set objWorksheet = objWorkbook.Worksheets(1)
        objExcel.DisplayAlerts = False
        myYear = Year(Now())
        myMonth = Month(Now())
        myDay = Day(Now())
        myDate = myYear & "/" & myMonth & "/" & myDay
        myDateFile = myYear & "-" & myMonth & "-" & myDay

        If (filePrefix = inputPrefix) then
            objWorksheet.PageSetup.RightFooter = "Revision Date: " & myDate & " C"
            objWorkbook.Save
        End If

        fileName = Replace(oFile.Name, ".xlsx", "")
        saveAndCloseXlsx objWorkbook
    End if
Next

Function saveAndCloseXlsx(objWorkbook)
    objWorkbook.ExportAsFixedFormat xiTypePDF, "C:\Users\Ian\Desktop\Test\" & fileName
    objWorkbook.Close
end Function


'------ Microsoft Word -------

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
sFolder = "C:\Users\Ian\Desktop\Test"
Set oFSO = CreateObject("Scripting.FileSystemObject")

For Each oFile In oFSO.GetFolder(sFolder).Files

    If UCase(oFSO.GetExtensionName(oFile.Name)) = "DOCX" Then
        fileName = oFile
        Set objDoc = objWord.Documents.Open(fileName)
        Set objSelection = objWord.Selection

        If objDoc.Bookmarks.Exists("RevisionDate") = True Then
            Set objRange = objDoc.Bookmarks("RevisionDate").Range
            myYear = Year(Now())
            myMonth = Month(Now())
            myDay = Day(Now())
            myDate = myYear & "/" & myMonth & "/" & myDay
            myDateFile = myYear & "-" & myMonth & "-" & myDay
            Prefix = left(oFile.Name, 2)
            filePrefix = cint(Prefix)

            If (inputPrefix = filePrefix) then
                objRange.text = "Revision Date: " & myDate & " C"
                objDoc.Bookmarks.Add "RevisionDate", objRange
            End If

            wdFormatPDF = 17
            SaveAndCloseDocx objDoc
        End If
    End if
Next

set oFSO = Nothing
objWord.Quit

Function SaveAndCloseDocx(objDoc)
    fileName = Replace(oFile.Name, ".docx", "")
    objDoc.SaveAs "C:\Users\Ian\Desktop\Test\" & fileName & ".pdf", wdFormatPDF
    objDoc.Close
End Function

And finally the hook itself:

#!/bin/sh
#
#
echo "Script Running"
cd 'C:\Users\Ian\desktop\QMS_Manual'
"./RevisionDate.vbs"
Start-Sleep -s 30
"./MovePDF.vbs"
cd 'C:\Users\Ian\desktop\Test2'
pdftk *.pdf cat output ECMWC.pdf
cd 'C:\Users\Ian\desktop\QMS_Manual'
DeleteAllButFinal.vbs

Why does this happen? I've read it may have something to do with the PATH environment variable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ian Pennebaker
  • 45
  • 1
  • 1
  • 5

1 Answers1

1

You probably want to prefix the calls to your .vbs scripts with:

cscript yourscript.vbs

You're running a Bash/sh script from your Git hook.

Your Bash/sh implementation won't know what to do with Start-Sleep because that's a PowerShell command. You'll need to find some Bash equivalent or shell out to PowerShell to execute that statement.

The script then tries to execute the contents of RevisionDate.vbs and MovePDF.vbs as Bash/sh statements rather than launch the VBScript interpreter that would normally be associated under a cmd.exe command shell. So you need to tell your shell script how to do this, i.e., cscript yourscript.vbs.

Paths:

Also watch those paths. Most Windows Bash/sh shell implementations won't know what C:\blah\blah means (the \ is used as an escape character). Instead you need to use Unix-style paths. For example,

'C:\Users\Ian\desktop\Test2'

would translate to:

/c/Users/Ian/desktop/Test2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kev
  • 118,037
  • 53
  • 300
  • 385
  • Thank you for the suggestion but it did not resolve or change the error message – Ian Pennebaker Jun 08 '15 at 17:17
  • Okay so theoretically I would have to go through the whole VBScript and convert to bash for it to run? Is there any way to run a VBScript normally? – Ian Pennebaker Jun 08 '15 at 17:29
  • @IanPennebaker Ok, open a gitbash console then type `cscript`, hit return, and tell me what you see. – Kev Jun 08 '15 at 17:33
  • `Options: //B Batch mode: Suppresses script errors and prompts from displaying //D Enable Active Debugging //E:engine Use engine for executing script //H:CScript Changes the default script host to CScript.exe //H:WScript Changes the default script host to WScript.exe (default) //I Interactive mode (default, opposite of //B) //Job:xxxx Execute a WSF job //Logo Display logo (default) //Nologo Prevent logo display: No banner will be shown at execution time //S Save current command line options for this user ` – Ian Pennebaker Jun 08 '15 at 17:37
  • `//T:nn Time out in seconds: Maximum time a script is permitted to run //X Execute script in debugger //U Use Unicode for redirected I/O from the console` @Kev – Ian Pennebaker Jun 08 '15 at 17:37
  • Sorry, that is difficult to read. I appreciate your help. @Kev – Ian Pennebaker Jun 08 '15 at 17:38
  • @IanPennebaker Right, what I'm saying is that you need to call the cscript interpreter from your bash script so *cscript* interprets and executes your `.vbs` scripts. Git's bash won't know how to do that just by specifying the name of a random script containing statements it won't understand. – Kev Jun 08 '15 at 17:39
  • Thank you very much. You just saved me a lot of headache! – Ian Pennebaker Jun 08 '15 at 17:46
  • @IanPennebaker you're welcome chap. :) Have also added a bit about paths in these types of bash/sh ports to Windows. – Kev Jun 08 '15 at 17:50