执行过程中脚本超时

时间:2011-01-25 11:17:36

标签: vbscript

我写了一个小脚本来帮助重新组织我的.mp3集合。当我运行这个脚本时,它有时会处理几千个文件,直到它出现错误状态(通常是一个文件的移动,其名称/路径中有一个我没有计算过的特殊字符),但它经常会使用文本

退出脚本
  

脚本“C:\ DevSpace \ mp3move.vbs”超出了脚本执行时间。   脚本执行已终止。

我不确定为什么会这样。为了弄清楚这发生了什么,我添加了几个msgbox行,我发现msgbox会弹出,但随后它会很快自动关闭。

以下是代码 - 我赞成不在论坛中正确获取格式

'Takes all .MP3 files in the source dir, reads the Artist tag associated with that file
'Checks for a dir named after the artist in the destination dir
'If the folder artist/album does not exists, it will create it
'Then move the .mp3 file to the dest dir 
Dim oAppShell, oFSO, oFolder, oFolderItems

Dim strPath, i  

Dim sInfo

iDebug=0

sInfo = "Item Description"

strPath = "K:\_preprocess"
sDestination = "K:\Music"


Set oAppShell = CreateObject("Shell.Application")
Set oFSO = CreateObject("Scripting.FileSystemObject")

If not oFSO.FolderExists(strPath) Then

  WScript.Echo "Folder " & strPath & " is inaccessble"

End If

Set oFolder = oAppShell.NameSpace(strPath)
Set oFolderItems = oFolder.Items()

sCreate = ""
sExist = ""
sMoved = ""
If (not oFolderItems is nothing) Then
  if oFolderItems.Count = 0 then 
    Wscript.echo "no files found in this folder: "  & strPath 
    WScript.Quit 
  end If

  If iDebug = 1 Then
    i = oFolderItems.count
    WScript.Echo i
  End If

For Each oItem in oFolderItems
    If iDebug = 1 Then
        i = i - 1
    End If

    If oItem.Type = "MP3 audio file (mp3)" or oItem.Type = "MP3 Format Sound (.mp3)"_
    Or oItem.Type = "Windows Media Audio file" or oItem.Type = "MP3 Format Sound" then 
        'get artist name
        sArtist = oFolder.GetDetailsOf(oItem, 20)

        If iDebug = 1 Then
            MsgBox oItem.name
            MsgBox sArtist
        End If

        'if 'The Beatles' change to 'Beatles, the'
        If InStr(LCase(sArtist),"the") = 1 Then
            sArtist = Mid(sArtist,5) & ", the"
        End If

        'remove \ from band name
        If InStr(sArtist,"\") > 0 Then
            sArtist = Replace(sAlbum,"\","")        
        End If        

        If InStr(sArtist,"/") > 0 Then
            sArtist = Replace(sAlbum,"/","")        
        End If        

        If iDebug = 1 Then
            MsgBox sArtist
        End If

        'if folder does not exist create
        'MsgBox sDestination & "\" & sArtist
        If oFSO.FolderExists(sDestination & "\" & sArtist) Then
            'MsgBox "EXIST"
            sExist = sExist & sDestination & "\" & sArtist & " exists" & vbCrLf
        Else
            'MsgBox "CREATE " & sDestination & "\" & sArtist
            rtn = oFSO.CreateFolder(sDestination & "\" & sArtist)
            sCreate = sCreate & sDestination & "\" & sArtist & " created" & vbCrLf
        End If              

        'get album name
        sAlbum = oFolder.GetDetailsOf(oItem, 14)

        'remove special characters from album name      
        If InStr(sAlbum,":") > 0 Then
            sAlbum = Replace(sAlbum,":","")     
        End if

        If InStr(sAlbum,"?") > 0 Then
            sAlbum = Replace(sAlbum,"?","")     
        End If      

        If InStr(sAlbum,"...") > 0 Then
            sAlbum = Replace(sAlbum,"...","")       
        End If

        If InStr(sAlbum,"/") > 0 Then
            sAlbum = Replace(sAlbum,"/","")     
        End If

        If InStr(sAlbum,"\") > 0 Then
            sAlbum = Replace(sAlbum,"\","")     
        End If

        'create dir artist/album
        If oFSO.FolderExists (sDestination & "\" & sArtist & "\" & sAlbum) Then
            'sExist = sExist & sDestination & "\" & sArtist & sAlbum & " exists" & vbCrLf
        Else
            'MsgBox sDestination & "\" & sArtist & "\" & sAlbum
            rtn = oFSO.CreateFolder (sDestination & "\" & sArtist & "\" & sAlbum)
            'sCreate = sCreate & sDestination & "\" & sArtist & " created" & vbCrLf
        End If

        'move file
        sSource = strPath & "\" & oItem.name & ".mp3"
        sDest = sDestination & "\" & sArtist & "\" & sAlbum & "\"

        If iDebug=1 Then 
            MsgBox sSource & vbCrLf & sDest
        End If

        If oFSO.FileExists (sSource) Then
            oFSO.MoveFile sSource, sDest
            'sMoved = sMoved & sSource & " moved to " & sDest & vbcrlf
            'MsgBox smoved
        Else
            MsgBox sSource & " not moved"
        End If
    End If

    If iDebug = 1
        WScript.Sleep 1000
        WScript.Echo i
    End If
Next

If iDebug=1 
    WScript.Echo i
End if
'MsgBox sCreate

'MsgBox sExist

'MsgBox sMoved

End If

2 个答案:

答案 0 :(得分:1)

您应该将WScript.Timeout属性设置为更高的值。

参见示例here

答案 1 :(得分:0)

输入文件存在问题。我用来测试的一些.mp3文件在标签中有非ascii字符,导致程序冻结