【问题标题】:VBScript FileSystemObject.Copyfolder and FileSystemObject.CreateFolderVBScript FileSystemObject.Copyfolder 和 FileSystemObject.CreateFolder
【发布时间】:2014-07-11 12:49:34
【问题描述】:

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

这段代码很好用。

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

【问题讨论】:

    标签: vbscript filesystemobject create-directory


    【解决方案1】:

    正如@aphoria 提到的,CreateFolder() 一次只能创建一个关卡。不过,您可以调用mkdir 命令一次性创建整个文件夹结构。

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

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

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

    【讨论】:

    • 谢谢您,我最喜欢您的解决方案,但在@aphoria 上标记了答案,因为他们首先发布了答案。 +1
    【解决方案2】:

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

    你需要做这样的事情(这只是一个例子……还有很大的改进空间):

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

    或者,您可以创建一个函数,一次创建多个深层文件夹。 Here is an example 的函数。

    【讨论】:

    • 谢谢 我从来不知道它只能创建一个文件夹。现在对我来说很有意义。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-05-15
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多