创建包含多个目录和文件的备份文件夹

时间:2013-08-19 10:45:34

标签: vbscript

我是新手,我正在尝试获取VBScript(因为aplicattion仅适用于此)tha备份了几个目录和文件......

示例:

Check if Folder1 exist... (It may be on c:\ or another drive)
If don´t exist the finish
If exist than should create a Bck Folder
THan Backup the folders that are inside folder1 (Folder2, Folder3)
Also backup all the files that are *.mds, *.vbs inside Folder4
The script have to maintain the struture...
And after that delete all folder1

这是我到目前为止所做的:

IF NOT EXIST "%INSTALLDIR%\Folder1\" GOTO ENDPROG 
mkdir "%INSTALLDIR%\BCK\" 
mkdir "%INSTALLDIR%\BCK\DADOS\" 
mkdir "%INSTALLDIR%\BCK\IMAGEM\" 
mkdir "%INSTALLDIR%\BCK\CONFIG\" 
mkdir "%INSTALLDIR%\BCK\OFFBck" 
copy "%INSTALLDIR%\Folder1\Dados*.MDB" "%INSTALLDIR%\BCK\dados\" 
copy "%INSTALLDIR%\Folder1\Dados*.MDD" "%INSTALLDIR%\BCK\dados\" 
copy "%INSTALLDIR%\Folder1\Dados*.VEI" "%INSTALLDIR%\BCK\dados\" 
copy "%INSTALLDIR%\Folder1\Imagem*.*" "%INSTALLDIR%\BCK\Imagem\" 
copy "%INSTALLDIR%\Folder1*.cfg" "%INSTALLDIR%\BCK\Config\" 
copy "%INSTALLDIR%\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\" 
copy "%INSTALLDIR%\Folder1\Folder1\OFFbck*.ZIP" "%INSTALLDIR%\BCK\OFFbck\" 
rmdir "%INSTALLDIR%\Folder1" /s /q 
:ENDPROG 

任何人都可以帮我解决这个问题吗?

我已经这样做了.....但如果文件夹已经存在备份,我会给出错误....但我无法开始复制....

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("C:\Folder1") Then
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Const OverwriteExisting = TRUE
    Set objFolder = objFSO.CreateFolder("C:\BCK")
    Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
    Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
    Set objFolder = objFSO.CreateFolder("C:\BCK\config")
    Set objFolder = objFSO.CreateFolder("C:\BCK\off")
Else
End If

提前致谢

我刚创造了这个......

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FolderExists("C:\audatex") Then
    Const OverwriteExisting = True
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.CreateFolder("C:\BCK")
    Set objFolder = objFSO.CreateFolder("C:\BCK\Imagem")
    Set objFolder = objFSO.CreateFolder("C:\BCK\dados")
    Set objFolder = objFSO.CreateFolder("C:\BCK\WTB")
    Set objFolder = objFSO.CreateFolder("C:\BCK\CFG")
    Set objFolder = objFSO.CreateFolder("C:\BCK\config")
    Set objFolder = objFSO.CreateFolder("C:\BCK\offdaten")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\offdaten\*.zip" , "c:\BCK\Offdaten\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\Dados\*.vei" , "c:\BCK\dados\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\Dados\*.mdd" , "c:\BCK\dados\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\*.cfg" , "c:\BCK\CFG\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\Dados\*.mdb" , "c:\BCK\dados\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFolder "C:\Audatex\Imagem" , "c:\BCK\Imagem" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\WTB\*.wtb" , "c:\BCK\WTB\" , OverwriteExisting
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.CopyFile "C:\Audatex\WTB\*.dtb" , "c:\BCK\WTB\" , OverwriteExisting
End IF  

但是知道我有以下问题:如果文件夹上没有任何内容我收到错误而我无法覆盖文件夹

由于

1 个答案:

答案 0 :(得分:0)

您可以复制具有以特定字符串开头的特定扩展名的文件,如下所示:

For Each f In objFSO.GetFolder("C:\Folder1").Files
  ext = LCase(objFSO.GetExtensionName(f))
  If (ext = "mdb" Or ext = "mdd" Or ext = "vei") And LCase(Left(f.Name, 5)) = "dados" Then
    f.Copy "C:\BCK\dados\"
  End If
Next

另一个(也许更简单)选项是使用正则表达式检查文件名:

Set re = New RegExp
re.Pattern = "^dados.*\.(mdb|mdd|vei)$"
re.IgnoreCase = True

For Each f In objFSO.GetFolder("C:\Folder1").Files
  If re.Test(f.Name) Then f.Copy "C:\BCK\dados\"
Next