利用計劃任務和VBS指令碼實現自動WEB共用資料夾裡的檔案

來源:互聯網
上載者:User
web|指令碼

  其實沒有題目寫得那麼複雜,就是用計劃任務每隔一段時間調用VBS指令碼,重新整理產生一個檔案清單檔案用於WEB服務調用。

Option Explicit
On Error Resume Next

'產生列表的檔案類型
Const sListFileType = "wmv,rm,wma"

'檔案所在的相對路徑
Const sShowPath="."

'排序類型的常量定義
Const iOrderFieldFileName = 0
Const iOrderFieldFileExt = 1
Const iOrderFieldFileSize = 2
Const iOrderFieldFileType = 3
Const iOrderFieldFileDate = 4

'排序順逆的常量定義
const iOrderAsc = 0
const iOrderDesc = 1

'產生列表的檔案數量
const iShowCount = 20


 
'顯示的日期格式函數
Function Cndate2(date1,intDateStyle)
 dim strdate,dDate1
    strdate=cstr(date1)
    If Isdate(strdate) Then
        If Left(cstr(strdate),1)="0" Then
            dDate1=Cdate("20"+cstr(strdate))
        else
            dDate1=Cdate(strdate)
        End If
    Else
      dDate1=Now()
    End If
    Select case intDateStyle
    Case 1:
     Cndate2 = Cstr(Year(dDate1))+"-"+Cstr(Month(dDate1))+"-"+Cstr(Day(dDate1))
    Case 2:
     Cndate2 = Cstr(Month(dDate1))+"-"+Cstr(Day(dDate1))
    Case 3:
     Cndate2 = Cstr(Month(dDate1))+"月"+Cstr(Day(dDate1))+"日"
    Case 4:
     Cndate2 = Cstr(year(dDate1))+"年"+ Cstr(Month(dDate1))+"月"+Cstr(Day(dDate1))+"日"
    End Select
End Function


Function ListFile(strFiletype,intCompare,intOrder,intShowCount)
 Dim sListFile
 Dim fso, f,  f1,  fc, s,ftype,fcount,i,j,k
 Dim t1,t2,t3,t4,t5
 Dim iMonth,iDay
 sListFile = ""
 Set fso = CreateObject("Scripting.FileSystemObject")
 Set f = fso.GetFolder(sShowPath)
 Set fc = f.Files
 fcount = fc.count
 redim arrFiles(fcount,5)
 redim arrFiles2(fcount,5)
 i=0
'排序
 For Each f1 in fc
  ftype = right(f1.name,len(f1.name)-instrrev(f1.name,"."))
  arrFiles(i,0) = f1.name
  arrFiles(i,1) = ftype
  arrFiles(i,2) = f1.size
  arrFiles(i,3) = f1.type
  arrFiles(i,4) = f1.DateLastModified
  i=i+1
 Next
 For i=0 to fcount-1
  for j=i+1 to fcount-1
   select Case intCompare
   Case iOrderFieldFileName,iOrderFieldFileExt,iOrderFieldFileType:
    If arrFiles(i,intCompare)>arrFiles(j,intCompare) then
     t1 = arrFiles(i,0)
     t2 = arrFiles(i,1)
     t3 = arrFiles(i,2)
     t4 = arrFiles(i,3)
     t5 = arrFiles(i,4)

     arrFiles(i,0) = arrFiles(j,0)
     arrFiles(i,1) = arrFiles(j,1)
     arrFiles(i,2) = arrFiles(j,2)
     arrFiles(i,3) = arrFiles(j,3)
     arrFiles(i,4) = arrFiles(j,4)

     arrFiles(j,0) = t1
     arrFiles(j,1) = t2
     arrFiles(j,2) = t3
     arrFiles(j,3) = t4
     arrFiles(j,4) = t5
    end if
   Case iOrderFieldFileSize:
    If cdbl(arrFiles(i,intCompare))>cdbl(arrFiles(j,intCompare)) then
     t1 = arrFiles(i,0)
     t2 = arrFiles(i,1)
     t3 = arrFiles(i,2)
     t4 = arrFiles(i,3)
     t5 = arrFiles(i,4)

     arrFiles(i,0) = arrFiles(j,0)
     arrFiles(i,1) = arrFiles(j,1)
     arrFiles(i,2) = arrFiles(j,2)
     arrFiles(i,3) = arrFiles(j,3)
     arrFiles(i,4) = arrFiles(j,4)

     arrFiles(j,0) = t1
     arrFiles(j,1) = t2
     arrFiles(j,2) = t3
     arrFiles(j,3) = t4
     arrFiles(j,4) = t5
    end if
   Case iOrderFieldFileDate:
    If Cdate(arrFiles(i,intCompare))>Cdate(arrFiles(j,intCompare)) then
     t1 = arrFiles(i,0)
     t2 = arrFiles(i,1)
     t3 = arrFiles(i,2)
     t4 = arrFiles(i,3)
     t5 = arrFiles(i,4)

     arrFiles(i,0) = arrFiles(j,0)
     arrFiles(i,1) = arrFiles(j,1)
     arrFiles(i,2) = arrFiles(j,2)
     arrFiles(i,3) = arrFiles(j,3)
     arrFiles(i,4) = arrFiles(j,4)

     arrFiles(j,0) = t1
     arrFiles(j,1) = t2
     arrFiles(j,2) = t3
     arrFiles(j,3) = t4
     arrFiles(j,4) = t5
    end if
   End Select
  next
 next
