为什么我的源文件夹和目标文件夹合并名称?我想在目的地

时间:2015-05-31 10:27:31

标签: vbscript directory copying file-copying

Dim WshNetwork,objNetwork, WshShell, objFSO, objItem
Set objNetwork = CreateObject("Wscript.Network")
Set WshNetwork = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")
If not objFSO.FolderExists("C:\Backups") Then
   objFSO.CreateFolder "C:\Backups"
Else
End If
If not objFSO.FolderExists("C:\Backups\" & strUserName) Then
   objFSO.CreateFolder ("C:\Backups\" & strUserName)
Else
End If
Set FSO = CreateObject("Scripting.FileSystemObject") 
strUserName = objNetwork.UserName
Set oMainFolder = FSO.GetFolder("C:\Users\" & strUserName &"\Documents\")
For Each oFolder In oMainFolder.SubFolders 
  On Error Resume Next
   If oFolder.Name <> "My Music" and oFolder.Name <> "My Pictures" and oFolder.Name <> "My Videos" Then
      FSO.CopyFolder ("C:\Users\" & strUserName &"\Documents\") & oFolder.Name, ("C:\Backups\" & strUserName) & oFolder.Name, True  

      FSO.CopyFile ("C:\Users\" & strUserName &"\Documents\*.*"), ("C:\Backups\" & strUserName), True
   End If
Next

2 个答案:

答案 0 :(得分:3)

因为这里

If not objFSO.FolderExists("C:\Backups\" & strUserName) Then

变量strUserName未初始化/为空。

答案 1 :(得分:0)

Dim WshNetwork,objNetwork, WshShell, objFSO, objItem

Set objNetwork = CreateObject("Wscript.Network")
Set WshNetwork = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject") 

strUserName = objNetwork.UserName

Set oMainFolder = FSO.GetFolder("C:\Users\" & strUserName &"\Documents\")

If not objFSO.FolderExists("C:\MyFiles") Then
  objFSO.CreateFolder "C:\MyFiles"
Else
End If

If not objFSO.FolderExists("C:\MyFiles\" & strUserName & "\") Then
  objFSO.CreateFolder ("C:\MyFiles\" & strUserName & "\")
Else
End If

For Each oFolder In oMainFolder.SubFolders 
  On Error Resume Next
  If oFolder.Name <> "My Music" and oFolder.Name <> "My Pictures" and 
  oFolder.Name <> "My Videos" Then
    FSO.CopyFolder ("C:\Users\" & strUserName &"\Documents\") & oFolder.Name, _
      ("C:\MyFiles\" & strUserName) & "\" & oFolder.Name, True  
    FSO.CopyFile ("C:\Users\" & strUserName &"\Documents\*.*"), _
      ("C:\MyFiles\" & strUserName), True
  End If
Next