[英]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.