'產生列表
   sListFile = sListFile + ("<table cellpadding=0 cellspacing=0 width=100% align=center class=""PageListTable"" style=""BEHAVIOR: url(images/sort2.htc); "">")
   sListFile = sListFile + ("<THEAD><Tr class=PageListTitleTr><Td class=PageListTitleTd>")
   sListFile = sListFile + ("名稱")
   sListFile = sListFile + ("</td><Td class=PageListTitleTd>")
   sListFile = sListFile + ("媒體")
   sListFile = sListFile + ("</td><Td class=PageListTitleTd>")
   sListFile = sListFile + ("大小")
   sListFile = sListFile + ("</td><Td class=PageListTitleTd>")
   sListFile = sListFile + ("類型")
   sListFile = sListFile + ("</td><Td class=PageListTitleTd ID=updatetime>")
   sListFile = sListFile + ("更新時間")
   sListFile = sListFile + ("</td></Tr></THEAD>")
   dim iLoopStart,iLoofEnd,iLoopStep
 If intOrder = 0 then
  iLoopStart = 0
  iLoofEnd = fcount-1
  iLoopStep = 1
 Else
  iLoopStart = fcount-1
  iLoofEnd = 0
  iLoopStep = -1
 End if
 dim iCount,sTDStyleClass
 iCount = 1
 For j=iLoopStart to iLoofEnd Step iLoopStep
  If instr(strFiletype,arrFiles(j,1))>0 and iCount<=intShowCount then
   sTDStyleClass = "PageListTd"+Cstr((iCount mod 2)+1)
      sListFile = sListFile + ("<Tr class=PageListTr><Td class="+sTDStyleClass+">")
      sListFile = sListFile + ("<img src=images/"+arrFiles(j,1)+".gif align=absbottom><img src=http://www.webjx.com/htmldata/2005-11-20/b.gif width=2 height=0><a href=" & sShowPath & "/" & CStr(arrFiles(j,0)) &">" & arrFiles(j,0) &"</a>")
      If datediff("h",arrFiles(j,4),now)<=24 then
       sListFile = sListFile + "<img src=http://www.webjx.com/htmldata/2005-11-20/images/new.gif align=absmiddle>"
      end if
      sListFile = sListFile + "</td><Td class="+sTDStyleClass+">"
      sListFile = sListFile + ("<a href=" & sShowPath & "/" & CStr(arrFiles(j,0)) &">")
'根據檔案名稱規則,產生中文提示
      select case left(arrFiles(j,0),3)
      case "sc2":
       sListFile = sListFile + "<font color=#AA0000>四川衛視 "
      case "sd2":
       sListFile = sListFile + "<font color=#00AA00>山東衛視 "
      case "gd2":
       sListFile = sListFile + "<font color=#0000AA>廣東衛視 "
      case "gx2":
       sListFile = sListFile + "<font color=#AAAA00>廣西衛視 "
      end select
'日期顯示
      If isnumeric(left(right(arrFiles(j,0),8),2)) then
       iMonth = cint(left(right(arrFiles(j,0),8),2))
       iDay = cint(left(right(arrFiles(j,0),6),2))
       sListFile = sListFile + cstr(iMonth)+"月" + cstr(iDay)+"日"
       sListFile = sListFile + ("</a></td><Td class="+sTDStyleClass+" align=right>")
      Else
       response.write arrFiles(j,0)
      end if
      If arrFiles(j,2)>1024*1024 then
      sListFile = sListFile + cstr(round(arrFiles(j,2)/1024/1024))
      sListFile = sListFile + ("MB")
      else
      sListFile = sListFile + cstr(round(arrFiles(j,2)/1024))
      sListFile = sListFile + ("KB")
      end if
      sListFile = sListFile + (" </td>")
      sListFile = sListFile + ("<Td class="+sTDStyleClass+">")
      sListFile = sListFile + cstr(arrFiles(j,3))
      sListFile = sListFile + ("</td>")
      sListFile = sListFile + ("<Td class="+sTDStyleClass+">")
      sListFile = sListFile + (Cndate2(arrFiles(j,4),4))
      sListFile = sListFile + ("</td>")
      sListFile = sListFile + ("</Tr>")
   iCount = iCount+1
  end if
 next
 sListFile = sListFile +  "</table>"
 ListFile = sListFile
End Function

'產生調用檔案的過程
Sub ShowFileListContent()
 Dim tUpdatetime,sUpdateContent

 Dim fso,f,f_js,f_js_write
 Set fso = CreateObject("Scripting.FileSystemObject")
 Set f = fso.GetFolder(sShowPath)
 Set f_js = fso.GetFile("list.js")
 
'比較調用檔案與檔案夾的最後修改時間
 If f.DateLastModified<>f_js.DateLastModified then
  sUpdateContent = ListFile(sListFileType,iOrderFieldFileDate,iOrderDesc,iShowCount)
  Set f_js_write = fso.CreateTextFile("list.js", True)
'JS調用就加上下面這對document.write
'  f_js_write.Write ("document.write('")
  f_js_write.Write (sUpdateContent)
'  f_js_write.Write ("')")
  f_js_write.Close
 End If
End Sub

Call ShowFileListContent()



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。