tags:

views:

507

answers:

3

I have this VBScript which runs however, while it is processing, it will randomly stop and require a user to hit the spacebar for it to display the rest of its ongoing output.

How do I figure out why this is happening?

Here is a copy of the script:

'On Error Resume Next

Dim arrFolders()
intSize = 0

Function StampNow()
Dim Hr, Mn, Yr, Mon, Dy, Date1
Date1=Now()

Hr=DatePart("h",Date1)
Mn=DatePart("n",Date1)
Yr = DatePart("yyyy",Date1)
Mon = DatePart("m",Date1)
Dy = DatePart("d",Date1)

StampNow = Yr & "-" & Mon & "-" & Dy
end function

'Output log info.
Function OutputToLog (strToAdd)
    Dim strDirectory,strFile,strText, objFile,objFolder,objTextFile,objFSO
    strDirectory = "c:\log"
    strFile = "\dpadmin_copy2run-"& StampNow &  ".bat"
    'strText = "dpadmin_copy2"
    strText = strToAdd

    ' Create the File System Object.
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Check that the strDirectory folder exists.
    If objFSO.FolderExists(strDirectory) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFolder = objFSO.CreateFolder(strDirectory)
       'WScript.Echo "Just created " & strDirectory
    End If

    If objFSO.FileExists(strDirectory & strFile) Then
       Set objFolder = objFSO.GetFolder(strDirectory)
    Else
       Set objFile = objFSO.CreateTextFile(strDirectory & strFile)
       'Wscript.Echo "Just created " & strDirectory & strFile
    End If

    set objFile = nothing
    set objFolder = nothing
    ' OpenTextFile Method needs a Const value
    ' ForAppending = 8 ForReading = 1, ForWriting = 2
    Const ForAppending = 8

    Set objTextFile = objFSO.OpenTextFile _
    (strDirectory & strFile, ForAppending, True)

    ' Writes strText every time you run this VBScript.
    objTextFile.WriteLine(strText)
    objTextFile.Close
End Function

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "D:\1\production\Openjobs"

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

    dim diffindates

    'Init vars for regex.
    Dim retVal, retVal2
    Dim Lastprop
    Dim objRegExpr 'regex variable
    Set objRegExpr = New regexp
    Set objRegExprX31 = New regexp
    objRegExpr.Pattern = "[0-9][0-9][0-9][0-9][0-9][0-9][A-Z][A-Z][A-Z]"
    objRegExprX31.Pattern = "[0-9][0-9][0-9][0-9][0-9][0-9]X31"
    objRegExpr.Global = True
    objRegExprX31.Global = True
    objRegExpr.IgnoreCase = True
    objRegExprX31.IgnoreCase = True

    'Variables for getting last accessed property.
    Dim fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")

    'Current time vars.
    Dim currenttime
    currenttime = Now()

    ParentFolder = "D:\1\Production\Openjobs\ClosedJobs"

For Each objFolder in colSubfolders
    intSize = intSize + 1

    retVal = objRegExpr.Test(objFolder.Name)
    retVal2 = objRegExprX31.Test(objFolder.Name)
    if (retVal OR retVal2 ) then
        'set filename to array
            strFolderName = objFolder.Name

        'Get last modified date.
        Set f = fs.GetFolder(objFolder.Name)
        Lastprop = f.DateLastModified
        'MsgBox(Lastprop)

        if ( DateDiff("m", f.DateLastModified, Now()) > 4) then
            diffindates =  DateDiff("m", f.DateLastModified, Now())
            Set objShell = CreateObject("Shell.Application")
            Set objCopyFolder = objShell.NameSpace(ParentFolder)

            OutputToLog("rem " & f.DateLastModified & ":" & objFolder.Name )

            outputtolog("move /Y """ & objFolder.Name & """ "  & ParentFolder)

            wscript.echo(diffindates & ":" & objFolder.Name & vbCr)
        end if
    end if
Next

Update

It stops at the line:

Set objTextFile = objFSO.OpenTextFile _
(strDirectory & strFile, ForAppending, True)

with the error Microsoft VBScript runtime error: Permission denied

I'm a little confusd by this. The logfile was only 356kb

A: 

Well the first step is to remove any global On Error Resume Next statements. Better feedback would come if we could see the script.

EBGreen
added a copy of the script to the original question
phill
i commented out the 'on error resume next and it still stops randomly until I press spacebar for it to continue
phill
+1  A: 

I was able to run your script several times without it pausing for input. Run your script with the //X flag to start it in the debugger:

>cscript //nologo //X dpadmin_copy2.vbs"

You should be able to then step through the code.

You can also start putting in wscript.echo trace statements everywhere and see if you can narrow down what it's waiting on.

One thing that's gotten me in the past; If your command console is in QuickEdit mode and you accidentally click anywhere in the console window, the console will hang while it waits for you to press a key.

Patrick Cuff
how do you start the command prompt without QuickEdit mode?
phill
For the shortcut you use to launch the command console, click on the icon in the title bar and select Properties. On the Options tab, make sure QuickEdit Mode is unchecked. Click OK and choose to Modify the shortcut. Launch it again, and QuickEdit will be off.
Patrick Cuff
A: 

You usually get an Permission denied when trying to write to a text file when the text file already has an open handle from some other process or because you have previously opened a handle earlier in you code which you have not closed. I haven't tried this but I don't know why this wouldn't work, you can look at using Handle from Sysinternals (Microsoft) to tell you what process has the open handle for the file. Please see here for a further reference of how to use Handle: http://www.orcsweb.com/blog/post/Closing-open-file-handles.aspx You could also write a second script which runs in a loop to monitor the main script. The second script can verify the first script by doing a WMI Process query which returns only processes that match a defined command line. The second script could then restart the main it stops, alert you, log a file, launch a handle search, etc.

mrTomahawk