-1

在一些朋友的帮助下,我做了这个代码来根据文件扩展名将文件复制到不同的文件夹。我打算每十分钟运行一次这个脚本,所以每当它复制一个新文件时,它应该以文本格式记录到驱动器 C。这可能吗?请帮帮我。

Set fso = CreateObject("Scripting.FileSystemObject")

Set testfolder = CreateObject("Scripting.Dictionary")
testfolder.CompareMode = vbTextCompare  'case-insensitive
testfolder.Add "env", "D:\env"
testfolder.Add "ikey", "D:\key"
'fso.CreateFolder(testfolder)

CopyFiles fso.GetFolder("D:\source")

Sub CopyFiles(fldr)
 For Each f In fldr.Files
basename  = fso.GetBaseName(f)
extension = fso.GetExtensionName(f)
If testfolder.Exists(extension) Then
dest  = fso.BuildPath(testfolder(extension), f.Name)
If Not fso.FileExists(dest) Then f.Copy dest
End If

Next

For Each sf In fldr.SubFolders
CopyFiles sf
Next
End Sub
4

1 回答 1

1

您需要为此创建一个新对象,然后写入文件。

所以,你需要

dim fs,fi
Set fs= CreateObject("Scripting.FileSystemObject")
Set fi=fs.CreateTextFile("testFile.txt",true, -1)
Set fso = CreateObject("Scripting.FileSystemObject")
Set testfolder = CreateObject("Scripting.Dictionary")

testfolder.CompareMode = vbTextCompare  'case-insensitive
testfolder.Add "env", "D:\env"
testfolder.Add "ikey", "D:\key"
'fso.CreateFolder(testfolder)

CopyFiles fso.GetFolder("D:\source")

Sub CopyFiles(fldr)
   For Each f In fldr.Files
      basename  = fso.GetBaseName(f)
      extension = fso.GetExtensionName(f)

      If testfolder.Exists(extension) Then
         dest  = fso.BuildPath(testfolder(extension), f.Name)
      End If

      If Not fso.FileExists(dest) Then 
         f.Copy dest
         fi.writeline("This file copied") ' THIS IS WHERE THE FAULT IS
      End If
    Next
    For Each sf In fldr.SubFolders
        CopyFiles sf
    Next
    fi.close
    set f=nothing
    set fs=nothing
End Sub
于 2013-04-29T12:33:16.907 回答