2012-09-26 55 views
3

我目前有一個VBscript,它可以掃描文件夾中的文件並根據文件名中的關鍵詞將文件移動到特定的文件夾。用特定的擴展名移動文件的VBScript

我現在需要腳本只掃描一個級別(即不遞歸掃描),我也需要搜索所有的子文件夾。

有人能幫我一個忙嗎?

編輯:由於編寫這個腳本我已經意識到,我需要有這隻有特定的擴展名從特定的文件夾和子文件夾到其他目錄基於文件名移動文件。 例如,我只需要移動.mp4和.avi文件。

有人可以幫我這個嗎?我嘗試過多種方法,但仍然無法獲得遞歸掃描和移動或擴展特定的移動工作。

以下是我目前的腳本。

'======================================================== 
' Script to Move Downloaded TV Shows and Movies to 
' correct folders based on wildcards in File Name 
'======================================================== 

On Error Resume Next 

Dim sTorrents, sTV, sMovie, sFile, oFSO 

' create the filesystem object 
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject") 

' Create Log File 
Set objLog = oFSO.OpenTextFile("c:\temp\log.txt", 8, True) 

' Set Variables 
sTorrents = "C:\Temp\torrents\" 
sTV = "C:\Temp\TV Shows\" 
sMovie = "C:\Temp\Movies\" 

' Scan each file in the folder 
For Each sFile In oFSO.GetFolder(sTorrents).Files 
' check if the file name contains TV Show Parameters 
If InStr(1, sFile.Name, "hdtv", 1) OR InStr(1, sFile.Name, "s0", 1) <> 0 Then 
    ' TV Show Detected - Move File 
    objLog.WriteLine Now() & " - " & sFile.Name & " Detected as TV Show - Moving to " & sTV 
    oFSO.MoveFile sTorrents & sFile.Name, sTV & sFile.Name 
' Move all other Files to Movies Directory 
Else objLog.WriteLine Now() & " - " & sFile.Name & " Detected as Movie - Moving to " & sMovie 
    oFSO.MoveFile sTorrents & sFile.Name, sMovie & sFile.Name 
End If 

Next 

If sTorrents.File.Count = 0 And sTorrents.SubFolders.Count = 0 Then 
    objLog.WriteLine Now() & " - There is nothing left to Process..." 
    objLog.Close 
End If 

回答

0

一些注意事項:

Sub listfolders(startfolder) 
Dim fs 
Dim fl1 
Dim fl2 

    Set fs = CreateObject("Scripting.FileSystemObject") 
    Set fl1 = fs.GetFolder(startfolder) 

    For Each fl2 In fl1.SubFolders 
     Debug.Print fl2.Path 

     ''process the files 
     ProcessFiles fl2.Path 

     'Recursion: lists folders for each subfolder 
     listfolders fl2.Path 
    Next 
End Sub 

''Code copied from question 
Sub ProcessFiles(sPath) 
    ' Scan each file in the folder 
    For Each sFile In oFSO.GetFolder(sPath).Files 
     ' check if the file name contains TV Show Parameters 
     If InStr(1, sFile.Name, "hdtv", 1) OR InStr(1, sFile.Name, "s0", 1) <> 0 Then 
      ' TV Show Detected - Move File 
      objLog.WriteLine Now() & " - " _ 
        & sFile.Name & " Detected as TV Show - Moving to " & sTV 
      oFSO.MoveFile sTorrents & sFile.Name, sTV & sFile.Name 
     ' Move all other Files to Movies Directory 
     Else 
      objLog.WriteLine Now() & " - " _ 
      & sFile.Name & " Detected as Movie - Moving to " & sMovie 
      oFSO.MoveFile sTorrents & sFile.Name, sMovie & sFile.Name 
     End If 
    Next 
End Sub 
+0

我不太清楚如何實現這個到我的腳本?我需要能夠在子文件夾項目上運行上面的代碼。 – wjbeckett

0

之前延長放*會發現與externsion的所有文件。 例子:oFSO.MoveFile (PATH\*.EXTERNSION)

