3

如果已在其他地方回答此问题,我们深表歉意。我正在努力理解其他论坛帖子上糟糕的书面英语,我真的很想了解发生了什么。

这段代码很好用。

dim FileSys, source, destination, WSNet, theDate, theTime, computerName

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WSNet = CreateObject("WScript.Network")

computerName = WSNet.computerName
theDate = Replace(Date, "/","-")
theTime = Replace(Time, ":",".")
source = "C:\source"
destination = "C:\destfolder"

if Not FileSys.FolderExists(destination) Then
  WScript.echo "the destination: " & destination & " doesnt exist, it will now be created"
  FileSys.Createfolder(destination)
  FileSys.CopyFolder source, destination
Else
  If FileSys.FolderExists(source) Then 
    FileSys.CopyFolder source, destination 
  Else
    WScript.echo "ERROR: The source folder is not present, nothing will be copied"
  End If 
End If

然而,当我替换这一行时:

destination = "C:\destfolder"

像这样的东西:

destination = "C:\destfolder\" & computerName & "\" & theDate & "\" & theTime

我得到一个错误。“找不到路径”,即使我缩小范围并使用:

destination = "C:\destfolder\" & computerName

我犯了同样的错误。在 WScript.echo 行上,字符串按我的预期出现,例如

C:\destfolder\MYPC\22-05-2014\13.55.44

它似乎没有创建文件夹,问题似乎出在 FileSys.CreateFolder 方法上,有人可以帮忙吗?

PS - 我的总体目标是将一些日志文件从一个地方复制到另一个地方,但按日期和时间按文件夹名称排序。

4

2 回答 2

4

正如@aphoria 提到的,CreateFolder()一次只能创建一个级别。但是,您可以调用该mkdir命令在一次调用中创建整个文件夹结构。

With CreateObject("WScript.Shell")
    .Run "cmd /c mkdir ""c:\destfolder\" & computerName & "\" & theDate & "\" & theTime & """", 0, True
End With

作为第二个参数传递0以防止命令提示符窗口在屏幕上闪烁。

作为第三个参数传递True以使您的脚本等到命令完成后再继续。

于 2014-05-23T00:56:13.170 回答
4

CreateFolder方法只能创建一层深的文件夹。

你需要做这样的事情(这只是一个例子......很多改进的空间):

destination = "C:\destfolder"
FileSys.Createfolder(destination)
FileSys.Createfolder(destination & "\" & computerName)
FileSys.Createfolder(destination & "\" & computerName & "\" & theDate)
FileSys.Createfolder(destination & "\" & computerName & "\" & theDate & "\" & theTime)

或者,您可以创建一个函数,一次创建多个深层文件夹。这是执行此操作的函数的示例。

于 2014-05-22T13:26:24.993 回答