ก่อนอื่นเราต้องสร้างออปเจ็ค FileSystemObject โดยใช้ CreateObject method.
ตัวอย่าง
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject") Scripting คือชื่อประเภทของไลบรารี่และ FileSystemObject คือชื่อของออปเจ็คที่เราต้องการสร้างลำดับต่อมาคือการใช้ Method ของ FileSystemObject ตัวอย่างเช่น Create ออปเจ็คก็จะมี CreateTextFile or CreateFolder
ดูตัวอย่างที่ใช้งานกันก่อนดีกว่า Script ด้านล่างนี่คือเราจะทำการ Move ไฟล์จาก C:\tmp\ ไปไว้ที่ C:\test\ และทำการเปลี่ยนชื่อไฟล์เป็นวันที่ LastModified เช็ควันที่ LastModified และ Extention ด้วย
OPTION EXPLICIT
DIM strExtensionsToCopy,strFolder
DIM objFSO, MaxAge, IncludeSubFolders,desFolder
' ************************************************************
' Setup
' ************************************************************
strFolder="C:\tmp\"
desFolder="C:\test\"
' Copy files from sub-folders?
includeSubfolders = true
' A comma separated list of file extensions
' Files with extensions provided in the list below will be copy
strExtensionsToCopy = "txt,temp"
' Max File Age (in Days). Files older than this will be copy.
maxAge = 1
' ************************************************************
set objFSO = createobject("Scripting.FileSystemObject")
CopyFiles strFolder,strExtensionsToCopy, maxAge, includeSubFolders
sub CopyFiles(byval strDirectory,byval strExtensionsToCopy,byval maxAge,includeSubFolders)
DIM objFolder, objSubFolder, objFile
DIM strExt
set objFolder = objFSO.GetFolder(strDirectory)
for each objFile in objFolder.Files
for each strExt in SPLIT(UCASE(strExtensionsToCopy),",")
if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
IF objFile.DateLastModified > (Now - MaxAge) THEN
objFile.Move Replace(objFile.Path,objFile.Name,pd(day(objFile.DateLastModified),2) & pd(month(objFile.DateLastModified),2) & year(objFile.DateLastModified) & pd(hour(objFile.DateLastModified),2) & pd(minute(objFile.DateLastModified),2) & "." & strExt)
objFile.move desFolder
exit for
END IF
end if
next
next
end sub
Function pd(n, totalDigits)
if totalDigits > len(n) then
pd = String(totalDigits-len(n),"0") & n
else
pd = n
end if
End Function
ยังมีเรื่องเล่าอีกเยอะสำหรับ FileSystemObject แล้วจะทยอยนำมาเล่าอีกเรื่อยนะครับ
OZone & Ultra
ไม่มีความคิดเห็น:
แสดงความคิดเห็น