2011-06-27 6 views
0

これは私の最初の質問です。ファイルを再帰的に移動することについての質問

それはどのような私が今やりたいことは、それが見つけたときがあることである

C:\Test\Sub1 
C:\Test\Sub1\Sub 
C:\Test\Sub2\Sub 

D:\Test\Sub1 
D:\Test\Sub1\Sub 
D:\Test\Sub2\Sub 

たとえば、ファイルが

新しいものである場合にのみ、別のフォルダにすべてのサブフォルダ内のファイルを移動についてスクリプトです拡張子がPdf、新しいファイルがzipxlsC:\Test\Sub2\Subになると、D:\Test\Sub2\Subに直接移動します。

次に、testのフォルダ全体をループし、上記の規則に従ってファイルを移動します。 私はいくつかの例を探していますが、それらは適合しません。

ありがとうございます。

編集

Option Explicit 

const DestFolder = "B:\Testing\" 

MoveFiles 

Sub MoveFiles 
    ' folder to look in 
    Dim strFolderPath : strFolderPath = "D:\Temp\Testing\" 

    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Dim RegEx : Set RegEx = New RegExp 

    ' specify the extension you want to search for; seperate with a | 
    ' currently searching for .txt and .mdb files 
    RegEx.Pattern = "\.(pdf|zip|xls|txt)$" 
    RegEx.IgnoreCase = True 

    RecurseFolder objFSO, strFolderPath, RegEx 
End Sub 

Sub RecurseFolder(objFSO, strFolderPath, RegEx) 
    Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath) 

    Dim objFile, strFileName,dest 
    For Each objFile In objFolder.Files 
     strFileName = objFile.Path 

    If RegEx.Test(strFileName) Then 

        'Checking whether file exist in destination 
     if not objFSO.FileExists(destfolder.strFileName) then 
      objFile.Move destfolder 
     else 
      msgbox "File is already existed" 
     End If 
    End If 
    Next 

    Dim objSubFolder 
    For Each objSubFolder In objFolder.SubFolders 
     RecurseFolder objFSO, objSubFolder.Path, RegEx 
    Next 
End Sub 

私はサブフォルダをループすることができますが、ソースフォルダを応じてフォルダに移動することはできません。たとえば、 FileAは、D:\Temp\Aです。それはB:\Temp\Aに移動されます。しかし今はB:\Tempに移動しました。さらに、私はvbsを書くのにNotePadしか使えないので、私は既存のファイルをチェックするためのバグがあるのか​​分からない。それが正しいか?

私に救いの手を貸してください。私はあなたの優しさにとても感謝しています。

+0

自分で試したことを教えてください。 –

+0

私はそれを示しました。お困りの方は – blami

答えて

0

私はそれをテストしましたが、これはかなりトリックです。あなたは明らかにエラートラッピングを追加したいと思いますが、それはあなたが後にしたことをします。私はファイルタイプの配列を使用しているので、簡単に追加または削除することができ、ドライブ文字の定数も使用できます。

もちろん、存在する場合のみではなく、日付/時刻比較のようにより堅牢にすることはできますが、これは十分な基礎になります。

' Build array of file types 
arrFileTypes = Split("PDF,XLS,ZIP,vbs,jpg", ",") 

Const sourceDrive = "C:" 
Const targetDrive = "P:" 


' Make initial call to get subfolders 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
ShowSubFolders objFSO.GetFolder("C:\test") 

' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
' Subroutine to enumerate folder, called recursively 
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
Sub ShowSubFolders(Folder) 

    For Each Subfolder in Folder.SubFolders 

     ' Get a list of the files in the folder  
     Set objFolder = objFSO.GetFolder(Subfolder.Path) 
     Set filesList = objFolder.Files 

     ' Loop each file and see if it is on the D: 
     For Each file In filesList 

      sourceFile = objFolder.Path & "\" & file.Name 
      targetFile = Replace(sourceFile, sourceDrive, targetDrive) 

      ' Loop allowed extension types 
      For Each extType In arrFileTypes 

       ' Extension match AND it is already there 
       If (UCase(Right(sourceFile, 3)) = UCase(extType)) And objFSO.FileExists(targetFile) Then 
        WScript.Echo "The file already exists on the target " & sourceFile 
       ' Extension match and it is NOT already there 
       ElseIf (UCase(Right(sourceFile, 3)) = UCase(extType)) And objFSO.FolderExists(replace(objFolder.Path, sourceDrive, targetDrive)) Then 
        WScript.Echo "I would move the file, it isn't on target " & sourceFile 
        objFSO.MoveFile sourceFile, targetFile 
       End If 
      Next 

     Next 

     ShowSubFolders Subfolder 

    Next 

End Sub 
+0

Thxをご覧ください。できます。 – blami

関連する問題