Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38708
  • 博文数量: 16
  • 博客积分: 550
  • 博客等级: 中士
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2005-05-16 22:49
个人简介

理科男,对未来有超凡的兴趣

文章分类

全部博文(16)

文章存档

2023年(1)

2016年(1)

2011年(1)

2005年(13)

我的朋友
最近访客

分类:

2005-08-16 10:17:37

有的时候,需要对一个目录下所有的某种类型文章进行压缩(例如WORD文档、MP3等)。如果使用手工,则数量少的时候还可以。如果多的话,则不胜其烦。为了解决这类问题,我使用Visual Basic Scripting设计了一个脚本,可以自动达到这个目标。在本脚本中,自动压缩所有文件。为了避免将脚本自己也压缩进去,使用了一些判断。

call main()

Sub main()
    Dim fs              '文件系统。
    Dim f               'folder
    Dim fc              'files
    Dim s                   'string
    Dim ws                  'SHELL。
    Dim subfs
    Dim fi
   
    '创建SHELL。
    Set ws = CreateObject("WScript.Shell")
   
    '创建文件对象。
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(ws.currentdirectory)
 Handle_files(ws.currentdirectory)

    Set subfs = f.SubFolders
    '遍历每个子目录。
    For Each fi In subfs
        Call ListSub(fi.Path)
    Next
End Sub

Sub ListSub(filename)
    On Error Resume Next
    Dim subfs   '子目录。
   
 '首先处理当前目录。
 Handle_Files(filename)

    '创建文件对象。
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(filename)
    Set subfs = f.SubFolders
    For Each fi In subfs
        Call ListSub(fi.Path)
    Next
End Sub

'处理每个目录下的文件。
Sub Handle_Files(foldername)
    '创建文件对象。
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(foldername)
    Set fc = f.Files

     '创建SHELL。
    Set ws = CreateObject("WScript.Shell")
  
    '遍历文件对象。
    For Each fl In fc
  if ((instr(fl.Name,"vbs") = 0) and (instr(fl.Name,"rar") = 0)) then
            '进行压缩。
            s = "winrar M -ep " & fl.Path & ".rar " & fl.Path
            ws.Run s, 0, True
        End If
    Next
End Sub

sub output(string)
 wscript.echo string
end sub

阅读(1218) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~