首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBScript FileSystemObject.Copyfolder和FileSystemObject.CreateFolder

VBScript FileSystemObject.Copyfolder和FileSystemObject.CreateFolder
EN

Stack Overflow用户
提问于 2014-05-22 21:00:12
回答 2查看 10.3K关注 0票数 3

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

这段代码运行得很好。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
destination = "C:\destfolder"

像这样的东西:

代码语言:javascript
复制
destination = "C:\destfolder\" & computerName & "\" & theDate & "\" & theTime

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

代码语言:javascript
复制
destination = "C:\destfolder\" & computerName

我得到了同样的错误。在WScript.echo行上,字符串如我所期望的那样显示,例如

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

好像不是在创建文件夹,问题似乎出在FileSys.CreateFolder方法上,有谁能帮上忙吗?

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-22 21:26:25

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

您将需要这样做(这只是一个改进空间的example...lots ):

代码语言:javascript
复制
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

票数 4
EN

Stack Overflow用户

发布于 2014-05-23 08:56:13

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

代码语言:javascript
复制
With CreateObject("WScript.Shell")
    .Run "cmd /c mkdir ""c:\destfolder\" & computerName & "\" & theDate & "\" & theTime & """", 0, True
End With

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

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23807562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档