Page 1 of 1

สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Mon Aug 19, 2019 10:02 am
by lnongkungl
สวัสดีครับ ทุกท่าน รบกวนสอบถามวิธีเลือกไฟล์ที่เก่ากว่าปี 2015 ครับ
คือผมต้องการ list ชื่อไฟล์ใน folder ทั้งหมด แล้วต้องการไฟล์ที่เก่ากว่า ปี 2015 ครับ ซึ่งก็ได้ code มาจาก internet แล้ว แต่ผมปรับยังไงมันก็ยังดึงข้อมูลมาทั้งหมดอยู่ดี

บรรทัดนี้ควร เพิ่มเติมยังไงครับ

Code: Select all

Cells(nextRow, 6) = objFile.DateLastModified

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Mon Aug 19, 2019 5:48 pm
by puriwutpokin
ควรแนบไฟล์ตัวอย่างมาดูด้วยครับ​
พร้อมทั้งโค้ด​ทั้งหมดครับ

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Tue Aug 20, 2019 3:08 pm
by lnongkungl
ไฟล์แนบไม่ได้อ่ะครับ เกินขนาด

code ครับ

Code: Select all

Option Explicit

Sub listAllFile()

Dim objFSO As Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Range("B2").Value)

Call GetFileDetails(objFolder)

End Sub

Function GetFileDetails(objFolder As Scripting.Folder)

Dim objFile As Scripting.File
Dim nextRow As Long
Dim objSubFolder As Scripting.Folder


nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each objFile In objFolder.Files
    Cells(nextRow, 1) = objFile.Name
    Cells(nextRow, 2) = objFile.Path
    Cells(nextRow, 3) = objFile.Size / 1024
    Cells(nextRow, 4) = objFile.Type
    Cells(nextRow, 5) = objFile.DateCreated
    Cells(nextRow, 6) = objFile.DateLastModified
    nextRow = nextRow + 1
Next

For Each objSubFolder In objFolder.SubFolders
    Call GetFileDetails(objSubFolder)
Next

    
End Function

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Tue Aug 20, 2019 7:32 pm
by snasui
:D กรุณาตัดไฟล์มาเฉพาะที่พอเป็นตัวอย่างได้ หรือสร้างไฟล์ตัวอย่างขึ้นมาใหม่สำหรับคำถามนี้โดยเฉพาะ จะได้ะสะดวกในการตอบของเพื่อนสมาชิกครับ

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Wed Aug 21, 2019 1:33 pm
by lnongkungl
ไฟล์ test ครับ

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Wed Aug 21, 2019 9:53 pm
by snasui
:D ตัวอย่างการปรับ Code ครับ

Code: Select all

Sub GetFileDetails(objFolder As Scripting.Folder)
    
    Dim objFile As Scripting.File
    Dim nextRow As Long
    Dim objSubFolder As Scripting.Folder
    Dim yr As Integer
    
    nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
    For Each objFile In objFolder.Files
        yr = Year(objFile.DateCreated)
        If yr <= 2015 Then
            Cells(nextRow, 1) = objFile.Name
            Cells(nextRow, 2) = objFile.Path
            Cells(nextRow, 3) = objFile.Size / 1024
            Cells(nextRow, 4) = objFile.Type
            Cells(nextRow, 5) = objFile.DateCreated
            Cells(nextRow, 6) = objFile.DateLastModified
            nextRow = nextRow + 1
        End If
    Next
    
    For Each objSubFolder In objFolder.SubFolders
        Call GetFileDetails(objSubFolder)
    Next
    
        
End Sub

Re: สอบถามวิธีเลือกเฉพาะไฟล์เก่าๆครับ

Posted: Thu Aug 22, 2019 3:49 pm
by lnongkungl
จริงด้วย ทำไมผมคิดไม่ออก มัวแต่ไปปรับตรงบรรทัดนั้น ใส่ทางเลือกให้มันตั้งแต่แรกก็จบ

ขอบคุณครับ อาจารย์