Create backup folder with several directories and files - vbscript

I am new at this and I am trying to get a VBScript (Because the aplicattion only works with this) tha makes a backup of several directories and files...
EXample:
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
This is what I have so far:
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
Can anyone give me a hand on this?
I already did this.....But i give an error if the folder already exist for the backup....but I cannot start to copy....
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
Thanks in advance
I just created this....
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
But know I have the following problems: If there is nothing on the folders i got an error and i cannot overwrite the folders
Thanks

You can copy files with particular extensions that start with a specific string like this:
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
Another (perhaps simpler) option would be to check the file name with a regular expression:
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

Related

Create multiple folders by merging two VBS codes

'I have 2 scripts but could not combine them
'create multiple folders script:
Dim objFSO, objFolder, strDirectory, i
strDirectory = "C:\Users\test\Desktop\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
i = 1 ''
While i < 150
Set objFolder = objFSO.CreateFolder(strDirectory & i)
i = i+1
''WScript.Quit ''
Wend
'desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
I want the code to automatically find the desktop path and then create the folders, some one help me please ?
To get the desktop folder path string and create a sub directory you can do this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Desktop")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(strPath + "\NewFolder") Then
objFso.CreateFolder strPath + "\NewFolder"

Vbscript copy files based on beginning letters

I am trying to get this script to copy all files starting with "XX". Currently it only copies one file.
Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\source")
strDestFolder = "C:\destination\"
For Each objFile In colFiles.Files
If Left(objFile.Name, 2) = "XX" Then
If objNewestFile = "" Then
Set objNewestFile = objFile
Else
If objNewestFile.DateLastModified < objFile.DateLastModified Then
Set objNewestFile = objFile
End If
End If
End If
Next
If Not objNewestFile Is Nothing Then
objFSO.CopyFile objNewestFile.Path,strDestFolder,True
End If
WScript.Echo "Copied."
You can use wildcards * and ? in [source] argument of FSO .CopyFile method.
So the code may look like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\source\XX*.*", "C:\destination\", True
WScript.Echo "Copied."

How to give a variable path for GetFolder in VBScript?

This script basically goes to a folder and list all the files in that folder and output it in a txt file. Now instead of defining the folder path I want it to use the txt file that contains a bunch of folder paths in it and I want to loop that txt file. How can I do this?
Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile
'Creating File System Object
Set fso = CreateObject("Scripting.FileSystemObject")
'Getting the Folder Object
Set ObjFolder = fso.GetFolder("C:\Users\Susan\Desktop\Anime\ova")
'Creating an Output File to write the File Names
Set ObjOutFile = fso.CreateTextFile("C:\Users\Susan\Documents\iMacros\Macros\WindowsFiles.txt")
'Getting the list of Files
Set ObjFiles = ObjFolder.Files
'Writing Name and Path of each File to Output File
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine(ObjFile.Path)
Next
ObjOutFile.Close
Ruriko, this should be a working version, i would add a check to see if the inputfile exist, i'm sure you can do that yourself.
Dim fso, ObjFolder, ObjOutFile, ObjFiles, ObjFile, outputFile, inputFileList
Const ForReading = 1, ForWriting = 2, ForAppending = 8, CreateIfNeeded = true
inputFileList = "list.txt"
outputFile = "C:\Users\Susan\Documents\iMacros\Macros\WindowsFiles.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set objTextFile = fso.OpenTextFile(inputFileList, ForReading)
Do Until objTextFile.AtEndOfStream
sFolderName = objTextFile.Readline
wscript.Echo "writing contents of " & sFolderName
writefilenames(sFolderName)
Loop
function writefilenames(sFolderName)
Set ObjFolder = fso.GetFolder(sFolderName)
If fso.FileExists(outputFile) Then
Set ObjOutFile = fso.OpenTextFile(outputFile, ForAppending)
Else
Set ObjOutFile = fso.OpenTextFile(outputFile, ForWriting, CreateIfNeeded)
End If
Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine(ObjFile.Path)
Next
ObjOutFile.Close
end function

How to delete file of source after copying in vbscript?

I am developing function that copy file from Temp Folder to Drive C.
After copying, I would like to delete file in Temp Folder.
I tried following codes but can't delete file.Please explain it to me.
sample codes:
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
objFSO.DeleteFile(File)
OR
Set objFSO = CreateObject("Scripting.FileSystemObject")
File = file of Temp Folder
objFSO.CopyFile File, "C:\"
Set delFileName = objFSO.GetFile(File)
delFileName.Delete delFileName
Copying a file from one location to C:\ and then deleting the version in the original location is the same as moving the file, so do that instead:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile File, "C:\"
Set objFSO = Nothing
If you really really want to do it in the way you've described then:
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
objFSO.CopyFile File, "C:\"
If Err.Number = 0 Then objFSO.DeleteFile File
On Error Goto 0
Set objFSO = Nothing
will do the trick.

How to make VBScript copy permissions along with files

This is all in Windows XP utilizing VBScript.
I have a directory with several files inside. The files have varying permissions set. I need to be able to copy the files to a new directory while retaining the permissions. Using the script below the copy works fine but the permissions are overwritten by the new parent folder.
I am aware of xcopy but I am unsure how to make it work within the script. Using robocopy is a slight possibility but should be avoided if at all possible. Other utilities are out of the question due to network contraints.
Any help is greatly appreciated.
Dim CopyFromPath, CopyToPath
Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Const OverwriteExisting = TRUE
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE,"Select folder to copy:",NO_OPTIONS,ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyFromPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = objShell.BrowseForFolder(WINDOW_HANDLE, "Where should the folder be copied to?:", NO_OPTIONS, ssfDRIVES)
if (not objFolder is nothing) then
Set objFolderItem = objFolder.Self
CopyToPath = objFolderItem.Path
else
Set objShell = nothing
WScript.Quit(0)
end if
Set objFolder = nothing
Set objFolderItem = nothing
Set objShell = nothing
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile CopyFromPath & "\*.*", CopyToPath & "\", OverwriteExisting
msgbox "The folder has now been copied to " & CopyToPath
xcopy is a good idea for it.
An example to how make it work within vbscript.
Set oWSHShell = CreateObject("WScript.Shell")
oWSHShell.Exec "xcopy C:\source C:\destination /O /X /H /K /Y"
Set oWSHShell = Nothing

Resources