文件夹清理 - 移动子文件夹

时间:2016-10-28 14:19:38

标签: vbscript

我设法将某些内容放在一起,将特定扩展名的文件移动到新位置,并根据修改日期组织成年/月文件夹。有人能帮我修改它还将源文件夹中的子文件夹移动到这些有组织的目录中吗? (不仅仅是子文件夹中的文件,还有包含内容的文件夹本身)。这将基于子文件夹的修改日期。

'Declare Variables
Dim srcDir,dstDir,myDir,objFSO,objFolder,objFiles,objFile,strmonth
Const OverwriteExisting = True

'Create a file sytem object as a base for manipulating files and directories
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Define Directories
srcDir = "C:\"
dstDir = "C:\Presentations"

'Get list of files in source directory
Set objFolder = objFSO.GetFolder(srcDir)
Set objFiles = objFolder.Files

'Loop through files and move to relevant folder
For Each objFile In objFiles
    'If file is .csv or .txt 
    If LCase(objFSO.GetExtensionName(objFile.Name)) = "csv" Or LCase(objFSO.GetExtensionName(objFile.Name)) = "txt" Or LCase(objFSO.GetExtensionName(objFile.Name)) = "pptx" Then
        strmonth = Month(objFile.DateLastModified)
        If strmonth = "1" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "January"
        ElseIf strmonth = "2" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "February"
        ElseIf strmonth = "3" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "March"
        ElseIf strmonth = "4" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "April"
        ElseIf strmonth = "5" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "May"
        ElseIf strmonth = "6" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "June"
        ElseIf strmonth = "7" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "July"
        ElseIf strmonth = "8" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "August"
        ElseIf strmonth = "9" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "September"
        ElseIf strmonth = "10" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "October"
        ElseIf strmonth = "11" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "November"
        ElseIf strmonth = "12" Then
            myDir = dstDir & "\" & Year(objFile.DateLastModified) & " - " & "December"
        End If
        If objFSO.FolderExists(myDir) Then
            'If directory exists, move the file there.
            objFSO.CopyFile objFile.Path, myDir & "\" & objFile.Name
            objFSO.DeleteFile objFile
        Else
            'Otherwise create the directory and then move the file there.
            objFSO.CreateFolder(myDir)
            objFSO.CopyFile objFile.Path, myDir & "\" & objFile.Name
            objFSO.DeleteFile objFile
        End If
    End If
Next

0 个答案:

没有答案