我尝试使用以下代码将所有文件打印到默认打印机。但现在具有挑战性的任务是,一旦文件被移动到打印机,我必须删除该文件夹。我试图删除但是在将所有文件移动到打印机之前文件被删除了。
如何检查文件移动后只删除文件夹?
TargetFolder = "C:\users\asankati\desktop\testsb"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
For Each objItem in colItems
objItem.InvokeVerbEx("print")
Next
strPath = "C:\users\asankati\desktop\testsb"
DeleteFolder strPath
Function DeleteFolder(strFolderPath)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
End If
Set objFSO = Nothing
End Function
答案 0 :(得分:0)
在调用下面的函数之前给出一个sleep语句。只需不断增加睡眠时间,直到达到要求为止。
DeleteFolder strPath
答案 1 :(得分:0)
以下是仅在将所有文件移动到打印机时才删除文件夹的代码。
TargetFolder = "C:\users\asankati\desktop\testsb"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(TargetFolder)
Set colItems = objFolder.Items
Dim filename
For Each objItem in colItems
objItem.InvokeVerbEx("print")
Next
set objFolder = Nothing
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "deletevbs.vbs"
Set objShell = Nothing
deletevbs.vbs的代码: - 其中我在递归调用deletefoldere函数,直到文件锁定错误被释放。 strPath =“C:\ users \ asankati \ desktop \ testsb”
On Error Resume Next
DeleteFolder strPath
DO until err.number = 0
DeleteFolder strPath
loop
Function DeleteFolder(strFolderPath)
Dim objFSO, objFolder
Set objFSO = CreateObject ("Scripting.FileSystemObject")
If objFSO.FolderExists(strFolderPath) Then
objFSO.DeleteFolder strFolderPath, True
ELSE
wscript.quit
End If
Set objFSO = Nothing
End Function
on Error GoTo 0