簡體   English   中英

使用VBScript移動項目時如何跳過打開的文件夾(和跳過打開的文件)?

[英]How to skip open folders (and skip open files) when moving items using VBScript?

我制作了一個VBscript來移動Windows文件夾,當腳本到達用戶已打開的文件夾時,該腳本將卡住。 有沒有一種方法可以更改腳本,使其跳過打開的文件夾並跳過打開的文件?


Dim fso, directory, item, group, dateStamp, NumberFilesDeleted, text, MoveFoldersErrorInfo, MoveFilesErrorInfo, objLogFile

On error resume Next 

Set fso = CreateObject("Scripting.FileSystemObject")

'Move folders in the temp folder to To_Be_Deleted folder and check for errors
fso.MoveFolder "E:\Projects\temp\*" , "E:\Projects\To_Be_Deleted"
If Err.Number <> 0 Then
   MoveFoldersErrorInfo =  "Error: " & Err.Number & " Error (Hex): " & Hex(Err.Number) & Err.Source & " Description: "  & Err.Description
   Err.Clear
End If

'Move files in the temp folder to To_Be_Deleted folder and check for errors
fso.MoveFile "E:\Projects\temp\*" , "E:\Projects\To_Be_Deleted"
If Err.Number <> 0 Then
    MoveFilesErrorInfo =  "Error: " & Err.Number & " Error (Hex): " & Hex(Err.Number)  & Err.Source & " Description: "  & Err.Description
    Err.Clear
End If

VBScript在文件的移動/刪除操作中不是很可靠。 改用DOS命令,它甚至可以刪除/移動用戶打開的文件。

您將不得不自己創建文件夾,這是一個減號。 但是可以保證它可以正常工作,並且不會給您錯誤消息。

您的代碼可能類似於以下內容:

Set wshShell = CreateObject( "WScript.Shell" )
wshShell.Run "cmd /c mkdir " & strNewDestination & "\" & strFolder, 0, True 
wshShell.Run "cmd /c copy /y " & strFolder & "\*.* " & strNewDestination & "\" & strFolder, 0, True
wshShell.Run "cmd /c del /y " & strFolder & "\*.*", 0, True

Vbscript使用文件操作非常慢,並且如果有可靠的方法來檢查文件是否已打開,它甚至會進一步減慢該過程。 您可以忽略打開的文件,就像在腳本樣本中一樣,“下一步繼續執行錯誤恢復”。

我只知道您要檢查的一個原因,那就是使用systernals的psfile.exe之類的工具強制關閉文件,但我想您的用戶對此不滿意。

作為一個外部程序,可以從腳本運行psfile並檢查是否需要關閉打開的文件,因此可以首先使用/ c參數(強制關閉)在已處理的文件夾上運行它,然后從批處理腳本中移動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM