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.
我有这个VBScript然后运行,当它正在处理时,它将随机停止并要求用户点击空格键以显示其正在进行的输出的其余部分。
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
错误Microsoft VBScript运行时错误:权限被拒绝
I'm a little confusd by this. The logfile was only 356kb
我有点困惑。日志文件只有356kb
3 个解决方案
#1
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:
我能够多次运行你的脚本,而不会暂停输入。使用// X标志运行脚本以在调试器中启动它:
>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.
您也可以在任何地方开始输入wscript.echo跟踪语句,看看是否可以缩小它正在等待的内容。
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.
有一件事让我过去了;如果您的命令控制台处于QuickEdit模式,并且您不小心在控制台窗口中的任何位置单击,则控制台将在等待您按下某个键时挂起。
#2
Well the first step is to remove any global On Error Resume Next statements. Better feedback would come if we could see the script.
那么第一步是删除任何全局On Error Resume Next语句。如果我们能看到脚本,就会有更好的反馈。
#3
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.
当文本文件已经有来自其他进程的打开句柄,或者因为您之前在代码中已经打开了尚未关闭的句柄时,通常会在尝试写入文本文件时获得权限被拒绝。我没有试过这个,但我不知道为什么这不起作用,你可以看看使用来自Sysinternals(微软)的Handle来告诉你哪个进程有文件的打开句柄。请参阅此处以获取有关如何使用Handle的更多参考:http://www.orcsweb.com/blog/post/Closing-open-file-handles.aspx您还可以编写第二个脚本,它在循环中运行以监视主要脚本。第二个脚本可以通过执行WMI流程查询来验证第一个脚本,该查询仅返回与定义的命令行匹配的流程。然后第二个脚本可以重新启动它停止的主,提醒您,记录文件,启动句柄搜索等。
#1
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:
我能够多次运行你的脚本,而不会暂停输入。使用// X标志运行脚本以在调试器中启动它:
>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.
您也可以在任何地方开始输入wscript.echo跟踪语句,看看是否可以缩小它正在等待的内容。
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.
有一件事让我过去了;如果您的命令控制台处于QuickEdit模式,并且您不小心在控制台窗口中的任何位置单击,则控制台将在等待您按下某个键时挂起。
#2
Well the first step is to remove any global On Error Resume Next statements. Better feedback would come if we could see the script.
那么第一步是删除任何全局On Error Resume Next语句。如果我们能看到脚本,就会有更好的反馈。
#3
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.
当文本文件已经有来自其他进程的打开句柄,或者因为您之前在代码中已经打开了尚未关闭的句柄时,通常会在尝试写入文本文件时获得权限被拒绝。我没有试过这个,但我不知道为什么这不起作用,你可以看看使用来自Sysinternals(微软)的Handle来告诉你哪个进程有文件的打开句柄。请参阅此处以获取有关如何使用Handle的更多参考:http://www.orcsweb.com/blog/post/Closing-open-file-handles.aspx您还可以编写第二个脚本,它在循环中运行以监视主要脚本。第二个脚本可以通过执行WMI流程查询来验证第一个脚本,该查询仅返回与定义的命令行匹配的流程。然后第二个脚本可以重新启动它停止的主,提醒您,记录文件,启动句柄搜索等。