VBScript FileSystemObject.Copyfolder 和 FileSystemObject.CreateFolder

标签 vbscript filesystemobject create-directory

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

这段代码很好用。

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)

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

关于VBScript FileSystemObject.Copyfolder 和 FileSystemObject.CreateFolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23807562/

相关文章:

vbscript - 在 VBScript 中对方法调用进行字符串化

excel - 从 pdf 中提取表格(到 Excel),首选。带VBA

vbscript - 如何找到文件路径

java - 如何使用 Java 在 TOMCAT 中创建一个目录?

java - 如何在java中创建空文件夹?

vbscript 通过任务计划程序在 Windows 10 中打开 excel 文件

arrays - 声明动态数组未按预期工作

audio - 使用VBScript切换当前的有源声音设备?

c# - 如何从脚本访问和使用 Scripting.FileSystemObject#

javascript - 在 asp.net mvc 中单击按钮后创建一个文件夹