0

這裏是一個recusive功能列出文件夾和子文件夾 它的測試和工作文件,但你可能會需要一些適應自己forkflow。它不是最優化的,但它很容易閱讀

Sub test() 
    aFiles = F_ListFilesInDirAndSubDir("C:\foo\folder") 
    'then, add some code to parse the array: 
    For i = 0 to UBound(aFiles) 
     'Move or not to move, that is what your code should tell 
    Next 
End Sub 

Public Function F_ListFilesInDirAndSubDir(ByVal sDir) 
    '=============================================================================== 
    'Get the list of files in a directory and in all its sub directories With the full path 
    '=============================================================================== 
    Dim sChild  As String 
    Dim aFolders As Variant 
    Dim aFiles  As Variant 
    Dim aChildFiles As Variant 
    Dim i   As Long 
    Dim j   As Long 
    F_ListFilesInDirAndSubDir = aFiles 
    Set fs = CreateObject("Scripting.FileSystemObject") 
    If Not fs.FolderExists(sDir) Then Exit Function 

    'Get the files in the directory 
    aFiles = F_ListFilesInDir(sDir) 
    'Add the fullpath 
    For i = 0 To UBound(aFiles) 
     If aFiles(i) <> "" Then 
      aFiles(i) = sDir & "\" & CStr(aFiles(i)) 
     End If 
    Next 

    'get the folders 
    aFolders = F_ListFoldersInDir(sDir) 

    'for each folders, push the files in the file list 
    For i = 0 To UBound(aFolders) 
     If aFolders(i) <> "" Then 
      sChild = sDir & "\" & CStr(aFolders(i)) 
      'Recursive call on each folders 
      aChildFiles = F_ListFilesInDirAndSubDir(sChild) 
      'Push new items 
      For j = 0 To UBound(aChildFiles) 
       If aChildFiles(j) <> "" Then 
        ReDim Preserve aFiles(UBound(aFiles) + 1) 
        aFiles(UBound(aFiles)) = aChildFiles(j) 
       End If 
      Next 
     End If 
    Next 

    F_ListFilesInDirAndSubDir = aFiles 
End Function 

Public Function F_ListFilesInDir(ByVal sDir) 
    '=============================================================================== 
    'Get the list of files in a directory 
    '=============================================================================== 
    Dim aList  As Variant 
    Dim i   As Long 
    Dim iChild As Long 
    Dim oFile 
    Dim oFolder 
    Dim oChildren 
    ReDim aList(0) 
    F_ListFilesInDir = aList 

    Set fs = CreateObject("Scripting.FileSystemObject") 

    If Not fs.FolderExists(sDir) Then Exit Function 

    Set oFolder = fs.GetFolder(sDir) 
    Set oChildren = oFolder.Files 

    iChild = CDbl(oChildren.Count) - 1 
    If iChild = -1 Then Exit Function 

    ReDim aList(iChild) 
    i = 0 
    For Each oFile In oChildren 
     aList(i) = oFile.Name 
     i = i + 1 
    Next 

    F_ListFilesInDir = aList 
End Function 

Public Function F_ListFoldersInDir(ByVal sDir As String) As Variant 
    '=============================================================================== 
    'Get the list of folders in a directory 
    '=============================================================================== 
    Dim aList  As Variant 
    Dim i   As Long 
    Dim oDir 
    Dim oFolder 
    Dim oChildren 
    ReDim aList(0) 

    F_ListFoldersInDir = aList 

    Set fs = CreateObject("Scripting.FileSystemObject") 
    If Not fs.FolderExists(sDir) Then Exit Function 

    Set oFolder = fs.GetFolder(sDir) 
    Set oChildren = oFolder.SubFolders 

    If oChildren.Count = 0 Then Exit Function 

    ReDim aList(oChildren.Count - 1) 
    i = 0 
    For Each oDir In oChildren 
     aList(i) = oDir.Name 
     i = i + 1 
    Next 

    F_ListFoldersInDir = aList 
End Function 
相關問題