繁体   English   中英

VBScript FileSystemObject.Copyfolder和FileSystemObject.CreateFolder

[英]VBScript FileSystemObject.Copyfolder and FileSystemObject.CreateFolder

如果在别处得到回答,请道歉。 我正在努力去理解其他论坛帖子上糟糕的书面英语,我真的很想了解发生了什么。

这段代码效果很好。

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

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

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

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

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

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

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

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

True作为第3个参数传递,使脚本等到命令完成后再继续。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM